OGeek|极客世界-中国程序员成长平台

标题: ios - 为以前版本的应用程序崩溃创建快捷方式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:20
标题: ios - 为以前版本的应用程序崩溃创建快捷方式

我在一个应用程序中创建了一个 shortcutItem,当我在 ios 9.2 中运行时它工作正常。但是当我打开具有 ios 8.1 的应用程序时,它会崩溃。

Thread 1:EXC_BAD_ACCESS (code=1, address=0x0)

如果我在 (launchOption == nil) 返回 YES 之后动态创建 shortcutItem 图标和标题,那么如何创建 shortcutItem那么电话显示 shortcutItems 吗?(因为未调用 createShortcutItem 它不应该显示我的想法。)我可以打开一次 shortcutItem即使在 didFinishLaunchingwithoptions 中没有调用 shortcutItems 和图标,我打开的应用程序也已打开并最小化。

iOS 8.1 在这一行遇到崩溃

shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

所以我返回 if launchOptions == nil 来修复崩溃。

以下是我的应用程序中用于使用快捷方式的代码。它是在我的主应用程序中创建的,因此我稍微修改了名称作为示例。

如何处理早期 iOS(8.0 起)或设备不支持的快捷方式。我希望我的应用同时支持 ios8 和更高版本。

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
  // Override point for customization after application launch.
  BOOL shouldPerformAdditionalDelegateHandling = YES;

  // Shortcut creation and methods
  [[MyAppShortcuts instance] createShortcutItem];

  if (launchOptions == nil) {
    return shouldPerformAdditionalDelegateHandling;
  }

  UIApplicationShortcutItem *shortcutItem;
  shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

  if ( shortcutItem != nil ){
    // Open app from shortcut
    self.rootViewCtrl_.shortcutItem_ = shortcutItem;

    [[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
    [[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];

    // This will block "performActionForShortcutItem:completionHandler" from being called.
    shouldPerformAdditionalDelegateHandling = NO;
  }
  return shouldPerformAdditionalDelegateHandling;
}


- (void) createShortcutItem {
  UIApplicationShortcutIcon* firstIcon = [UIApplicationShortcutIcon iconWithTemplateImageName"shortcutFirstItem"];
  UIApplicationShortcutItem* firstItem;
  firstItem = [[UIApplicationShortcutItem alloc]initWithType: firstItemType
                                                 localizedTitle: NSLocalizedString(@"First Item", nil)
                                              localizedSubtitle: nil
                                                           icon: firstIcon
                                                       userInfo: nil];
  //..... creating 2nd 3rd 4th item
  [UIApplication sharedApplication].shortcutItems = @[firstItem, secondItem, thirdItem, fourthItem];
}

application:performActionForShortcutItem: 方法在每次使用快捷方式打开应用时调用。如果我在它每次调用的方法中创建 shortcutItems(icon, title and type) ,这是否会以任何方式影响打开快捷方式,因为一次又一次地创建项目?

- (void) applicationUIApplication *)application performActionForShortcutItemUIApplicationShortcutItem *)shortcutItem completionHandlervoid (^)(BOOL))completionHandler
{
    // Shortcut creation and methods
    [ [ MyAppShortcuts instance ] createShortcutItem ];
    [[FinAppShortcuts instance] handleShortCut:shortcutItem ];     
}



Best Answer-推荐答案


您应该在检查快捷方式项目是否可用后放置所有逻辑(iOS 9.1 及更高版本)。对于没有通过点击快捷方式调用的实例,我认为 launchOptions 不是 nil。

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    BOOL shouldPerformAdditionalDelegateHandling = YES;

    //checks if you could user shortcut items. only available in iOS 9.1 onwards
    if ([UIApplicationShortcutItem class]){

        // Shortcut creation and methods
        [[MyAppShortcuts instance] createShortcutItem];

        if (launchOptions[UIApplicationLaunchOptionsShortcutItemKey]){
            // Open app from shortcut
            self.rootViewCtrl_.shortcutItem_ = shortcutItem;
            [[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
            [[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];
            // This will block "performActionForShortcutItem:completionHandler" from being called.
            shouldPerformAdditionalDelegateHandling = NO;
        }

    }
    return shouldPerformAdditionalDelegateHandling;
}

关于ios - 为以前版本的应用程序崩溃创建快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34916648/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4