一、3D Touch 的实现:
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来配置模拟器,使模拟器支持3D Touch
- 在终端中按顺序输入以下命令:
1.git clone https://github.com/DeskConnect/SBShortcutMenuSimulator.git
2.cd SBShortcutMenuSimulator
3.make
Zero-zhou:~ lanou3g$ cd /Users/lanou3g/Desktop/SBShortcutMenuSimulator
Zero-zhou:SBShortcutMenuSimulator lanou3g$ make
//若u出现error,执行 sudo xcode-select --switch /Applications/应用程序名.app/Contents/Developer
xcrun: error: active developer path ("/Applications/Xcode 2.app/Contents/Developer") does not exist, use `xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools (or see `man xcode-select`)
Zero-zhou:SBShortcutMenuSimulator lanou3g$ sudo xcode-select --switch /Applications/Xcode 2.app/Contents/Developer
Password:
xcode-select: error: invalid argument '2.app/Contents/Developer'
Usage: xcode-select [options]
Print or change the path to the active developer directory. This directory
controls which tools are used for the Xcode command line tools (for example,
xcodebuild) as well as the BSD development commands (such as cc and make).
Options:
-h, --help print this help message and exit
-p, --print-path print the path of the active developer directory
-s <path>, --switch <path> set the path for the active developer directory
--install open a dialog for installation of the command line developer tools
-v, --version print the xcode-select version
-r, --reset reset to the default command line tools path
Zero-zhou:SBShortcutMenuSimulator lanou3g$ sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
Zero-zhou:SBShortcutMenuSimulator lanou3g$ cd /Users/lanou3g/Desktop/SBShortcutMenuSimulator
Zero-zhou:SBShortcutMenuSimulator lanou3g$ make
xcrun --sdk iphonesimulator clang -Os -miphoneos-version-min=9.0 -isysroot "`xcrun --sdk iphonesimulator --show-sdk-path`" -arch i386 -arch x86_64 -fobjc-arc -fmodules -c -o SBShortcutMenuListener.o SBShortcutMenuListener.m
xcrun --sdk iphonesimulator clang -Os -miphoneos-version-min=9.0 -isysroot "`xcrun --sdk iphonesimulator --show-sdk-path`" -arch i386 -arch x86_64 -fobjc-arc -fmodules -dynamiclib -o SBShortcutMenuSimulator.dylib SBShortcutMenuListener.o
- 然后打开刚才写好的程序 运行一下打开模拟器,再去终端中按顺序输入一下命令:
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,每次想要显示快速入口只要重复操作即可