iOS - 无需打开新标签即可从Native App返回Safari [英] iOS - Return to Safari from Native App without Opening New Tab

查看:137
本文介绍了iOS - 无需打开新标签即可从Native App返回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必须是网址编码或无法正常使用。



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



- (BOOL)应用程序:(UIApplication *)应用程序openURL :( NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation



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



@property(非原子,强)NSURL * sourceURL;



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



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



因为 self.sourceURL 包含您网页的网址,Safari将通过网址启动。但是,由于该页面已经打开,iOS会检测到该页面并重新打开该页面。



我的 Github页面实现了这一切。



并在安装完成后完成您的iPhone中的示例项目,从移动Safari打开此堆栈溢出帖子并打开我的真棒应用程序



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


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 - 无需打开新标签即可从Native App返回Safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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