How to load a url link that is inside a web view and keep it in that web view in SWIFT

放肆的年华 提交于 2019-11-29 18:03:49

In the webview delegate you are going to want to navigate to the new url using that same webview.

For you this means that inside your switch case you should use loadRequest

loadRequest documentation

To fix your issue you should remove the line using UIApplication.sharedApplication().openURL(request.URL!)

and replace it with a call to loadRequest

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    switch navigationType {

    case .LinkClicked:
        // Open links in Safari

        guard let newRequest = request as? URLRequest else { return false }

        webView.loadRequest(newRequest)
        return false
    default:
        // Handle other navigation types...
        return true
   }
}

Hope this helps! Have a nice day!

Give delegates to your UIViewcontroller : UIWebViewDelegate

 let url = NSURL(string: "http://www.djmazacool.com")
 let req = NSURLRequest(URL: url!)
 YOURWEBVIEW.delegate = self
 YOURWEBVIEW.loadRequest(req)



func webViewDidStartLoad(webView: UIWebView)
{
        // start animation
        print("start loaded call")
}
func webViewDidFinishLoad(webView: UIWebView)
{
        // load url complete
        print("loading done call")
}

Output :

Don't Forgot to add this attribute in .plist file.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

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