2015-09-26 106 views
14

我正在爲我的iOS 9應用在swift中實施一些3D觸摸快速操作,並且我有一個奇怪的問題。當我的應用程序處於後臺並且我使用快速操作啓動時,一切都按計劃進行。當我的應用程序完全死亡(即我從多任務菜單中殺死它),並且我以快速操作啓動時,應用程序崩潰。我在調試時遇到了麻煩,因爲一旦我終止了應用程序,Xcode中的調試會話就會被分離。有沒有辦法讓我連接到應用程序進行調試像正常,或者是否有我的代碼中會導致它的東西?提前致謝。使用UIApplicationShortcutItem啓動

代碼:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    var launchedFromShortCut = false 

    //Check for ShortCutItem 
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem 
    { 
     launchedFromShortCut = true 
     self.handleShortCutItem(shortcutItem) 
    } 

    return !launchedFromShortCut 
} 

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) 
{ 
    self.handleShortCutItem(shortcutItem) 
} 

func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) 
{ 
    //Get type string from shortcutItem 
    if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) 
    { 
     //Get root navigation viewcontroller and its first controller 
     let rootNavigationViewController = window!.rootViewController as? UINavigationController 


     if let rootViewController = rootNavigationViewController?.viewControllers.first as! LaunchViewController? 
     { 
      //Pop to root view controller so that approperiete segue can be performed 
      rootNavigationViewController?.popToRootViewControllerAnimated(false) 

      switch shortcutType 
      { 
       case .Compose: 
        rootViewController.shouldCompose() 
        break 
      } 
     } 
    } 
} 

謝謝!

+0

你可以在死後看應用程序的崩潰日誌文件... –

+0

你得到這個排序,我有同樣的問題? – theiOSDude

+0

@theiOSDude不,我沒有。 –

回答

15

我終於得到了這個工作。這是我的AppDelegate.swift文件結束爲;

class AppDelegate: UIResponder, UIApplicationDelegate { 

// Properties 
var window: UIWindow? 
var launchedShortcutItem: UIApplicationShortcutItem? 

func applicationDidBecomeActive(application: UIApplication) { 

    guard let shortcut = launchedShortcutItem else { return } 

    handleShortcut(shortcut) 

    launchedShortcutItem = nil 

} 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    // Override point for customization after application launch. 
    var shouldPerformAdditionalDelegateHandling = true 

    // If a shortcut was launched, display its information and take the appropriate action 
    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem { 

     launchedShortcutItem = shortcutItem 

     // This will block "performActionForShortcutItem:completionHandler" from being called. 
     shouldPerformAdditionalDelegateHandling = false 

    } 

    return shouldPerformAdditionalDelegateHandling 
} 


func handleShortcut(shortcutItem:UIApplicationShortcutItem) -> Bool { 

    // Construct an alert using the details of the shortcut used to open the application. 
    let alertController = UIAlertController(title: "Shortcut Handled", message: "\"\(shortcutItem.localizedTitle)\"", preferredStyle: .Alert) 
    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
    alertController.addAction(okAction) 

    // Display an alert indicating the shortcut selected from the home screen. 
    window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

    return handled 

} 

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) { 

    completionHandler(handleShortcut(shortcutItem)) 

} 

這在很大程度上是由蘋果公司sample code爲UIApplicationShortcuts拍攝,雖然我有我的應用程序啓動警報,證明它是認識選擇適當的快捷方式,這樣可以適應您的代碼以彈出視圖控制器。

我認爲func applicationDidBecomeActive是我失蹤的關鍵部分,並且從didFinishLaunchingWithOptions中刪除了self.handleShortCut(shortcutItem)(否則它似乎是兩次調用handleShortCut)。

+0

這真棒很快就會測試。 –

+0

你回報的「已處理」價值是什麼? –

37
  1. 在Xcode中,開放式產品 - >方案 - >編輯方案
  2. 運行中的計劃,改啓動設置爲「等待執行上馬」現在

,如果打開在調試並運行您的應用程序時,Xcode將等待您從主屏幕啓動您的應用程序,以便您可以使用3D Touch Shortcut Item啓動它。

See Screenshot in Xcode of the Setting

+1

非常感謝!這使我能夠從終止狀態測試快速行動。 +1 –

+1

這不會打印控制檯中的日誌? – genaks

+2

如何將內容打印到控制檯? – iOShepherd

2

我嘗試了所有的上面,而且比我試圖在處理方法handleShortcut延遲後的快捷方式並沒有解決問題 :

self.performSelector("action1", withObject: self, afterDelay: 0.5) 

,並添加一個方法的每一個動作,它的工作就像魅力

0

用這個替換你的didfinishlaunching方法。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { 

    if let shortcutItem = 
     launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] 
     as? UIApplicationShortcutItem { 

    handleShortcut(shortcutItem) 
    return false 
    } 
    return true 
}