Ios Obj-C中的3D触摸主页快捷方式
我的所有应用程序目前都是用Obj-C编写的。使用3D Touch实现主屏幕快捷方式示例代码的链接完全用Swift编译。任何人都会看到Obj-C的文档,所以我不必通过我的AppDelegate进行翻译 更新: 在Info.plist中添加所有快捷方式后,我添加了AppDelegate.m:Ios Obj-C中的3D触摸主页快捷方式,ios,objective-c,swift,ios9,3dtouch,Ios,Objective C,Swift,Ios9,3dtouch,我的所有应用程序目前都是用Obj-C编写的。使用3D Touch实现主屏幕快捷方式示例代码的链接完全用Swift编译。任何人都会看到Obj-C的文档,所以我不必通过我的AppDelegate进行翻译 更新: 在Info.plist中添加所有快捷方式后,我添加了AppDelegate.m: - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NSLog(@"%@", shortcutItem.type);
if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addPrayerRequest"]) {
Requests *gonow = [[Requests alloc] init];
[nav pushViewController:gonow animated:YES];
}
if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addPrayer"]) {
PrayerStats *controller = [[PrayerStats alloc] init];
[nav pushViewController:controller animated:YES];
}
if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addFast"]) {
FastStats *controller1 = [[FastStats alloc] init];
[nav pushViewController:controller1 animated:YES];
}
if ([shortcutItem.type isEqualToString:@"com.316apps.iPrayed.addStudy"]) {
StudyStats *controller2 = [[StudyStats alloc] init];
[nav pushViewController:controller2 animated:YES];
}
}
这使得它可以工作,而无需使用任何其他方法,也无需向didFinishLaunchingWithOptions添加任何内容。有两种状态,用户可以通过快速操作打开应用程序 TL;DR 无论快速操作完成时应用程序处于何种状态,您总是在做相同的事情,这就是为什么您只需要覆盖
application:performationforshortcutitem:completionHandler:
,因此如果您想做不同的事情,那么您需要在两个位置处理它们,如果不是这样,那么只需要重写就足够了
- 一个是如果应用程序被终止或没有在后台运行,我们在启动时会在后台获得快捷方式信息
- 另一种情况是,如果应用程序正在后台运行,我们可以从中获取新应用程序委托方法的快捷方式信息
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
以及没有在后台运行(杀死)在您的
或
您应该检查应用程序是否通过快速操作启动:
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];
引用苹果官方文件
您有责任确保系统调用此方法
有条件地,取决于是否有一个应用程序启动
方法(应用程序:willFinishLaunchingWithOptions:或
应用程序:didFinishLaunchingWithOptions:)已处理
快速操作调用。系统调用启动方法(在
当用户选择应用程序的快速操作时调用此方法)
您的应用程序将启动而不是激活
请求的快速操作可能使用不同于的代码路径
当你的应用程序启动时使用的那些。例如,说你的应用程序
通常启动以显示视图A,但您的应用程序是在中启动的
对需要视图B的快速行动的响应。为处理此类情况,
启动时,检查您的应用程序是否通过快捷方式启动
行动。在您的计算机中执行此检查
应用程序:将使用选项完成启动:或
application:didFinishLaunchingWithOptions:method通过检查
UIApplicationLaunchProctionsShortcutItemKey启动选项键。这个
UIApplicationShortcutItem对象作为
启动选项键
如果您发现您的应用程序确实是通过快速操作启动的,
在启动方法中执行请求的快速操作并返回
该方法的值为NO。如果返回的值为“否”,则
系统不调用
应用程序:PerformationForShortcutItem:completionHandler:method。
如果您查看为apple提供的示例代码,您会发现他们建议您编写一个处理快捷方式项的方法,以便您可以在以下三个位置处理它:
应用程序:短期项目的性能
应用程序:使用选项完成启动
将使用选项完成启动
- (BOOL)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem {
BOOL handled = NO;
if (shortcutItem == nil) {
return handled;
}
if ([shortcutItem.type isEqualToString:kFavoritesQuickAction]) {
handled = YES;
}
if (handled) {
// do action here
}
return handled;
}
然后,您可以在获取快捷方式项的任何位置调用此方法。这将有助于你前进 我制作了一个objective-c演示项目,用于主屏幕快速操作 演示项目在启动应用程序之前,在不使用Info.plist文件的情况下实现静态快速操作,以避免出现不必要的情况。 您可以轻松地将其更改为动态快速动作
正如apple文档中提到的,您可以在application:didfishlaunchingwithoptions:method中处理快速操作。在这种情况下,您应该返回NO to block来调用application:performationforshortcutitem:completionHandler:method。实现以下3个简单步骤: 步骤1:在
AppDelegate
类中编写下面的方法,以配置动态快捷方式项
注意:如果希望在info.plist中配置快捷方式项目,可以将其设置为静态。
(请参阅)
第2步:在AppDelegate
类中,应用程序使用选项完成启动方法写入下面的代码
// UIApplicationShortcutItem is available in iOS 9 or later.
if([[UIApplicationShortcutItem class] respondsToSelector:@selector(new)]){
[self configDynamicShortcutItems];
// If a shortcut was launched, display its information and take the appropriate action
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem)
{
// When the app launch at first time, this block can not called.
//App launch process with quick actions
[self handleShortCutItem:shortcutItem];
}else{
// normal app launch process without quick action
}
}
步骤3:在AppDelegate
类中的委托方法和完成处理程序下面写入
/*
Called when the user activates your application by selecting a shortcut on the home screen, except when
application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) returns `false`.
You should handle the shortcut in those callbacks and return `false` if possible. In that case, this
callback is used if your application is already launched in the background.
*/
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem];
completionHandler(handledShortCutItem);
}
/**
* @brief handle shortcut item depend on its type
*
* @param shortcutItem shortcutItem selected shortcut item with quick action.
*
* @return return BOOL description
*/
- (BOOL)handleShortCutItem : (UIApplicationShortcutItem *)shortcutItem{
BOOL handled = NO;
NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
NSString *shortcutSearch = [NSString stringWithFormat:@"%@.Search", bundleId];
NSString *shortcutFavorite = [NSString stringWithFormat:@"%@.Favorite", bundleId];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if ([shortcutItem.type isEqualToString:shortcutSearch]) {
handled = YES;
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"secondVC"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
else if ([shortcutItem.type isEqualToString:shortcutFavorite]) {
handled = YES;
ThirdViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"thirdVC"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
return handled;
}
它同时适用于swift 3和4(仅适用于主屏幕快捷方式)
//如图所示添加plist项,并在Appdelegate中编写以下方法
//主屏幕上的3D触摸法快捷方式
func应用程序(application:UIApplication,performActionFor shortcutItem:UIApplicationShortcutItem,completionHandler:@escaping(Bool)->Void){
如果shortcutItem.type==“共享”{
//处理操作共享
let alert=UIAlertController(标题:“3D触摸共享”,信息:"Yahoo!!!3D touch正在工作,我遗漏了什么?当我在didFinishLaunching中输入最后一行代码时,我得到了未使用的项目shortcutItem。我需要将它放在其他地方吗?好的,你在设置它之后是否使用shortcutItem?@Keller不,因为我没有写这个答案,其他人写了,只是好奇为什么它在那里。只是添加了t他执行ShortcutItem代码使我能够执行我在信息中添加的快捷方式,而且由于Nicolas的答案中没有其他快捷方式,他很好奇为什么需要它。@user717452查看更新后的答案时引用了apple docs,如果您不想处理不同的流,则不必执行。@user717452您总是这样做的。@user717452同样的事情,不管快速动作完成时应用程序处于何种状态。因此,如果你想做不同的事情,那么你会
/**
* @brief config dynamic shortcutItems
* @discussion after first launch, users can see dynamic shortcutItems
*/
- (void)configDynamicShortcutItems {
// config image shortcut items
// if you want to use custom image in app bundles, use iconWithTemplateImageName method
UIApplicationShortcutIcon *shortcutSearchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
UIApplicationShortcutIcon *shortcutFavoriteIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite];
UIApplicationShortcutItem *shortcutSearch = [[UIApplicationShortcutItem alloc]
initWithType:@"com.sarangbang.QuickAction.Search"
localizedTitle:@"Search"
localizedSubtitle:nil
icon:shortcutSearchIcon
userInfo:nil];
UIApplicationShortcutItem *shortcutFavorite = [[UIApplicationShortcutItem alloc]
initWithType:@"com.sarangbang.QuickAction.Favorite"
localizedTitle:@"Favorite"
localizedSubtitle:nil
icon:shortcutFavoriteIcon
userInfo:nil];
// add all items to an array
NSArray *items = @[shortcutSearch, shortcutFavorite];
// add the array to our app
[UIApplication sharedApplication].shortcutItems = items;
}
// UIApplicationShortcutItem is available in iOS 9 or later.
if([[UIApplicationShortcutItem class] respondsToSelector:@selector(new)]){
[self configDynamicShortcutItems];
// If a shortcut was launched, display its information and take the appropriate action
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem)
{
// When the app launch at first time, this block can not called.
//App launch process with quick actions
[self handleShortCutItem:shortcutItem];
}else{
// normal app launch process without quick action
}
}
/*
Called when the user activates your application by selecting a shortcut on the home screen, except when
application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) returns `false`.
You should handle the shortcut in those callbacks and return `false` if possible. In that case, this
callback is used if your application is already launched in the background.
*/
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem];
completionHandler(handledShortCutItem);
}
/**
* @brief handle shortcut item depend on its type
*
* @param shortcutItem shortcutItem selected shortcut item with quick action.
*
* @return return BOOL description
*/
- (BOOL)handleShortCutItem : (UIApplicationShortcutItem *)shortcutItem{
BOOL handled = NO;
NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
NSString *shortcutSearch = [NSString stringWithFormat:@"%@.Search", bundleId];
NSString *shortcutFavorite = [NSString stringWithFormat:@"%@.Favorite", bundleId];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if ([shortcutItem.type isEqualToString:shortcutSearch]) {
handled = YES;
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"secondVC"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
else if ([shortcutItem.type isEqualToString:shortcutFavorite]) {
handled = YES;
ThirdViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"thirdVC"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
return handled;
}