Reputation: 8068
I want to be able to authenticate a transaction from a Safari website via a custom iPhone app, then have a callback that takes the user back to the same Safari window... A bit like Facebook OAuth-style login.
It's step #4 I don't know how to do. I guess I could quite easily redirect the user back to the browser in a different window, but is it possible to send them back to the same window they were in before with a callback?
Upvotes: 1
Views: 200
Reputation: 61
Update: It is possible, after some experimentation I've spotted something interesting in Safari's behaviour. If Safari has a tab open with a url not on the site's root then it will not open a new tab if your app requests the same URL again.
So in your case if you came from facebook.com/auth and your app opened http://facebook.com/auth then the tab will be reused! You can safely pass in web parameters too; these will be ignored in the tab handling decision. I haven't tested http vs https to see if this is also ignored but it's something to be aware of.
Old answer:
yes it's possible, however the only documented way to open Safari will result in a new tab being opened for your "request".
The documented way to do this is to use a URL scheme like this:
if( [[UIApplication sharedApplication] canOpenURL:myURL] )
{
[[UIApplication sharedApplication] openURL:myURL];
}
Why I say this is possible is because Google Maps does this with iOS 8. If you use mobile Safari to search for an address then select the resulting map view it will open the Google Maps app (if it's installed). The Google maps app will have a banner at the top of the page stating "RETURN TO SAFARI". If you click on this banner it will take you back to Safari without opening a new tab just in the fashion you would like.
This very same behaviour is also extended to Chrome too. The documentation here also states a new tab will be created when you open a URL in Chrome unless you make multiple calls:
https://developer.chrome.com/multidevice/ios/links#creating-a-new-tab
Of course Chrome is their own app and in this case they're well within their rights to use undocumented functionality. How Google has achieved this with Safari I can't say.
I would suggest ideally finding a way to support both browsers as Google have done with their maps application to give users a consistent experience and respect their choice of browser. This functionality is certainly reasonable in my opinion; if Safari launched your app why should you not be able to return the user back to that page.
Upvotes: 3