Reputation: 161
I have similar case with this problem iOS: apple universal link if app is not open? . When I click an universal link, the app could not go into func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {}
if it is not in background.
I added some codes in the didFinishLaunchingWithOptions
. However it is not working. Thank you so much If anyone could help.
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
}
Upvotes: 10
Views: 7845
Reputation: 433
You can access the URL in the launch options dictionary that is passed to your app at initialization time. Example of an implementation inside the AppDelegate
application(_:didFinishLaunchingWithOptions:)
method:
// 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)
}
}
}
}
// or
if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
AppDelegate.handleOpenURLWhenAppIsNotOpen(url)
}
In AppDelegate
you need method:
private class func handleOpenURLWhenAppIsNotOpen(_ url: URL) {
//can make what you like
}
Upvotes: 2
Reputation: 626
For IOS14 Swift 5 use SceneDelegate file and func
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Get URL components from the incoming user activity.
guard let userActivity = connectionOptions.userActivities.first,
userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let incomingURL = userActivity.webpageURL
else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
LinkHandler.sharedInstance.handleLink(url: incomingURL)
}
guard let _ = (scene as? UIWindowScene) else { return }
}
Upvotes: 3
Reputation: 151
Swift 4, 5 (Working solution)
You can try the following code snippet:
if let launchOptions = launchOptions, let dict = launchOptions[UIApplication.LaunchOptionsKey.userActivityDictionary] as? NSDictionary {
if let userActivity = dict.first(where: { $0.value as? NSUserActivity != nil })?.value as? NSUserActivity {
// handle your logic here
}
}
Upvotes: 2
Reputation: 964
Place this code into didFinishLaunchingWithOptions function to open the URL when the app launch (Swift 3 code) :
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
}
}
}
}
Upvotes: 11
Reputation: 71
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
}
}
Use the above method if you can't access 'userActivity' in conventional way.
Upvotes: 2
Reputation: 161
I have used Branch.io for handling universal link. The following code is for reference if anyone needed.
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)
}
Upvotes: 0