Saturday 15 July 2017

How To Add 3D Touch To Your IOS App

3D Touch Utility
The 3D Touch technology was first introduced on the iPhone 6s and 6s+. Devices supporting 3D Touch are equipped with a tap force sensitive display, measuring the pressure on the screen. The 3D Touch technology allows users to press an app icon on the Home screen and get a quick access to some functionality presented in the app. Also, within an app, a user can get access to some features.
From iOS 9, Apple made 3D Touch APIs available:
  • Home Screen Quick Action API
  • UIKit peek and pop API
  • Web view peek and pop API
  • UITouch force properties
In order to find out whether a device supports the 3D Touch technology, you have to read out the forceTouchCapability values. While the app is working, a user can turn off 3D Touch, so this value has to be checked in the traitCollectionDidChange delegate method.
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {

NSLog(@"3D Touch is available");

} else {

NSLog(@"3D Touch is not available on this device");

}

}
Firmware Flash File
3D Touch Quick Actions
There are two types of Home Screen Quick Actions: dynamic and static.
Static actions are defined in the Info.plist file within the UIApplicationShortcutItems array.
Dynamic actions have to be added to the UIApplication application object in the shortcutItems property. You can use two methods for creation:
Method 1
init(type: String,

localizedTitle: String,

localizedSubtitle: String?,

icon: UIApplicationShortcutIcon?,

userInfo: [AnyHashable: Any]? = nil)
This method creates a Home screen dynamic quick action with a header, optional subheader, optional icon, and optional user info dictionary.
Method 2
convenience init(type: String,

localizedTitle: String)
Creates a Home screen dynamic quick action with a header but with no icon.
Quick Actions Handler
func application(application: UIApplication,
performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
let didHandle: Bool = /* handle the quick action using shortcutItem */
completionHandler(didHandle)
}
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var performAdditionalHandling = true
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey]
as? UIApplicationShortcutItem {
/* handle the quick action using shortcutItem */
performAdditionalHandling = false
}
return performAdditionalHandling
}
UIKit peek and pop API
This API is used for content preview (quick) and further transition to it. New methods in UIViewController for ViewController registration and registration cancellation allow notifications as to whether it is going to be used by 3D Touch. Additionally added are new protocols for 3D Touch support.
ViewController registration:
-(id)registerForPreviewingWithDelegate:(id)delegate sourceView:(UIView *)sourceView;
Peek:
- (UIViewController *)previewingContext: (id)previewingContext viewControllerForLocation:(CGPoint)location {
// check if we're not already displaying a preview controller
if ([self.presentedViewController isKindOfClass:[PreviewViewController class]]) {
return nil;
}
// shallow press: return the preview controller here (peek)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *previewController = [storyboard instantiateViewControllerWithIdentifier:@"PreviewView"];
return previewController;
}
Commit:
- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
// deep press: bring up the commit view controller (pop)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *commitController = [storyboard instantiateViewControllerWithIdentifier:@"CommitView"];
[self showViewController:commitController sender:self];
// alternatively, use the view controller that's being provided here (viewControllerToCommit)
}
In preview, you can also add UIPreviewAction and UIPreviewActionGroup
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"Action 1"

style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action,

UIViewController * _Nonnull previewViewController) {

NSLog(@"Action 1 triggered");

}];
// add them to an arrary

NSArray *actions = @[action1, action2, action3];
// add all actions to a group

UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group"

style:UIPreviewActionStyleDefault actions:actions];

NSArray *group = @[group1];
The True Potential of 3D Touch
As developers are getting to know the benefits of the 3D technology, it becomes clear that it will become a staple.