美文网首页
iOS App 快捷选项 shortcutItems

iOS App 快捷选项 shortcutItems

作者: codingchou | 来源:发表于2019-11-13 10:35 被阅读0次
  • 通过3D Touch按压App图标,会显示快捷选项,点击选项可快速进入到App的特定页面,提升了App的体验。
  • 这是通过 UIApplicationShortcutItem 实现的,直接上代码。
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self creatShortcutItem];
    
    if(launchOptions){
        if([[[UIDevice currentDevice] systemVersion] doubleValue]>=9.0){
            UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
            //如果是从快捷选项标签启动app,则根据不同标识执行不同操作,然后返回NO,防止调用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
            if (shortcutItem) {
                NSLog(@"----- %@ ------ = %s",shortcutItem.type,__FUNCTION__);
                //判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作
                [self dealWithShortcut:shortcutItem.type];
                return NO;
            }
        }
    }
    
    return YES;
}

#pragma mark - 创建 shortcutItem

- (void)creatShortcutItem {
    //创建系统风格的icon
    //    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    
    // 自定义icon,大小为 70*70 px
    UIApplicationShortcutIcon *payIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_pay.png"];
    UIApplicationShortcutItem *payItem = [[UIApplicationShortcutItem alloc]initWithType:@"com.codingchou.test.pay" localizedTitle:@"支付" localizedSubtitle:nil icon:payIcon userInfo:nil];
    
    UIApplicationShortcutIcon *scanIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_scan.png"];
    UIApplicationShortcutItem *scanItem = [[UIApplicationShortcutItem alloc]initWithType:@"com.codingchou.test.scan" localizedTitle:@"扫一扫" localizedSubtitle:nil icon:scanIcon userInfo:nil];
    
    UIApplicationShortcutIcon *qrcodeIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_qrcode.png"];
    UIApplicationShortcutItem *qrcodeItem = [[UIApplicationShortcutItem alloc]initWithType:@"com.codingchou.test.qrcode" localizedTitle:@"付款" localizedSubtitle:nil icon:qrcodeIcon userInfo:nil];
    
    UIApplicationShortcutIcon *redpackIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_redpacket.png"];
    UIApplicationShortcutItem *redpackItem = [[UIApplicationShortcutItem alloc]initWithType:@"com.codingchou.test.redpacket" localizedTitle:@"红包" localizedSubtitle:nil icon:redpackIcon userInfo:nil];
    
    //添加到快捷选项数组
    [UIApplication sharedApplication].shortcutItems = @[payItem,scanItem,qrcodeItem,redpackItem];
}

- (void)dealWithShortcut:(NSString*)type {
    NSLog(@"dealWithShortcut = %@",type);
    
    if([type isEqualToString:@"com.codingchou.test.pay"]){
        NSLog(@"快捷方式 -- 支付");
        
    } else if([type isEqualToString:@"com.codingchou.test.scan"]){
        NSLog(@"快捷方式 -- 扫一扫");
        
    } else if([type isEqualToString:@"com.codingchou.test.qrcode"]){
        NSLog(@"快捷方式 -- 付款");
        
    } else if([type isEqualToString:@"com.codingchou.test.redpacket"]){
        NSLog(@"快捷方式 -- 红包");
        
    }
}

#pragma mark - 当app已启动, 点击shortcutItem回调
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    NSLog(@"快捷方式 : %@",shortcutItem.type);
    
    [self dealWithShortcut:shortcutItem.type];
    
    completionHandler(YES);
}

  • 效果图


    快捷选项

相关文章

网友评论

      本文标题:iOS App 快捷选项 shortcutItems

      本文链接:https://www.haomeiwen.com/subject/cmocictx.html