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是在开发者中心里开启的分组,不用这个,用引掉的那个也行的。

posted @   lear  阅读(400)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2013-02-19 添加分页和下拉刷新方法时需要增加的frameworks
点击右上角即可分享
微信分享提示