2013-02-28 220 views
3

我有使用PhoneGap和JQuery mobile製作的iOS應用程序。該應用程序有一些我想在移動Safari中打開的外部鏈接,但截至目前,它們只是在應用程序視圖中打開。這些鏈接是這樣寫的:強制iOS應用程序在safari中打開外部鏈接

<a rel="external" href="wwww.example.com">Click Here</a> 

我讀的JQuery移動文檔和它說,加入rel="external"會解決這個問題,但顯然不是。有任何想法嗎?請記住,這是一個基於HTML的應用程序。

+0

是由有'rel ='一個錯字? – jjv360 2013-02-28 15:03:11

+0

是的,這是我的錯,它的拼寫正確的HTML,我只是寫在我自己。拼寫已被糾正在後 – mhartington 2013-02-28 15:05:24

+0

可能重複[PhoneGap:在Safari中打開外部URL](http://stackoverflow.com/questions/10244965/phonegap-opening-external-urls-in-safari) – 2013-02-28 15:12:27

回答

9

最後能夠通過導航到MainviewController.m和尋找一個部分做在其他職位提到然後從該

/* Comment out the block below to over-ride */ 

/* 

- (void) webViewDidStartLoad:(UIWebView*)theWebView 
{ 
    return [super webViewDidStartLoad:theWebView]; 
} 

- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error 
{ 
    return [super webView:theWebView didFailLoadWithError:error]; 
} 

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

將其更改爲這個

/** 

* Start Loading Request 

* This is where most of the magic happens... We take the request(s) and process the response. 

* From here we can re direct links and other protocalls to different internal methods. 

*/ 

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

{ 

    NSURL *url = [request URL]; 

    // add any other schemes you want to support, or perform additional 

    // tests on the url before deciding what to do -jm 

    if([[url scheme] isEqualToString:@"http"] || 

     [[url scheme] isEqualToString:@"https"]) 

    { 

     [[UIApplication sharedApplication] openURL:url]; 

     return NO; 

    } 

    else 

    { 

     return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ]; 

    } 



} 
它提到web視圖

我沒有Objective-C的經驗,所以我不得不嘗試這個,所以我很高興我能夠實現它。

1

尼斯幫了我一點,但automtically 打開鏈接通過將:

if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
} 

它的工作,現在當用戶點擊一個網址爲http://或https://它它在Safari的

所以完全我得到這個代碼打開:

if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
if([[url scheme] isEqualToString:@"http"] || 

    [[url scheme] isEqualToString:@"https"]) 

{ 

    [[UIApplication sharedApplication] openURL:url]; 

    return NO; 

} 

else 

{ 

    return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ]; 

} 
} 
相關問題