header

3DTouch-ShortcutItem - iOS9 - xcode7

据说苹果某个神秘的团队闭门潜心研发多年的3DTouch终于,应用在iOS9上,且公开了API。

4CA7E65D1DB563D301F37DFF391C2612副本

在系统主界面用力按压 APP 图标,如上会出现自定义菜单

有两种方法可以实现
一.代码(这种方法也是可以动态的改变 3DTouch菜单的内容与功能):
第一步:自定义一个初始化菜单的方法;

-(void)init3DTouch{
    if ([[UIDevice currentDevice] systemVersion].floatValue < 9.0) {
        return;
    }
    UIApplication *app = [UIApplication sharedApplication];
    NSMutableArray *its = [[app shortcutItems] mutableCopy];
    if (its.count == 0) {
        NSMutableArray *items = [[NSMutableArray alloc] init];
        UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
        UIApplicationShortcutItem *i2 = [[UIApplicationShortcutItem alloc] initWithType:@"com.xiaomi.i2" localizedTitle:@"发起群聊" localizedSubtitle:@"" icon:icon2 userInfo:nil];
        [items addObject:i2];
        UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
        UIApplicationShortcutItem *i1 = [[UIApplicationShortcutItem alloc] initWithType:@"com.xiaomi.i1" localizedTitle:@"电话会议" localizedSubtitle:@"" icon:icon userInfo:nil];
        [items addObject:i1];
        [app setShortcutItems:items];
    }
}

 

第二步:实现 UIApplication 协议来响应系统主页按压APP出现菜单的,菜单对应点击事件;

-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    if ([shortcutItem.type isEqualToString:@"com.xiaomi.i1"]) {
        
        // 点击菜单一 ‘发起群聊’ 执行的内容
    }
    else if ([shortcutItem.type isEqualToString:@"com.xiaomi.i2"]){
        
        // 点击菜单二 ‘电话会议’ 执行的内容
    }
}
// Called when the user activates your application by selecting a shortcut on the home screen,
// except when -application:willFinishLaunchingWithOptions: or -application:didFinishLaunchingWithOptions returns NO.

 

第三步:判断 launchOptions != nil 则返回NO;顺便获取激活APP的对象,手动执行所点击的菜单事件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self init3DTouch];
    // 当APP 不再后台运行时 通过3DTouch 激活APP,launchOptions不为空手动调用 performActionForShortcutItem
    if ( launchOptions != nil ) {
        UIApplicationShortcutItem *i = [launchOptions objectForKey: UIApplicationLaunchOptionsShortcutItemKey];
        [self application:application performActionForShortcutItem:i completionHandler:^(BOOL succeeded) {
            NSLog(@"launchOptions no null");
        }];
        return NO;
    }
    return YES;
}

 

二.info.plist 

 

 

=============================

参考:http://www.jianshu.com/p/74fe6cbc542b

posted @ 2015-10-16 15:34  loganv  阅读(977)  评论(0编辑  收藏  举报
footer