OC:3D Touch

In our Wedding Planer app we recently introduced 3D Touch quick actions from the home screen. On an iPhone with 3D Touch you can now quickly reach four of the most important sections ofthe app directly from the home screen.


OC:3D Touch_第1张图片
Home Screen

In this blog post I want to show you, how we implemented this function in our controller hierarchy. As a rootview controller we use a UITabBarController and then on each tab we use a UINavigationController. With thequick actions we want to give users the possibility to quickly access the second ViewController in each tab.To accomplish this the app has to be able to switch the tab and then perform a segue to the second controller.This way we can keep the hierarchy within the app and the user can easily move back and forth in the app after using the quick action.


OC:3D Touch_第2张图片

First let’s have a look at our definition in the Info.plist file. Here we use the UIApplicationShortcutItemType key toencode the tab number and the first segue to perform. We decided not to use the UIApplicationShortcutItemUserInfo dictionary for this kind of data because this should contain user data for dynamic quick actions.
123456789

UIApplicationShortcutItems UIApplicationShortcutItemTitle Action Title UIApplicationShortcutItemType tab1:segueName

In the second step we have to implement the performActionForShortcutItem method in the UIApplicationDelegate.Since we have a fixed hierarchy in our app and use a schema for the ShortcutItemType we can create a niceimplementation that opens a specific tab and if a segue is part of the type string also performs the segue on the firstcontroller.

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { 
UITabBarController *rootController = (UITabBarController *)self.window.rootViewController; 
NSString *type = shortcutItem.type; 
NSArray *types = [type componentsSeparatedByString:@":"]; 
NSString *tabNo = [((NSString*)types[0]) substringFromIndex:3]; 
[rootController setSelectedIndex:tabNo.integerValue]; 
UINavigationController *navController = rootController.selectedViewController; 
[navController popToRootViewControllerAnimated:false]; 
if ([types count] > 1) 
{ 
NSString *segue = (NSString*)types[1];
if (segue != nil) { 
UIViewController *firstViewController = navController.viewControllers[0]; 
[firstViewController performSegueWithIdentifier:segue sender:nil]; 
} }}

With this implementation you can easily access all your second level view controller in a tab bar app from a home screenquick action.

你可能感兴趣的:(OC:3D Touch)