我知道在iOS上可以通过注册自定义方案(例如so://)直接链接到应用程序,也可以通过iTunes链接到应用商店。
在许多情况下,理想的流程是提供一个链接,如果已安装该应用程序,则重定向到该应用程序,否则重定向到商店。这是否可能,如果可能,如何实现?
为了明确情况,场景是我正在iPhone上从电子邮件中打开链接(http),邀请我加入应用程序中的一个组。如果用户在该设备上安装了该应用,则应该打开它,否则http链接应重定向到iTunes。
我知道在iOS上可以通过注册自定义方案(例如so://)直接链接到应用程序,也可以通过iTunes链接到应用商店。
在许多情况下,理想的流程是提供一个链接,如果已安装该应用程序,则重定向到该应用程序,否则重定向到商店。这是否可能,如果可能,如何实现?
为了明确情况,场景是我正在iPhone上从电子邮件中打开链接(http),邀请我加入应用程序中的一个组。如果用户在该设备上安装了该应用,则应该打开它,否则http链接应重定向到iTunes。
我认为更简单的方法是在您的服务器上设置一个页面,其中包含以下Javascript代码:
(function() {
var app = {
launchApp: function() {
window.location.replace("myapp://");
this.timer = setTimeout(this.openWebApp, 1000);
},
openWebApp: function() {
window.location.replace("http://itunesstorelink/");
}
};
app.launchApp();
})();
这基本上试图重定向到您的应用程序,并设置一个超时时间,如果失败就重定向到应用商店。
你甚至可以让代码更加智能,检查用户代理以确定他们是iOS用户、Android用户还是Web用户,然后适当地将其重定向。
无法进行此项检查。
不过,有一个很好的解决方法。
基本思路如下:
最后两个步骤在此SO帖子中有详细说明。
iframe
的网页,iOS会自动重定向到该位置。如果该应用程序未安装,则不会发生任何事情。这使您可以深入链接到已安装的应用程序中,或者如果未安装,则重定向到应用商店。<!DOCTYPE html>
<html>
<head>
<title>iOS Automatic Deep Linking</title>
</head>
<body>
<iframe src="twitter://" width="0" height="0"></iframe>
<p>The Twitter App is not installed</p>
</body>
</html>
如果应用程序未安装,则以下是一个更详细的重定向到App Store的示例:
<!DOCTYPE html>
<html>
<head>
<title>iOS Automatic Deep Linking</title>
<script src='//code.jquery.com/jquery-1.11.2.min.js'></script>
<script src='//mobileesp.googlecode.com/svn/JavaScript/mdetect.js'></script>
<script>
(function ($, MobileEsp) {
// On document ready, redirect to the App on the App store.
$(function () {
if (typeof MobileEsp.DetectIos !== 'undefined' && MobileEsp.DetectIos()) {
// Add an iframe to twitter://, and then an iframe for the app store
// link. If the first fails to redirect to the Twitter app, the
// second will redirect to the app on the App Store. We use jQuery
// to add this after the document is fully loaded, so if the user
// comes back to the browser, they see the content they expect.
$('body').append('<iframe class="twitter-detect" src="twitter://" />')
.append('<iframe class="twitter-detect" src="itms-apps://itunes.com/apps/twitter" />');
}
});
})(jQuery, MobileEsp);
</script>
<style type="text/css">
.twitter-detect {
display: none;
}
</style>
</head>
<body>
<p>Website content.</p>
</body>
</html>
“智能应用横幅”- 不确定它们何时出现,但在寻找相同内容后发现本帖子,然后是关于智能应用横幅的跟进。
智能应用横幅是每个页面头部的单行html元标记,用于向用户提供您的应用程序,以便获得更好的网络体验:
<meta name="apple-itunes-app" content="app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL">
该图标显示在页面顶部,并带有“在应用中打开此页面”或转到App Store的选项。
iPhone上这个页面的元数据如下(当然是匿名的):
<meta name="apple-itunes-app" content="app-id=605841731, app-argument=lync://confjoin?url=https://meet.rtc.yourcorporatedomain.com/firstName.lastName/conferenceID">
是的,这很容易。这需要你想要打开的应用程序在plist中声明一个url scheme:
//if you can open your app
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"yourapp://"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"yourapp://"]];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"ituneappstorelink"]];
}
有几个简单的步骤可以实现此操作
步骤1
进入 -> 项目(选择目标) -> 信息 -> URL类型
在Xcode中创建URL Scheme
这里的URL Scheme是myApp(最好将所有字符都小写)。
步骤2
如果您计划从URL接收参数/查询字符串,请设置委托。
这里是代码:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
NSLog(@"APP : simple action %@",url.scheme);
if ([url.scheme hasPrefix:@"myapp"]) {
NSLog(@"APP inside simple %@",url.absoluteString);
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:NO];
NSArray *queryItems = urlComponents.queryItems;
NSString * abc = [self valueForKey:@"abc"
fromQueryItems:queryItems];
NSString * xyz = [self valueForKey:@"xyz"
fromQueryItems:queryItems];
NSLog(@"Sid up = %@", abc);
NSLog(@"PID up = %@", xyz);
// you can do anything you want to do here
return YES;
}
return NO;
}
Xcode端工作结束。
第三步
参考@BananaNeil的代码,因为我不是前端开发人员。
(function() {
var app = {
launchApp: function() {
window.location.replace("myApp://share?abc=12&xyz=123");
this.timer = setTimeout(this.openWebApp, 1000);
},
openWebApp: function() {
window.location.replace("http://itunesstorelink/");
}
};
app.launchApp();
})();
如果有人仍然被这个问题困扰并需要最简单的解决方案,你会喜欢 node-deeplink
1.) 如果应用已安装:通过深度链接调用应用程序将始终调用根组件的componentDidMount。因此,您可以在那里附加侦听器。例如:
Linking.getInitialURL()
.then(url => {
if (url) {
this.handleOpenURL({ url });
}
})
.catch(console.error);
Linking.addEventListener('url', this.handleOpenURL);
handleOpenURL(event) {
if (event) {
console.log('event = ', event);
const url = event.url;
const route = url.replace(/.*?:\/\//g, '');
console.log('route = ', route);
if(route.match(/\/([^\/]+)\/?$/)) {
const id = route.match(/\/([^\/]+)\/?$/)[1];
const routeName = route.split('/')[0];
if (routeName === 'privatealbum') {
Actions.privateAlbum({ albumId: id });
}
}
}
}
2.) 如果应用程序未安装:只需在您的服务器上设置路由,node-deeplink包将处理当应用程序未安装在您的移动设备上时,从Web浏览器到应用商店的桥接。
通过这种方式,这两种情况都可以轻松处理。