Madu
Madu

Reputation: 5019

Universal Links while app is not running

I am implementing Universal Links in my app. Every things works for me, except when the app is not running in the background. In that case how i can open a specific page in my app? iOS launches my app but i am not getting any callback in

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler
in this function neither i am getting any url with this line

NSURL *launchURL = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey]; inside

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions this function.

Does anybody have an idea how i can get the url which caused to launch my application.

Thanks

Upvotes: 2

Views: 2388

Answers (4)

KVISH
KVISH

Reputation: 13178

In Swift 5 with a SceneDelegate file it will call:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { }

When your app is NOT running in background and user clicks on a universal link.

Upvotes: 1

fabiosoy
fabiosoy

Reputation: 31

You can access to your url in this way ...

    NSDictionary *aux = [launchOptions objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];
    if (aux) {
         NSUserActivity *activity = [aux objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
         NSString *urlString = activity.webpageURL.absoluteString;
    }

But for better use you have to validate this URL and return YES if you can manage it

For example ...

if([urlString hasPrefix:@"https://myUrl.com"]) {
  return YES;
}

.. after this the method

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

It will be called

Upvotes: 2

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

You need to implement

- (BOOL)application:(UIApplication *)application  openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation{

   url= yourappurl://with some app link data
   sourceApplication= //who called your application

  }

For example if your app is called from facebook messenger, then you will get

  sourceApplication=@"com.facebook.messenger"

Upvotes: 0

Jelly
Jelly

Reputation: 4522

When you open a Universal Link with your app

 - (BOOL)application:(UIApplication *)app
        openURL:(NSURL *)url
        options:(NSDictionary<NSString *,
                         id> *)options

is called with the Universal Link as a parameter and options if it is the case. Check apple docs for details.

Upvotes: 1

Related Questions