Skip to content

Instantly share code, notes, and snippets.

@monsoir
Created December 7, 2016 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monsoir/a5285b1c86a136cc5cb5a8ae58d6baf6 to your computer and use it in GitHub Desktop.
Save monsoir/a5285b1c86a136cc5cb5a8ae58d6baf6 to your computer and use it in GitHub Desktop.
配置 3D Touch | Configure 3D Touch
//
// AppDelegate+DDDTouch.h
// Coda
//
// Created by Mon on 06/12/2016.
// Copyright © 2016 wenyongyang. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate (DDDTouch)
/**
检测系统是否支持 3D Touch
Detect whether the system supports 3D Touch.
@return YES 若系统支持 3D Touch,否则,NO | YES if the system supports 3D Touch, otherwise, NO
*/
- (BOOL)systemSupport3DTouch;
/**
检测 3D Touch 是否可用,需在 window 初始化后调用
Detect whether 3D Touch is available, should be invoked after window has been initialized.
@return YES 若 3D Touch 可用,否则,NO | YES if 3D Touch is available, otherwise, NO
*/
- (BOOL)available3DTouch;
/**
3D Touch 可用的情况下,设置 Home Screen 的 3D Touch 快捷按钮,需在 window 初始化后调用
On the condition that 3D Touch is available, configure the quick actions of home screen, should be invoked after window has been initialized.
*/
- (void)setupShortcutItemsIfNeeded;
/**
判断应用是否从 3D Touch 启动,需在 window 初始化后调用
Check whether App is started by 3D Touch, should be invoked after window has been initialized.
@param launchOptions 启动参数 | the launch options passed from application:didFinishLaunchingWithOptions:
@param launchWithoutExtraOperation 指明是否应用是否按常规启动(没有使用 3D Touch 启动),若从 3D Touch 启动,则返回 NO, 否则,返回 YES | A pointer to a BOOL value which indicates that App is started normally(not by 3D Touch), its value will be set to YES if App is started by 3D Touch, otherwise, NO
@return 激发启动的 3D Touch item 若应用从 3D Touch 启动,否则,nil | The UIApplicationShortcutItem that trigger the start of App, otherwise, nil
*/
- (UIApplicationShortcutItem *)checkLaunchFrom3DTouchIfNeededWithOptions:(NSDictionary *)launchOptions launchWithoutExtraOperation:(BOOL *)launchWithoutExtraOperation;
/**
处理 3D Touch 启动
3D Touch quick action will be implemented here.
@param shortcutItem 激发启动的 3D Touch item | The UIApplicationShortcutItem that trigger the start of App, otherwise, nil
@return YES 若处理成功,否则,NO | YES if action executes successfully, otherwise, NO
*/
- (BOOL)handleShortcutItem:(UIApplicationShortcutItem *)shortcutItem;
@end
//
// AppDelegate+DDDTouch.m
// Coda
//
// Created by Mon on 06/12/2016.
// Copyright © 2016 wenyongyang. All rights reserved.
//
#import "AppDelegate+DDDTouch.h"
@implementation AppDelegate (DDDTouch)
- (void)setupShorcutItems {
NSMutableArray *shortcuts = [NSMutableArray array];
<# Configure our quick actions #>
[UIApplication sharedApplication].shortcutItems = [shortcuts copy];
}
- (UIApplicationShortcutItem *)checkLaunchFrom3DTouchIfNeededWithOptions:(NSDictionary *)launchOptions launchWithoutExtraOperation:(BOOL *)launchWithoutExtraOperation{
UIApplicationShortcutItem *shortcutItem = nil;
*launchWithoutExtraOperation = YES;
if ([self available3DTouch]) {
shortcutItem = launchOptions[UIApplicationLaunchOptionsShortcutItemKey];
*launchWithoutExtraOperation = shortcutItem ? NO : YES;
}
return shortcutItem;
}
- (BOOL)handleShortcutItem:(UIApplicationShortcutItem *)shortcutItem {
BOOL handleResult = NO;
NSDictionary *userInfo = shortcutItem.userInfo;
NSString *targetClassName = userInfo[ApplicationShortcutItemUserInfoViewControllerKey];
if (targetClassName) {
Class aClass = NSClassFromString(targetClassName);
if (aClass) {
UIViewController *codeFunctionVC = [[aClass alloc] init];
UINavigationController *nc = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
[nc pushViewController:codeFunctionVC animated:YES];
handleResult = YES;
}
}
return handleResult;
}
- (void)setupShortcutItemsIfNeeded {
if ([self available3DTouch]) {
[self setupShorcutItems];
}
}
- (BOOL)available3DTouch {
return self.window.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
}
- (BOOL)systemSupport3DTouch {
return [[UIApplication sharedApplication] respondsToSelector:@selector(shortcutItems)];
}
// 3D Touch 的系统调用 | System will invoke this method when using 3D Touch
- (void) application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
completionHandler([self handleShortcutItem:shortcutItem]);
}
@end
#import "AppDelegate.h"
#import "AppDelegate+DDDTouch.h"
@interface AppDelegate ()
@property (nonatomic, strong) UIApplicationShortcutItem *shortcutItem;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
<# Other stuff #>
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = nc;
// 配置 3D Touch
BOOL launchWithoutExtraOperation = YES;
if ([self systemSupport3DTouch]) {
self.shortcutItem = [self checkLaunchFrom3DTouchIfNeededWithOptions:launchOptions launchWithoutExtraOperation:&launchWithoutExtraOperation];
[self setupShortcutItemsIfNeeded];
}
[self.window makeKeyAndVisible];
return launchWithoutExtraOperation;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if (self.shortcutItem) {
[self handleShortcutItem:self.shortcutItem];
self.shortcutItem = nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment