iOS开发-实现3DTouch按压App快捷选项shortcutItems及跳转功能

iOS开发-实现3DTouch按压App快捷选项shortcutItems及跳转功能

App的应用图标通过3D Touch按压App图标,会显示快捷选项,点击选项可快速进入到App的特定页面。

这里用到了UIApplicationShortcutItem与UIMutableApplicationShortcutItem

一、效果图

这里暂时着了一张图代替了。

二、UIApplicationShortcutItem与UIMutableApplicationShortcutItem

UIApplicationShortcutItem

objectivec 复制代码
@interface UIApplicationShortcutItem : NSObject <NSCopying, NSMutableCopying>

- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle localizedSubtitle:(nullable NSString *)localizedSubtitle icon:(nullable UIApplicationShortcutIcon *)icon userInfo:(nullable NSDictionary<NSString *, id <NSSecureCoding>> *)userInfo NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle;
...

UIMutableApplicationShortcutItem

objectivec 复制代码
@interface UIMutableApplicationShortcutItem : UIApplicationShortcutItem

// An application-specific string that identifies the type of action to perform.
@property (nonatomic, copy) NSString *type;

// Properties controlling how the item should be displayed on the home screen.
@property (nonatomic, copy) NSString *localizedTitle;
@property (nullable, nonatomic, copy) NSString *localizedSubtitle;
@property (nullable, nonatomic, copy) UIApplicationShortcutIcon *icon;

// Application-specific information needed to perform the action.
// Will throw an exception if the NSDictionary is not plist-encodable.
@property (nullable, nonatomic, copy) NSDictionary<NSString *, id <NSSecureCoding>> *userInfo;

// used with UISceneActivationConditions to customize what scene should be activated for a shortcut
@property (nullable, nonatomic, copy) id targetContentIdentifier;

@end

三、shortcutItems及跳转功能

通过配置需要当3DTouch按压显示的快捷ShortcutItem及跳转代码如下

objectivec 复制代码
#import "SD3DTouchManager.h"
#import "SDSessionManager.h"

static SD3DTouchManager *shareInstance = nil;

@implementation SD3DTouchManager

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        shareInstance = [[SD3DTouchManager alloc] init];
    });
    return shareInstance;
}

- (void)creat3DShortcutItem {
    if (IS_IOS9_OR_LATER) {
        UIApplicationShortcutIcon *sendIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"ic_bar_search"];
        
        UIApplicationShortcutItem *sendItem = [[UIApplicationShortcutItem alloc]initWithType:SD_3D_WE_SEND localizedTitle:@"找代送" localizedSubtitle:nil icon:sendIcon userInfo:nil];
        
        UIApplicationShortcutIcon *buyIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"ic_note_comment"];
        
        UIApplicationShortcutItem *buyItem = [[UIApplicationShortcutItem alloc]initWithType:SD_3D_WE_BUY localizedTitle:@"找代买" localizedSubtitle:nil icon:buyIcon userInfo:nil];
        
        UIApplicationShortcutIcon *rechargeIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"ic_note_tag"];
        
        UIApplicationShortcutItem *rechargeItem = [[UIApplicationShortcutItem alloc]initWithType:SD_3D_RECHARGE localizedTitle:@"一键充值" localizedSubtitle:nil icon:rechargeIcon userInfo:nil];
        
        UIApplicationShortcutIcon *profileIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"ic_mine"];
        
        UIApplicationShortcutItem *profileItem = [[UIApplicationShortcutItem alloc]initWithType:SD_3D_RECHARGE localizedTitle:@"我的信息" localizedSubtitle:nil icon:profileIcon userInfo:nil];
        
        [UIApplication sharedApplication].shortcutItems = @[sendItem,buyItem,rechargeItem,profileItem];
    }
}

- (BOOL)application3DShortcutOptions:(NSDictionary *)launchOptions {
    BOOL luanched = YES;
    if (IS_IOS9_OR_LATER) {
        
        UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
        
        if (shortcutItem) {
            //判断设置的快捷选项标签唯一标识,根据不同标识执行不同操作
            [self apply3DTouch:shortcutItem];
            luanched = NO;
        }
    }
    
    return luanched;
}

- (void)apply3DTouch:(UIApplicationShortcutItem *)shortcutItem {
    if ([SDSessionManager shareInstance].isLogin) {
        UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
        
        UINavigationController *navigationController;
        if ([controller isKindOfClass:[UINavigationController class]]) {
            navigationController = (UINavigationController *)controller;
        } else {
            navigationController = controller.navigationController;
        }
        
        if ([shortcutItem.type isEqualToString:SD_3D_WE_SEND]) {
            DLog(@"3D 进入代送页面");
            
        } else if ([shortcutItem.type isEqualToString:SD_3D_WE_BUY]) {
            DLog(@"3D 进入代买页面");
            
        } else if ([shortcutItem.type isEqualToString:SD_3D_RECHARGE]) {
            DLog(@"3D 进入充值页面");
            
        } else if ([shortcutItem.type isEqualToString:SD_3D_MIME_INFO]) {
            DLog(@"3D 进入我的信息页面");
            
        }
    }
}

@end

在AppDelegate的didFinishLaunchingWithOptions初始化

objectivec 复制代码
//设置3D touch
    [[SD3DTouchManager sharedInstance] creat3DShortcutItem];

实现跳转performActionForShortcutItem

objectivec 复制代码
#pragma mark - 3D Touch
//如果APP没被杀死,还存在后台,点开Touch会调用该代理方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    if (shortcutItem) {
        //判断设置的快捷选项标签唯一标识,根据不同标识执行不同操作
        [[SD3DTouchManager sharedInstance] apply3DTouch:shortcutItem];
    }
    
    if (completionHandler) {
        completionHandler(YES);
    }
}

四、小结

iOS开发-实现3DTouch按压App快捷选项shortcutItems及跳转功能

App的应用图标通过3D Touch按压App图标,会显示快捷选项,点击选项可快速进入到App的特定页面。

学习记录,每天不停进步。

相关推荐
茶底世界之下3 天前
为什么预览、录制、离线导出不能共享同一个背压策略?
ios·swift
2501_916007473 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008893 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909063 天前
移动端开发工具链怎么组合比较好,iOS、Android、跨端三套配置方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
Zldaisy3d3 天前
中南大学 | 热稳定纳米析出相助力增材制造Al-Fe-Cr-Sc-Zr合金400℃长期热暴露后室温强度保持
3d·制造
2501_916008893 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
陆康永3 天前
3d swipe 拖动 -改为linear css
3d·swiper
2501_915918414 天前
iOS编程用什么软件好?主流工具Xcode、AppCode、CodeRunner与新兴快蝎对比
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
Zldaisy3d4 天前
增材制造陶瓷的强化策略:机制与工艺-结构-性能关系的多尺度综述
3d·制造
在下胡三汉4 天前
用 glTF/glb 加载自定义 3D 模型
3d·gltf