xcode 如何获得打开应用程序而不是 safari 的通用链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43843093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a universal link to open the app instead of safari?
提问by Hazem Hagrass
I've been trying for a while now to get universal links of iOS10to work but it's not working till now.
我一直在尝试让iOS10 的通用链接正常工作,但直到现在还没有工作。
I have an apple-app-site-association configured.
Have tried using the root domain, using a subfolder, on the same server or another. In all cases I am redirected to safari web page with a drop down to open my app as an option
What am I missing?
我配置了一个 apple-app-site-association。已尝试在同一台服务器或另一台服务器上使用根域、使用子文件夹。在所有情况下,我都被重定向到带有下拉菜单的 safari 网页,以打开我的应用程序作为选项
我错过了什么?
回答by Tejas K
After spending some time trying to configure Universal Links here is what I have learned so far:
在花了一些时间尝试配置 Universal Links 之后,这是我到目前为止所学到的:
1 - Universal Links DO NOTwork with redirects or when you type your link in Safari address bar and hit enter.
1 - 通用链接不适用于重定向或当您在 Safari 地址栏中键入链接并按 Enter 键时。
2 - Type your address in Notes and click on it, if your app opens directly it means you have everything set-up correctly.
2 - 在 Notes 中输入您的地址并单击它,如果您的应用程序直接打开,则意味着您已正确设置所有内容。
3 - Universal Links work when user performs an actionon your website, to try this add a button to your website/blog and link it to the Universal Link implemented to open your app. Now open your website/blog in Safari, and click the button, it will directly open your app.
3 - 当用户在您的网站上执行操作时,通用链接会起作用,尝试将按钮添加到您的网站/博客并将其链接到为打开您的应用而实施的通用链接。现在在 Safari 中打开您的网站/博客,然后单击按钮,它将直接打开您的应用程序。
4 - When you click on 'Open' in Safari, subsequent calls won't open the app directly, for reasons mentioned in point 1.
4 - 当您在 Safari 中单击“打开”时,后续调用不会直接打开应用程序,原因在第 1 点中提到。
Hope this helps :)
希望这可以帮助 :)
回答by Arik Segal
You have probably tapped a small button on the device status bar while using the app, that led you back to the web site. Once you do that, the system would remember that and would always launch Safari when a universal link to your app is tapped. To undo that, long-tap the link, and in the menu that pops up, choose "Open with [your app]"
您可能在使用该应用程序时点击了设备状态栏上的一个小按钮,这会将您带回网站。一旦你这样做了,系统就会记住这一点,并在你的应用程序的通用链接被点击时总是启动 Safari。要撤消该操作,请长按链接,然后在弹出的菜单中选择“使用 [您的应用程序] 打开”
回答by Chandan Anand
In AppDelegate.m call this:
在 AppDelegate.m 中调用:
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options;{
NSLog(@"url recieved: %@", url);
NSLog(@"query string: %@", [url query]);
NSLog(@"host: %@", [url host]);
NSLog(@"url path: %@", [url path]);
NSDictionary *dict = [self parseQueryString:[url query]];
NSLog(@"query dict: %@", dict);
if ([[url host] isEqualToString:@"login"]) {
[self setupHomeScreen:NO type:@"" entityId:@""];
[self _endSession];
return YES;
} else if ([[url host] isEqualToString:@"smartoffice"]) {
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
NSRange needle = [url.absoluteString rangeOfString:@"?" options:NSCaseInsensitiveSearch];
NSString *data = nil;
if(needle.location != NSNotFound) {
NSUInteger start = needle.location + 1;
NSUInteger end = [url.absoluteString length] - start;
data = [url.absoluteString substringWithRange:NSMakeRange(start, end)];
}
for (NSString *param in [data componentsSeparatedByString:@"&"]) {
NSArray *keyvalue = [param componentsSeparatedByString:@"="];
if([keyvalue count] == 2){
[result setObject:[keyvalue objectAtIndex:1] forKey:[keyvalue objectAtIndex:0]];
}
}
NSString *entityID = ([result objectForKey:@"secondparameter"]);
NSString *type = [result objectForKey:@"parameter"];
[self setupHomeScreen:YES type:type entityId:entityID];
//[self setupHomeScreen:YES type:@"TASK" entityId:@"160315"];
return result;
} else {
NSLog(@"myApp is not installed");
[self setupHomeScreen:NO type:@"0" entityId:@"0"];
}
return NO;
}
Open Xcode, got to Project Settings -> Info, and add inside ‘The URL Types” section a new URL scheme.
打开 Xcode,进入项目设置 -> 信息,然后在“URL 类型”部分添加一个新的 URL 方案。
That is the scheme ://resource. So we go ahead and type com.myApp://
这就是方案://资源。所以我们继续输入 com.myApp://
Fo me its backofficeapp://
对我来说它的后台应用程序://
Now use this link on Safari
现在在 Safari 上使用此链接
backofficeapp://// This one only redirects you to a landing page.
backofficeapp://// 这只会将您重定向到登录页面。
OR
或者
backofficeapp://smartoffice/1?parameter=TASK&secondparameter=157536
backofficeapp://smartoffice/1?parameter=TASK&secondparameter=157536
Where
在哪里
parameter = “TASK”
参数 = “任务”
secondparameter = “taskID” // #iOS Mobile 2020 = 157536
secondparameter = “taskID” // #iOS Mobile 2020 = 157536
This parameter is for landing on a specific screen.
该参数用于登陆特定屏幕。
Remember this URL won't work if the app is not installed.
请记住,如果未安装该应用程序,则此 URL 将不起作用。