ios url从邮件重定向到应用程序 [英] ios url redirect from mail to app

查看:97
本文介绍了ios url从邮件重定向到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以某种方式使用通过邮件发送的URL启动我的应用程序?例如,我有用户想要邀请他们的朋友加入应用程序的用户个人资料。他们发送的电子邮件有一些网址:

Is it possible to somehow launch my app using a URL sent via mail? For example I have User profile that user wants to invite their friend into the app. They send an e-mail that has some url like:

通过以下链接加入我:
http:// appname?sender_id = 25& some_other_value =某事

join me via this link: http://appname?sender_id=25&some_other_value=something

并从iPhone打开该链接会将用户带入应用程序并让我解析这些值。

and opening that link from the iPhone would bring user into the application and would let me parse those values.

这可能吗?

推荐答案

是的,这完全有可能。
您需要为您的应用注册URL方案。

Yes, this is totally possible. You need to register a URL scheme for your app.

在Xcode中选择您的应用项目,然后单击目标,并从信息选项卡中注册一个新的URL方案。

Select your app project in Xcode, then click on the target, and from the Info tab, register a new URL scheme.

标识符是您的应用程序标识符com.company.AppName,URL Scheme是您要使用的名称,如appName

The identifier is your app identifier as com.company.AppName and the URL Scheme is the name you wish to use, like appName

现在至于理想的解决方案,因为我们现在将这个添加到我们的应用程序中,理想情况下,您应该不使用自定义方案发送电子邮件中的链接。
原因是用户可能会从计算机上打开它,因此此链接无效。

Now as for the ideal solution, as we are adding this to our app now, you should ideally NOT send links in email with your custom scheme. The reason is that the user might open it from a computer, so this link will not work.

最佳方案如下:


  1. 当你的应用程序第一次运行时,打开你的应用程序Safari浏览器并将其发送到你的网站。

  2. 在网站上,为Safari安装一个cookie(如myAppIsInstalled)

  3. 在同一个网站上,只需将用户重定向到您的应用,即可将用户踢回您的应用自定义网址方案,例如 appname://

  1. When your app is run for the First time, open from withing your app the Safari browser and send it to your website.
  2. In the website, install a cookie for Safari (like myAppIsInstalled)
  3. In the same website, kick the user back to your app, by just redirecting him to your app with your custom URL scheme, like appname://

现在你发送你的通过链接到您网站的普通网址发送电子邮件
,这里是第2部分:

Now you send in your emails the links with normal URL's that link to your website and here comes part 2:


  1. 在您的网站中检查您的网站应用程序已安装(存在cookie)

  2. 如果存在,则不是从您的网站打开链接,而是将用户重定向到您的应用程序,并使用正确的值,例如

  1. In your website you check if your app is installed (the cookie is present)
  2. If it's present, instead of opening the link from your website, redirect the user to your app, with the proper values, like

appname:// mailbox?sender_is = 123& user_name = Lefteris

appname://mailbox?sender_is=123&user_name=Lefteris

这可确保您的电子邮件链接始终有效,并且只有在您的应用已安装在设备...

This ensures your email links will always work and that you will open from Mobile Safari the links ONLY if your app has been installed on the device...

最后,只是一个注释,URL方案是 appname:// 而不是 http:// appname

Finally, just a note, the URL scheme is appname:// and not http://appname

现在更好地解释第1部分,在我们的AppDelegate中,我们可以在 - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 委托:

Now to explain the part 1 better, in our AppDelegate, we can do this in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions delegate:

//if user has not set the app installed cookie, set it now.
bool hasAppInstalledCookie = [[NSUserDefaults standardUserDefaults] boolForKey:@"appInstalledCookie"];
if (!hasAppInstalledCookie) {
    //mark it was set
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"appInstalledCookie"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    //open the web browser
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.myApp.com/appInstalled"]];
}

现在我们的appInsalled页面(例如index.html),我们只是设置一个cookie(我们想要的任何cookie名称),然后我们将用户踢回我们的应用程序,如下所示:

Now in our appInsalled page, (index.html for example), we just set a cookie (any cookie name we want) then we kick the user back to our app, like this:

<script type="text/javascript">
    window.location = 'appName://';
</script>

我们使用cookie的原因是当用户打开电子邮件时使用此cookie链接。我们将检查浏览器是否是移动版Safari并安装了cookie。
这样,我们知道用户已经安装了我们的应用程序,重定向将正常工作。

The reason we are using a cookie, is to use this cookie, when the user opens up an email link. We will check if the browser is mobile safari AND the cookie is installed. This way, we know the user has installed our app, and the redirect will work properly.

这篇关于ios url从邮件重定向到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆