2016-03-31 119 views
0

我在我的應用程序中通過WebView加載html字符串。目前的問題是,當我的WebView加載包含另一個鏈接的內容,並且當用戶點擊該鏈接時,新視圖不會在我的應用程序中打開 - 它會在默認瀏覽器中打開。我如何在WebView中打開該鏈接?這裏是我的代碼:如何在webview中打開鏈接,如果它在webview中(swift)

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.edgesForExtendedLayout = UIRectEdge.None 
    SVProgressHUD.show() 
    //EZLoadingActivity.show("أرجو الإنتظار", disableUI: true) 
    self.automaticallyAdjustsScrollViewInsets = false 
    self.navigationController!.navigationBar.tintColor = UIColor.whiteColor() 
    self.navigationController!.navigationBar.barStyle = UIBarStyle.Black 
    self.navigationController!.navigationBar.tintColor = UIColor.whiteColor() 

    let logo = UIImage(named: "toolbar_icon3") 
    let imageView = UIImageView(image:logo) 
    self.navigationItem.titleView = imageView 

    myweb.loadHTMLString(webviewurl, baseURL: nil) 
} 

的Android允許做它:

public boolean shouldOverrideUrlLoading(WebView view, String url) 

但在iOS上,我們如何才能做到這一點?

+0

他們應該 除非你重寫'shouldStartLoadWithRequest',否則默認是在webview中打開的。請爲您的「有時」案例提供一些示例鏈接 – zcui93

回答

0

你可以嘗試爲你創建一個url請求UIWebView

yourWebView.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!)) 

如果你想做些什麼而Web視圖打開,你可以使用UIWebViewDelegate聲明。

在你的類:

class ViewController: UIViewController, UIWebViewDelegate { 

然後,

yourWebView.delegate = self 

最後,你可以使用:

func webViewDidStartLoad(webView: UIWebView) { 
    //do something while the web view is loading! 
} 

有關UIWebViewDelegate的更多信息,請訪問:Apple's Documentation.