如果安装,则重定向到应用程序,否则重定向到 AppStore

我知道通过注册一个自定义方案(例如://)可以直接链接到 iOS 中的应用程序,也可以通过 itunes 链接到 Appstore 中的应用程序。

在许多情况下,理想的流程是提供一个链接,重定向到它所安装的应用程序 如果,如果没有,则重定向到存储。这可能吗? 如果可能,怎么做?

为了清晰起见,我在 iphone 上打开了一个邀请我加入一个应用程序小组的邮件链接。如果用户在该设备上安装了应用程序,它应该打开,否则 http 链接应该重定向到 itunes。

103782 次浏览

是的,很简单。这需要你想打开的应用程序在 plist 中声明一个 url 方案:

//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 在你的服务器上
  2. 在这个 URL 上,您设置了一个 cookie,比如 appInstall to the users mobile safari
  3. 然后用你的注册方案将用户踢回到你的应用程序(就像 FB 对 SSO 所做的一样)
  4. 你所有的电子邮件链接都指向你的网站,但是在网站上,你要检查浏览器是否是移动 Safari,是否存在已安装的 cookie
  5. 如果浏览器不是移动 Safari 或者没有找到 Cookie,您可以重定向到 AppStore,或者停留在您的网页中。
  6. 如果 # 4的条件为真,则使用注册方案将用户重定向到应用程序
  7. 如果应用程序已被用户删除,因此自定义 URL 方案失败,那么您就有了一个故障安全重定向到应用程序商店

最后两个步骤是解释 在这个职位

我认为更简单的答案是在你的服务器上用下面的 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 用户还是 webuser,然后对它们进行适当的重定向。

如果你有一个网页,你链接的电子邮件与网页包含一个 iframesrc设置为自定义方案的应用程序,iOS 将自动重定向到该位置的应用程序。如果没有安装应用程序,什么都不会发生。这允许您深入链接到应用程序,如果它是安装,或重定向到应用程序商店,如果它没有安装。

例如,如果您安装了 twitter 应用程序,并导航到一个包含以下标记的网页,您将立即被引导到该应用程序。如果您没有安装 Twitter 应用程序,您将看到文本“ The Twitter App is not install”

<!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>

下面是一个更全面的例子,如果没有安装应用程序,它会重定向到应用程序商店:

<!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">

它显示在页面顶部的图标和“打开此页面”与应用程序或路由到应用程序商店。

enter image description here

IPhone 上这个页面的元数据如下所示(当然是匿名的) :

<meta name="apple-itunes-app" content="app-id=605841731, app-argument=lync://confjoin?url=https://meet.rtc.yourcorporatedomain.com/firstName.lastName/conferenceID">

苹果开发者文档-用智能应用程序横幅推广应用程序

有几个简单的步骤来实现这一行动

第一步

Go-> Project (select target)-> info-> URL Types

enter image description here

在 Xcode 建立网址方案 像这样

enter image description here 这里的 URL Scheme 是 myApp (最好是所有字符都用小写)。

第二步

如果您计划从 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();
})();

希望对大家有所帮助

这里有很多复杂的边缘情况,所以最简单的解决方案是让别人来处理这些事情。

这就是 https://branch.io/的工作。 你可以使用他们的免费计划来实现你想要的,还有一些额外的功能

  • 统计数字
  • 您可以随链接一起传递信息,即使用户必须首先进行安装,也可以检索到该链接
  • 链接将工作在桌面上(默认情况下,它将文本链接到您的手机安装)

我不是 Branch.io 的员工,但我用他们的产品。

如果有人仍然困在这个问题,需要最简单的解决方案,你会喜欢 Node-deeplink

1)如果应用程序已经安装: 通过深度链接调用应用程序将始终调用 root 组件的 Component entDidMount。所以你可以在那里安装一个监听器。 比如:

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 浏览器与应用程序存储之间的桥接。

通过这种方式,两个案件都能得到顺利解决