2017-03-08 185 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) 
       } 
      } 
     } 
    } 

在應用程序委託你需要方法

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

謝謝您的答覆。我試過,但它說AppDelegate沒有成員handleOpenURLWhenAppIsNotOpen。如果我輸錯了,我很抱歉。 – ray

+0

handleOpenURLWhenAppIsNotOpen是我的自定義方法,您可以在其中編寫您喜歡的任何內容(在應用程序中顯示登錄屏幕或其他屏幕)。在我的情況下,我初始化tabBarController並選擇四個選項卡中的一個 –

0

我已經使用Branch.io處理通用鏈接。如果有人需要,以下代碼僅供參考。

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」上面的方法。

1

將此代碼爲didFinishLaunchingWithOptions函數打開URL時,應用程序啓動(斯威夫特3碼):

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 
       } 
      } 
     } 
    } 
相關問題