2015-09-28 209 views
8

我想將iOS 9的快速操作添加到我的應用程序。將「快速操作」添加到我的iOS 9應用程序

我把這個代碼在我的應用程序委託:

import UIKit 
enum ShortcutType: String { 
    case NewScan = "QuickAction.NewScan" 
    case Settings = "QuickAction.Settings" 
} 
@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    static let applicationShortcutUserInfoIconKey = "applicationShortcutUserInfoIconKey" 

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

     UIViewController.prepareInterstitialAds() 

     if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) { 
      UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)) 
     } 

     // QUICK ACTIONS 
      var launchedFromShortCut = false 

      if #available(iOS 9.0, *) { 
       if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem { 
        launchedFromShortCut = true 
        handleShortCutItem(shortcutItem) 
       } 
      } else { 
       return true 
      } 
      return !launchedFromShortCut 

    } 

    /**************** QUICK ACTIONS ****************/ 
    @available(iOS 9.0, *) 
    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) { 
      let handledShortCutItem = handleShortCutItem(shortcutItem) 
      completionHandler(handledShortCutItem) 
    } 
    @available(iOS 9.0, *) 
    func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool { 
     var handled = false 
     if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) { 
      let rootNavigationViewController = window!.rootViewController as? UINavigationController 
      let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController? 
      rootNavigationViewController?.popToRootViewControllerAnimated(false) 
      switch shortcutType { 
       case .NewScan: 

        rootViewController?.performSegueWithIdentifier("goToCamera", sender: nil) 
        handled = true 

       case.Settings: 
        rootViewController?.performSegueWithIdentifier("goToSettings", sender: nil) 
        handled = true 
      } 
     } 
     return handled 
    } 
} 

現在,我可以讓我的應用程序圖標上的力觸摸>快速操作都會顯示>我選擇快速行動「新掃描」>應用程序將會打開並向我展示最後的觀點,我已經離開了。

但是segue不會被執行。

這裏是我的故事板的一部分:

enter image description here

說明:

答:導航控制器和initiale控制器

B:視圖控制器,檢查後,這將使一個SEGUE導航控制器C

C:導航控制器

d:表視圖控制器

E:視圖控制器

如果我選擇新的掃描與快速行動 - 我想顯示的ViewController E.

回答

5

看來,你正在做正確的事情基礎在example code in the documentation。但是,您的handleShortCutItem:實現中有很多可選的鏈接。您是否使用過調試器來驗證這些表達式中是否有nil值?另外,從我所能看到的(儘管圖像模糊),該故事板中第一個導航控制器的根視圖控制器沒有對E的延續。所以我不知道你打算如何到達那裏。

我建議你在你的handleShortCutItem:實現中設置一個斷點,首先驗證你正在使用的值不是nil並且代碼正在執行。完成此操作後,您可以使用故事板來實例化所需的視圖控件,並只需要將視圖控制器層次結構放置在導航控制器中,並將導航控制器的屬性設置爲此數組,即可創建它們的一個數組。再次,這是很難說你想要什麼,從你的形象,但也許是這樣的:

func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool { 
    guard let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) else { 
     return false 
    } 

    guard let rootNavigationController = window?.rootViewController as? UINavigationController else { 
     return false 
    } 

    guard let rootViewController = rootNavigationController?.viewControllers.first else { 
     return false 
    } 

    guard let storyboard = rootNavigationController.storyboard else { 
     return false 
    } 

    var viewControllers = [rootViewController] 
    switch shortcutType { 
    case .NewScan: 
     // Instantiate the necessary view controllers for this case 
     viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some view controller#>")] 
     ... 
     viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some other view controller#>")] 

    case.Settings: 
     // Instantiate the necessary view controllers for this case 
     viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some view controller#>")] 
     ... 
     viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some other view controller#>")] 
    } 

    // Set the new view controllers array 
    rootNavigationController.setViewControllers(viewControllers, animated: false) 

    return true 
} 

注:既然你標記Swift2這個問題,我已經採取了調整代碼以使用守衛的自由聲明。

相關問題