2016-01-21 26 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

相關問題