3D Touch集成过程整理
1、集成App图标按压快速打开某个功能
在AppDelegate.m中加入以下三个东西
在启动方法里加入3D Touch菜单
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ...... //3D Touch iOS9以上才支持 if([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0){ //创建3D Touch菜单 [self createItem]; //启动的时候判断是不是点击3D Touch菜单进来的 UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey]; if (shortcutItem) { [SaveData setValueToSettingWithName:@"ShortcutItemType" value:shortcutItem.type]; //NSLog(@"We've launched from shortcut item: %@", shortcutItem.localizedTitle); } else { //NSLog(@"We've launched properly."); } } return YES; }
#pragma mark - 创建3D Touch菜单 -(void)createItem{ //给App图标添加3D Touch菜单 //签到 //菜单图标 UIApplicationShortcutIcon *iconSignin = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dtouch_signin"]; //菜单文字 UIMutableApplicationShortcutItem *itemSignin = [[UIMutableApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"签到"]; //绑定信息到指定菜单 itemSignin.icon = iconSignin; //记体重 //菜单图标 UIApplicationShortcutIcon *iconWeight = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dtouch_weight"]; //菜单文字 UIMutableApplicationShortcutItem *itemWeight = [[UIMutableApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"记体重"]; //绑定信息到指定菜单 itemWeight.icon = iconWeight; //记录饮食运动 //菜单图标 UIApplicationShortcutIcon *iconFood = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dtouch_food"]; //菜单文字 UIMutableApplicationShortcutItem *itemFood = [[UIMutableApplicationShortcutItem alloc] initWithType:@"3" localizedTitle:@"记录饮食运动"]; //绑定信息到指定菜单 itemFood.icon = iconFood; //发动态 //菜单图标 UIApplicationShortcutIcon *iconWeibo = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dtouch_weibo"]; //菜单文字 UIMutableApplicationShortcutItem *itemWeibo = [[UIMutableApplicationShortcutItem alloc] initWithType:@"4" localizedTitle:@"发动态"]; //绑定信息到指定菜单 itemWeibo.icon = iconWeibo; //绑定到App icon NSArray *items = [NSArray arrayWithObjects:itemWeibo, itemFood, itemWeight, itemSignin, nil]; [UIApplication sharedApplication].shortcutItems = [NSArray arrayWithArray:items]; }
#pragma mark - 桌面图标3DTouch按压后菜单的事件 - (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler{ if ([SaveData getValueFromSettingWithName:LOGIN_TICKET]) { if ([self.window.rootViewController isKindOfClass:NSClassFromString(@"RootTabBarController")]) { //NSLog(@"有TabBar"); RootTabBarController *tabBar = (RootTabBarController *)self.window.rootViewController; MLNavigationController *nav = (MLNavigationController *)tabBar.selectedViewController; //签到 if ([shortcutItem.type isEqualToString:@"1"]) { DFPointsMallViewController *newView = [[DFPointsMallViewController alloc]init]; newView.title = @"积分商城"; newView.hidesBottomBarWhenPushed = YES; [nav pushViewController:newView animated:NO]; } //记体重 if ([shortcutItem.type isEqualToString:@"2"]) { RenwuRecordWeightViewController *newView = [[RenwuRecordWeightViewController alloc]init]; newView.title = @"记录体重"; newView.hidesBottomBarWhenPushed = YES; [nav pushViewController:newView animated:NO]; } //记录饮食运动 if ([shortcutItem.type isEqualToString:@"3"]) { CalorieCalculatorViewController *newView = [[CalorieCalculatorViewController alloc]init]; newView.title = @"记录饮食运动"; newView.hidesBottomBarWhenPushed = YES; [nav pushViewController:newView animated:NO]; } //发动态 if ([shortcutItem.type isEqualToString:@"4"]) { QuanziPubViewController *newView = [[QuanziPubViewController alloc]init]; newView.title = @"发动态"; MLNavigationController *mlNav = [[MLNavigationController alloc]initWithRootViewController:newView]; [nav presentViewController:mlNav animated:YES completion:nil]; } } } }
注意:点击应用图标的快速入口进入app时,如果app在后台运行,则会调用后面的回调方法。如果是新打开app,参数则会传入到启动方法的launchOptions里,就和通知类似。
我这里如果点击是新打开app的话,我是先把数据先记录到本地,等进入到首页后再进行处理,处理好后再销毁记录在本地的数据。
方法如下:
//快速进入 if ([SaveData getValueFromSettingWithName:@"ShortcutItemType"]) { NSString *shortcutItemType = [SaveData getValueFromSettingWithName:@"ShortcutItemType"]; //签到 if ([shortcutItemType isEqualToString:@"1"]) { DFPointsMallViewController *newView = [[DFPointsMallViewController alloc]init]; newView.title = @"积分商城"; newView.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:newView animated:NO]; } //记体重 if ([shortcutItemType isEqualToString:@"2"]) { RenwuRecordWeightViewController *newView = [[RenwuRecordWeightViewController alloc]init]; newView.title = @"记录体重"; newView.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:newView animated:NO]; } //记录饮食运动 if ([shortcutItemType isEqualToString:@"3"]) { CalorieCalculatorViewController *newView = [[CalorieCalculatorViewController alloc]init]; newView.title = @"记录饮食运动"; newView.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:newView animated:NO]; } //发动态 if ([shortcutItemType isEqualToString:@"4"]) { QuanziPubViewController *newView = [[QuanziPubViewController alloc]init]; newView.title = @"发动态"; MLNavigationController *mlNav = [[MLNavigationController alloc]initWithRootViewController:newView]; [self.navigationController presentViewController:mlNav animated:YES completion:nil]; } [SaveData removeValueFromSettingWithName:@"ShortcutItemType"]; }
下面这个是把数据记录到本地的方法,写下吧以免时间长忘记了
+(id)getValueFromSettingWithName:(NSString *)name{ //NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults *defaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.fitmissSharedDefaults"]; id value = [defaults objectForKey:name]; return value; } +(void)setValueToSettingWithName:(NSString *)name value:(id)value{ //NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults *defaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.fitmissSharedDefaults"]; [defaults setObject:value forKey:name]; [defaults synchronize]; } +(void)removeValueFromSettingWithName:(NSString *)name{ //NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults *defaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.fitmissSharedDefaults"]; [defaults removeObjectForKey:name]; [defaults synchronize]; }
group.fitmissSharedDefaults是在开发者中心里开启的分组,不用这个,用引掉的那个也行的。