1.在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中实现添加和分享的入口,代码如下 :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIApplicationShortcutIcon *firstItemIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIMutableApplicationShortcutItem *firstItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"First" localizedTitle:@"添加" localizedSubtitle:nil icon:firstItemIcon userInfo:nil];
UIApplicationShortcutIcon *secondItemIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare]; UIMutableApplicationShortcutItem *secondItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"Second" localizedTitle:@"分享" localizedSubtitle:nil icon:secondItemIcon userInfo:nil]; application.shortcutItems = @[firstItem,secondItem];
return YES;
}
2.实现这个方法
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
在这个方法中处理添加和分享的事件,代码如下:
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
if ([shortcutItem.type isEqual:@"add"])
{ NSLog(@"执行添加事件");
}else if(
[shortcutItem.type isEqual:@"share"]
){
NSLog(@"执行分享的操作 ");
}
}
二、通过模拟器进行调试 使用[SBShortcutMenuSimulator](
https://github.com/DeskConnect/SBShortcutMenuSimulator
)来配置模拟器,使模拟器支持3D Touch
在终端中按顺序输入以下命令:
1.git clone https://github.com/DeskConnect/SBShortcutMenuSimulator.git
2.cd SBShortcutMenuSimulator
3.make
然后打开刚才写好的程序 运行一下打开模拟器,再去终端中按顺序输入一下命令:
1.xcrun simctl spawn booted launchctl debug system/com.apple.SpringBoard
--environment DYLD_INSERT_LIBRARIES=$PWD/SBShortcutMenuSimulator.dylib
2.xcrun simctl spawn booted launchctl stop com.apple.SpringBoard
3.echo 'com.apple.mobilecal' | nc 127.0.0.1 8000
注意: 'com.apple.mobilecal' ''里边写的是自己项目的Bundle identifier. 这行命令就是要让模拟器显示出3D Touch,每次想要显示快速入口只要重复操作即可