Can an iOS App Switch to Safari Without Opening a Page

iOS - Return to Safari from Native App without Opening New Tab

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

Defining a custom URL scheme

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.

Open Mobile Safari without using openURL:url

I solved this using the answers on this page ..

iOS How can I use UIApplication launchApplicationWithIdentifier which is in private APIs?

Though I didn't have to move my .app file in to /Applications

It was enough to add the entitlements, then simply call it using ...

[[UIApplication sharedApplication] launchApplicationWithIdentifier:@"com.apple.mobilesafari" suspended:NO];

Plasma

Launching Safari App without using any specific url from an iOS App

Actually we can open a specific app from safari. You have to create a custom url scheme for the application and open that link from safari like any other link is opened from safari.

Create a custom url scheme like

myApp://

Then open it from safari e.g on onClick of button.

Return to the browser page that launched an app without refreshing

I finally work out a way to do this. If you append #anything to the URL of a page that is already open in Safari, it simply returns to that page without reloading it!

This makes sense although there are a few anomalies. If there is an anchor on the page matching the #anything, it doesn't jump to that point in the page as it would if a link with the anchor was clicked. Also, if there is already a page open exactly matching the URL including #anything, the page is reloaded.

So my solution it so to call the original URL with a #random anchor appended to it.



Related Topics



Leave a reply



Submit