2016-01-21 64 views
2

我已经在应用程序中创建了shortcutItem,并且当我在ios 9.2中运行时它工作正常。但是当我打开具有ios 8.1的应用程序时,它会崩溃。为以前版本的应用程序崩溃创建快捷方式

主题1:EXC_BAD_ACCESS(代码= 1,地址=为0x0)

是如何shortcutItem创建说,如果我创建(launchOption == nil) return YES后动态shortcutItem图标和标题是否手机显示shortcutItems然后(?因为createShortcutItem不叫它不应该显示是我的想法。)一旦我打开应用程序打开应用程序打开并尽可能最小化,即使shortcutItems和图标没有在didFinishLaunchingwithoptions中调用,我将能够打开shortcutItem

日上午在这一行

shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey]; 

,所以我回到if launchOptions == nil修复毁人在IOS 8.1崩溃。

以下是我在使用快捷方式的应用程序中使用的代码。它是在我的主应用程序中创建的,所以我稍微修改了一些名称。

如何处理早期iOS(从8.0)或设备不支持的快捷方式。我希望我的应用程序支持ios8和更高版本。应用程序被使用快捷打开时

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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) application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler 
{ 
    // Shortcut creation and methods 
    [ [ MyAppShortcuts instance ] createShortcutItem ]; 
    [[FinAppShortcuts instance] handleShortCut:shortcutItem ];  
} 
+0

崩溃时的消息究竟是什么?你为什么要调用'objectForKeyedSubscript:'而不是普通的'objectForKey:'? –

+0

崩溃是“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)”以红色突出 – Jaff

回答

2

在检查它是否可用(iOS 9.1以上)之后,您应该放置有关快捷方式的所有逻辑。我不认为launchOptions是没有零的情况下,它不是通过点击快捷方式调用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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; 
} 
+0

感谢您的答复。一个问题,我可以看到第一次安装应用程序的快捷方式,并从快速启动直接打开? – Jaff

+0

是的,我不认为你需要'createShortcutItem'方法.. 还代替使用'[MyAppShortcuts实例]'你可以使用'self'而不是 –

+0

是的我创建了[MyAppShortcuts实例]来减少堆栈中的代码AppDelegate中。这只是一个拥有所有捷径方法的课程。它只是有一些方法创建快捷方式和handleShortcuts – Jaff