0

I have used custom url scheme to open url in browser.I have one button in browser. how to know in app when button is clicked in browser.

below is my code for ref:

 let customURL = URL(string: customURLScheme)!
    if UIApplication.shared.canOpenURL(customURL) {
        if #available(iOS 10.0, *) {
           // UIApplication.shared.open(customURL)
            UIApplication.shared.open(customURL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(customURL)
        }
    }

and in Appdelegate

       func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

  return true
 }

Can I use Universal links or deep linking?

2 Answers2

0

In case of Universal Link You should implement delegate method. Hope this snippet will be helpful.

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
        if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSURLComponents *URLComponents = [NSURLComponents componentsWithURL:userActivity.webpageURL resolvingAgainstBaseURL:YES];
        [[UniversalLinksManagerLocator model] handleDeepLink:userActivity.webpageURL];
    }
}
Paul Lafytskyi
  • 273
  • 3
  • 12
0

Use Universal Links

When linking from the browser, you should most definitely be using Universal Links. If the user does not have the app installed and they click a URI scheme, the browser will show an error message. There are ways around this, like a javascript redirect, but these are very hacky and tend not to work all the time.

Detecting click in browser

The functions in your app delegate will not be called until the app has already been handed control from the browser, so it's impossible to detect the browser click from the app itself. You'll have to use some javascript click event handlers to detect that, but all of the handoff is handled at the OS level so you won't be able to control that.

Registering an open from a deep link

Once the deep link opens your app, it will call one of three functions.

From URI Scheme (myapp://):

application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

From Universal Link ONLY WHEN APP IS RUNNING IN BACKGROUND:

(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler

From Universal Link if app is closed

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

This last one gets most people because they assume continueUserActivity should get called but it's really this function and they put the deep link url inside the launch options parameter.

Use Branch or a third party

Lastly, you could use Branch that leverages both URI and Universal Links whenever necessary and forwards all of your app delegate functions to one callback so you don't have to update routing logic in three different places.

clayjones94
  • 2,648
  • 17
  • 26