PhoneGap InAppBrowser: open iOS Safari Browser

匿名 (未验证) 提交于 2019-12-03 03:12:02

问题:

In our PhoneGap iOS application, we are using the InAppBrowser plugin to display some content, and we need to open a page in Safari from within the InAppBrowser.

How can we have links from within the InAppBrowser open in Safari?

回答1:

From the phonegap documentation:

Opens a URL in a new InAppBrowser instance, the current browser instance, or the system browser.

var ref = window.open(url, target, options); 
  • ref: Reference to the InAppBrowser window. (InAppBrowser)
  • url: The URL to load (String). Call encodeURI() on this if the URL contains Unicode characters.
  • target: The target in which to load the URL, an optional parameter that defaults to _self. (String)

    • _self: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the InAppBrowser.
    • _blank: Opens in the InAppBrowser.
    • _system: Opens in the system's web browser.

So to answer your question, use:

window.open(your_url, '_system', opts); 

Note that the domain will need to be white-listed.


Update 4/25/2014:

I think I kind of misunderstood the question (thanks to commenter @peteorpeter) -- you want to have some way to click a link in the InAppBrowser and have that open in the system browser (e.g. Mobile Safari on iOS). This is possible, but it will require some forethought and cooperation between the app developer and the person responsible for the links on the page.

When you create an IAB instance, you get a reference to it back:

var ref = window.open('http://foo.com', '_blank', {...}); 

You can register a few event listeners on that reference:

ref.addEventListener('loadStart', function(event){ ... }); 

This particular event is fired every time the URL of the IAB changes (e.g. a link is clicked, the server returns a 302, etc...), and you can inspect the new URL.

To break out into the system browser, you need some sort of flag defined in the URL. You could do any number of things, but for this example let's assume there's a systemBrowser flag in the url:

.....html?foo=1&systemBrowser=true

You'll look for that flag in your event handler, and when found, kick out to the system browser:

ref.addEventListener('loadStart', function(event){     if (event.url.indexOf('systemBrowser') > 0){         window.open(event.url, '_system', null);     } }); 

Note that this is not the best method for detecting the flag in the url (could lead to false positives, possibly) and I'm pretty sure that PhoneGap whitelist rules will still apply.



回答2:

Unfortunately target=_system does not work from within the InAppBrowser. (This would work if the link originated in the parent app, though.)

You could add an event listener to the IAB and sniff for a particular url pattern, as you mention in your comments, if that fit your use case.

iab.addEventListener('loadstart', function(event) {    if (event.url.indexOf("openinSafari") != -1) {          window.open(event.url, '_system');      }  } 

The 'event' here is not a real browser event - it is a construct of the IAB plugin - and doesn't support event.preventDefault(), so the IAB will also load the url (in addition to Safari). You might try to handle that event within the IAB, with something like:

iab.addEventListener('loadstop', function(event) {    iab.executeScript('functionThatPreventsOpenInSafariLinksFromGoingAnywhere');   } 

...which I have not tested.



回答3:

This message is for clarification:

If you open an another with window.open by catching a link on loadstart, it will kill yor eventhandlers that assigned to first IAB.

For example,

iab = window.open('http://example.com', '_blank', 'location=no,hardwareback=yes,toolbar=no'); iab.addEventListener('loadstop', function(event) {console.log('stop: ' + event.url);}); iab.addEventListener('loaderror', function(event) { console.log('loaderror: ' + event.message); }); iab.addEventListener('loadstart', function(event) {     if (event.url.indexOf("twitter") != -1){         var ref2 = window.open(event.url, '_system', null);      } }); 

When the second window.open executed, it will kill all the event listeners that you binded before. Also loadstop event will not be fired after that window.open executed.

I'm finding another way to avoid but nothing found yet..



回答4:

window.open() doesn't work for me from within an InAppBrowser, whether or not I add a script reference to cordova.js to get support for window.open(...'_system'), so I came up with the following solution which tunnels the "external" URL back to the IAB host through the hashtag so it can be opened there.

Inside the InAppBrowser instance (I'm using AngularJS, but you can replace angular.element with jQuery or $ if you're using jQuery):

angular.element(document).find('a').on('click', function(e) {     var targetUrl = angular.element(this).attr('href');     if(targetUrl.indexOf('http') === 0) {         e.preventDefault();         window.open('#' + targetUrl);     } }); 

Note that that's the native window.open above, not cordova.js's window.open. Also, the handler code assumes that all URLs that start with http should be externally loaded. You can change the filter as you like to allow some URLs to be loaded in the IAB and others in Safari.

Then, in the code from the parent that created the InAppBrowser:

inAppBrowser.addEventListener('loadstart', function(e) {     if(e.url.indexOf('#') > 0) {         var tunneledUrl = e.url.substring(e.url.indexOf('#') + 1);         window.open(tunneledUrl, '_system', null);     } }); 

With this solution the IAB remains on the original page and doesn't trigger a back-navigation arrow to appear, and the loadstart handler is able to open the requested URL in Safari.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!