2015-09-28 8 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 
    } 
} 

を今、私は私のアプリのアイコンに力touchを作ることができる>迅速な行動が、私はクイックアクション「新規スキャン」を選択>表示されます>アプリ私が残した最後のビューを開いて表示します。

しかし、セグは実行されません。ここで

は私の絵コンテの一部です:

enter image description here

説明:

A:ナビゲーションコントローラとイニシャルコントローラー

B:ViewControllerを、チェックした後、これはセグエを行いますナビゲーションコントローラC

C:ナビゲーションコントローラ

D:表ビューコントローラ

E:のViewController私は迅速なアクションを持つ新しいスキャン]を選択した場合

- 私はあなたが正しくベースの事をやっていることが表示されます

答えて

5

のViewController E.を表示したいと思いますexample code in the documentationにあります。ただし、handleShortCutItem:の実装では、多くのオプションのチェーンがあります。デバッガを使って、これらの式のどれもがnil値でないことを確認しましたか?また、私は見ることができますが(画像はぼやけていますが)、そのストーリーボードの最初のnavコントローラのルートビューコントローラにはEのセグがありません。

handleShortCutItem:実装では、作業中の値がnilではなく、コードが実際に実行されていることを最初に検証するようにブレークポイントを設定することをお勧めします。これを実行したら、ストーリーボードを使用して、必要なビューコントロールをインスタンス化し、ビューコントローラー階層をナビゲーションコントローラー内に配置し、ナビゲーションコントローラーのviewControllersプロパティをこの配列に設定するために配列を作成するだけです。ここでも、それはあなたがあなたのイメージからしたい正確に何を伝えるのは難しいですが、おそらくこのような何か:

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でこの質問をタグ付けされたので、私はガードを使用するようにコードを調整する自由を撮影しましたステートメント。

関連する問題