iOS - 从本机应用程序返回 Safari 而不打开新标签 [英] iOS - Return to Safari from Native App without Opening New Tab

查看:13
本文介绍了iOS - 从本机应用程序返回 Safari 而不打开新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何从 iOS 7+ 中的本机应用切换到 Safari.我使用了 UIApplication.sharedApplication.openURL(),但这会打开一个新选项卡.我想在不打开新标签的情况下将用户返回到他/她之前查看的当前页面.我发现

我将解释您的应用已安装的情况.

假设包含链接的网页是:

http://www.mywebsite.com/mypage.html#mytag

您在网页中提供的链接应将一些参数传递给应用,其中之一应该是您希望应用返回的 URL.以下示例链接可能是:

myawesomeapp://?action=my_action_1&sourceurl=http%3A%2F%2Fwww.mywebsite.com%2Fmypage.html%23mytag

请注意,您在方案中作为参数传递的 URL 必须是 URL 编码 否则将无法正常工作.

3) 在您的应用委托中,您需要实现该方法:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

在此方法中,解析 URL,解码查询项并将 sourceURL 传递给负责在调用之前处理操作的视图控制器.在这种情况下,我在 ViewController 中设置了一个公共属性来存储 URL.

@property (nonatomic, strong) NSURL *sourceURL;

4) 在视图控制器中,当用户完成交互时,您只需调用:

[[UIApplication sharedApplication] openURL:self.sourceURL];

因为 self.sourceURL 包含您网页的 URL,Safari 将使用该 URL 启动.但是,由于该页面已经打开,iOS 会检测到这一点并重新打开该页面.

我的 Github 页面 中有一个示例项目,它实现了所有这些.

最后,在您的 iPhone 中安装示例项目后,从移动 Safari 中打开此堆栈溢出帖子并打开 my awesome应用

打开应用后,单击按钮,您将返回此堆栈溢出帖子.

I am having trouble figuring out how to switch to Safari from a native app in iOS 7+. I've used UIApplication.sharedApplication.openURL(), but that opens a new tab. I would like to return the user to the current page he/she was viewing before without opening a new tab. I found this SO post, but it is a few years old, so I was hoping things have changed since then.

Here is the workflow I am envisioning:

  1. User taps on a link on an HTML page on Safari to open/install my app
  2. User performs an action on my app
  3. After the user is done performing the action, my app opens Safari automatically, and the user is back on the page where he/she left off

Google has somehow done this with their Google Maps app. If you search for an address on google.com on Safari, you can tap on the map that appears in the search results, and it will open the Maps app. At the top of the Maps app will be a "Return to Safari" bar that you can tap. Once you tap it, you are returned to Safari without loading another tab. I can't seem to find anything regarding how Google did this. If I can replicate that behavior in my app, that would work just fine.

Any help would be greatly appreciated!

解决方案

There is a way to accomplish what you want using standard iOS APIs. No need to use external components.

You control your webpage and your app, so you know the exact URL that has the link to your app.

These are the steps:

1) In your app, define a custom URL scheme. In this case let's assume you use the scheme myawesomeapp://. You can do this in your Xcode project by going to the Info section of your target. See below

2) In your web page you need to handle the two scenarios: app installed / not installed. It is just a matter of detecting if an app responds to the scheme myawesomeapp://.

To detect from your webpage if your app is not installed please refer to this post

I will explain the case where your app is already installed.

Let's say the webpage that contains the link is:

http://www.mywebsite.com/mypage.html#mytag

The link you provide in your webpage should pass some parameters to the app and one of these should be the URL that you want the app to return. Following with the example the link could be:

myawesomeapp://?action=my_action_1&sourceurl=http%3A%2F%2Fwww.mywebsite.com%2Fmypage.html%23mytag

Note that the URL you pass as a parameter inside the scheme has to be URL encoded or it won't work properly.

3) In your app delegate you need to implement the method:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

In this method, parse the URL, decode the query items and pass the sourceURL to the view controller responsible of handling the action prior to calling it. In this case I set a public property in the ViewController that will store the URL.

@property (nonatomic, strong) NSURL *sourceURL;

4) In the view controller when the user finishes the interaction, you just call:

[[UIApplication sharedApplication] openURL:self.sourceURL];

Because self.sourceURL contains the URL of your webpage, Safari will be launched with the URL. However, because that page is already opened, iOS detects this and re-opens that page.

I have a sample project in my Github page that implements all this.

And to finalize, after you install the sample project in your iPhone, open this stack overflow post from mobile Safari and open my awesome app

Once the app is opened, click on the button and you will return to this stack overflow post.

这篇关于iOS - 从本机应用程序返回 Safari 而不打开新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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