2011-05-06 67 views
25

我正在爲使用PhoneGap的iPad編寫應用程序,並且希望加載外部網址而不觸發Safari或使用內部網絡瀏覽器(如ChildBrowser)。PhoneGap for iPhone:加載外部URL時出現問題

我正在使用PhoneGap iPad/iPhone示例項目,並嘗試了不同的方法。在onBodyLoad()函數我說:

window.location.href('http://www.wordreference.com'); 

,但此行開啓使用新的Safari瀏覽器window.From這一點是不可能的鏈接回來的PhoneGap

之後,我試着用AJAX請求替換頁的內容使用document.write

function loadHTML(url, timeout) { 
if (timeout == undefined) 
    timeout = 10000; 
var req = new XMLHttpRequest(); 
var timer = setTimeout(function() { 
    try { 
     req.abort(); 
    } catch(e) {} 
    navigator.notification.loadingStop(); 
},timeout); 
req.onreadystatechange = function() { 
    if (req.readyState == 4) { 
     if (req.status < 300) { 
      clearTimeout(timer); 

      var html = req.responseText; 
      //just a debug print 
    alert(html); 
    document.write(html); 

     } 
     navigator.notification.loadingStop(); 
     delete req; 
    }  
};   
req.open('GET', url, true); 
req.send(); 
} 

現在,從內部onBodyLoad()調用:

loadHTML('http://www.wordreference.com',10000); 

打開PhoneGap容器中的鏈接,這很好。問題的關鍵是,我想加載動態頁面用Python編寫的

loadHTML('http://www.mysite.com/cgi-bin/index.py',10000) 

在這一點上的Safari不叫,但顯示在PhoneGap的容器中的黑頁! 我想指出,如果我在Safari中輸入鏈接,我的鏈接完全可以正常工作(我無法報告它的隱私問題)。

可能是與某種需要的權限有關的問題???

我發現了類似的相對的東西的PhoneGap黑莓和所提出的解決方案是修改與

<access subdomains="true" uri="http://www.mysite.com/" /> 

一個config.xml文件我想直接在我的index.html添加此標籤,但它不工作。

iPhone有沒有類似的方法?

非常感謝

回答

23

我想我已經找到了解決辦法,

在PhoneGap的應用程序委託.m文件{} YourProject AppDelegate.m,修改方法:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType]; 
} 

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
NSURL *url = [request URL]; 
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) { 
    return YES; 
} 
else { 
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ]; 
} 
} 

這將打開PhoneGap容器內的所有外部鏈接!

ps。您會在附近找到對此link的引用,但我認爲它不適用於使用0.9.5版編寫的應用程序,因爲默認情況下,Safari會爲外部鏈接打開。

+0

好極了!這是在應用程序內部保留window.location.href =''命令的正確答案。 – davidethell 2011-12-28 00:13:29

+3

之後如何導航迴應用程序? – 2012-10-26 12:13:17

+0

太棒了,謝謝你。請注意,從1.6開始,您需要在MainViewController.m中查找此方法。 – 2013-06-20 14:54:54

2

這工作 - 謝謝克勞斯。也許有些應用需要比「http」和「https」更具歧視性。

我用phonegap android做了類似的事情,見下文。提供一個接口(我在這裏稱爲EXTERNALLINK),從javascript調用loadExternalLink,然後將該URL加載到當前的WebView中。我不是專家,但似乎爲我工作,只爲你想要應用它的鏈接。

活動:

public class AndroidActivity extends DroidGap { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    try 
    { 
     super.loadUrl("file:///android_asset/www/index.html"); 
     this.appView.addJavascriptInterface(new JavaScriptInterface(), "EXTERNALLINK"); 
    } 
    catch(Exception lException) 
    { 
     throw new RuntimeException("hello hello", lException); 
    } 
    } 

    class JavaScriptInterface 
    { 
     public void loadExternalLink(String lUrl) 
     {   
     try 
     { 
      loadUrl(lUrl); 
     } 
     catch(Exception lEx) 
     { 
      int i = 0; 
     } 
     } 
    } 
} 

JavaScript調用示例:

window.EXTERNALLINK.loadExternalLink("http://www.google.com");

10

對於具有在安卓這個問題的人:

我不知道早期版本,但在PhoneGap的1.1.0您可以創建一個名爲res/xml/phonegap.xml的文件並列出這些域d不能在外部瀏覽器中打開。

DroidGap.java

/** 
* Load PhoneGap configuration from res/xml/phonegap.xml. 
* Approved list of URLs that can be loaded into DroidGap 
*  <access origin="http://server regexp" subdomains="true" /> 
* Log level: ERROR, WARN, INFO, DEBUG, VERBOSE (default=ERROR) 
*  <log level="DEBUG" /> 
*/ 
private void loadConfiguration() { 
[...] 

phonegap.xml

<?xml version="1.0" encoding="UTF-8"?> 
<phonegap> 
    <access origin="http://stackoverflow.com" subdomains="true" /> 
</phonegap> 
+0

+1在1.2上爲我工作,非常感謝! – JohnP 2011-12-12 07:56:01

+4

請注意,任何谷歌的,它應該沒有「http://」 - 只是「stackoverflow.com」 – xdumaine 2011-12-21 21:55:48

1

在Android中,你可以通過設置

super.setBooleanProperty("loadInWebView", true); 
做外部鏈接的網頁視圖中打開

在你的DroidGap Activity的super.loadUrl之前。

這將使每個外部鏈接在web視圖中打開。如果您只想在網絡視圖中打開某些網域,請改爲使用addWhiteListEntry。例如:

addWhiteListEntry("mydomain.com", true); 
+2

不幸的是,在頁面轉換期間屏幕變黑。這很煩人。你知道任何方式嗎? – nisc 2011-10-18 14:00:44

+0

無法正常工作,即使在CordovaActivity中也找不到setBooleanProperty – theLazyFinder 2016-02-24 18:38:36

2

在Android中,以解決屏幕中的頁面過渡去黑的問題,爲的PhoneGap 1.1.0,你可以把:

super.setIntegerProperty("backgroundColor", Color.WHITE); 
super.setStringProperty("loadingPageDialog", "Loading page..."); 

super.loadUrl之前在你的DroidGap Activity的onCreate()方法。

這裏是一個參考PhoneGap的論壇有細節:

http://comments.gmane.org/gmane.comp.handhelds.phonegap/11491