2017-03-08 23 views
2

私はこの問題で同様のケースを持っていますiOS: apple universal link if app is not open?。ユニバーサルリンクをクリックすると、アプリがバックグラウンドでない場合は、func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {}に入ることができませんでした。アプリがバックグラウンドにないときに開いているユニバーサルリンク

didFinishLaunchingWithOptionsにいくつかのコードを追加しました。しかし、それは動作していません。もしも誰かが助けてくれたらありがとう。アプリデリゲートで

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    let activityDic = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] 
    if activityDic != nil { 
     // Continue activity here 
     self.window?.rootViewController?.restoreUserActivityState(activityDic as! NSUserActivity) 
    } 


    return true 
} 

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { 
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {   
      if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "xxx") as? XXXTableViewController { 
       if let window = self.window, let rootViewController = window.rootViewController { 
        var currentController = rootViewController 
        while let presentedController = currentController.presentedViewController { 
         currentController = presentedController 
        } 
        currentController.present(controller, animated: true, completion: nil) 
       } 
      } 
    } 

    return true 

} 

答えて

1

AppDelegate didFinishLaunchingWithOptionsメソッドで使用このコードを

//catch if open app from url 
    if let options = launchOptions{ 
     for key in options.keys { 
      if(key == UIApplicationLaunchOptionsKey.url){ 
       if let url = options[key] as? URL{ 
        AppDelegate.handleOpenURLWhenAppIsNotOpen(url) 
       } 
      } 
     } 
    } 

私は普遍的なリンクを処理するためBranch.ioを使用している方法

private class func handleOpenURLWhenAppIsNotOpen(_ url: URL) { 
    //can make what you like 
} 
+0

は、ご返信いただきありがとうございます。私は試してみましたが、AppDelegateにはメンバーhandleOpenURLWhenAppIsNotOpenはありません。私が間違って入力した場合はごめんなさい。 – ray

+0

handleOpenURLWhenAppIsNotOpenは、好きなもの(現在のログイン画面やアプリ内の他の画面)を書くことができる私のカスタムメソッドです。私の場合は、init tabBarControllerを選択し、4つのタブ –

0

を必要としています。次のコードは、必要に応じて参照用です。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    //branch.io 
    let branch: Branch = Branch.getInstance() 
    branch.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: {params, error in 
     // If the key 'pictureId' is present in the deep link dictionary 
     if error == nil && params!["pictureId"] != nil { 
      print("clicked picture link!") 
      // load the view to show the picture 
     } else { 
      // load your normal view 
     } 
    }) 
    //branch io 

    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) //don't put it on return 
    return true 
} 

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { 
     // pass the url to the handle deep link call 
     Branch.getInstance().continue(userActivity) 

} 
2
if let activityDic = launchOptions?[UIApplicationLaunchOptionsUserActivityDictionaryKey] { 

     let options = activityDic.allValues.filter({ (option:AnyObject) -> Bool in 

      return option is NSUserActivity 
     }) 

     if options.count > 0 , let userActivity = options[0] as? NSUserActivity{ 
      // User activity handling should be done here 

     } 

    } 

あなたは、従来の方法で「userActivity」にアクセスすることができない場合は、上記の方法を使用してください。 didFinishLaunchingWithOptionsに

1

置き、このコードは、時にアプリの起動(スウィフト3コード)URLを開くために機能:

if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? URL { //Deeplink 
     // process url here 
    } 
    else if let activityDictionary = launchOptions?[UIApplicationLaunchOptionsKey.userActivityDictionary] as? [AnyHashable: Any] { //Universal link 
     for key in activityDictionary.keys { 
      if let userActivity = activityDictionary[key] as? NSUserActivity { 
       if let url = userActivity.webpageURL { 
        // process url here 
       } 
      } 
     } 
    } 
関連する問題