I am trying to incorporate some 3D touch into an application and I've run into a weird issue where the forceTouchCapability
check is returning nil
on viewDidLoad
but not in viewWillAppear/viewDidAppear
.
I'm aware that this is only available on iOS 9+ so I've added checks to verify that the traitCollection
property on the view controller responds to forceTouchCapability
as in the following:
- (void)loadView {
self.view = [[MyView alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Checking the force touch availability here
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] &&
self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
// This won't get called because forceTouchCapability is returning nil
// which corresponds to UIForceTouchCapabilityUnknown
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
In LLDB with a breakpoint at the if
statement, entering po [self.traitCollection forceTouchCapability]
returns nil
which corresponds to UIForceTouchCapabilityUnknown
. However, the traitCollection
itself is not nil
.
According to the documentation for UIForceTouchCapabilityUnknown:
UIForceTouchCapabilityUnknown: The availability of 3D Touch is unknown. For example, if you create a view but have not yet added it to your app’s view hierarchy, the view’s trait collection has this value.
Has the view not been added to the hierarchy by this point?
I'm curious if anyone has run into this issue before and how to work around this? I would like to avoid adding this in the viewDidAppear
as this can get called quite a bit.
If it helps, I'm running this on a 6S on iOS 9.1 with Xcode 7.2