Universal Links while app is not running

六月ゝ 毕业季﹏ 提交于 2019-12-12 20:45:56

问题


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


回答1:


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.




回答2:


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




回答3:


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"


来源:https://stackoverflow.com/questions/36283423/universal-links-while-app-is-not-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!