0

I have a web view that has a webpage with links at the bottom. I have a way to deal with them being clicked, but all it does is open them into a new safari browser. This is not what I want.I would like for them to stay inside the web view and allow me to navigate them easily like that. Is there a way to do this?

I have tried just using a regular web view, but then the links at the bottom will not work.

Here is what I have so far:

class ViewController: UIViewController, UIWebViewDelegate {


@IBOutlet weak var myWebview: UIWebView!
@IBOutlet weak var returnButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    prettyReturnButton(returnButton)

    let url = NSURL(string: "mywebpage")

    myWebview.delegate = self

    print("Web page:" , url)
    let requestObj = NSURLRequest(URL: url!);
    myWebview.loadRequest(requestObj);
    myWebview.keyboardDisplayRequiresUserAction = true



}

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    switch navigationType {
    case .LinkClicked:
        // Open links in Safari
        //let newRequestObj = NSURLRequest(URL: request.URL!)
       // webView.loadRequest(newRequestObj)
        UIApplication.sharedApplication().openURL(request.URL!)
        return false
    default:
        // Handle other navigation types...
        return true
    }
}
MNM
  • 2,673
  • 6
  • 38
  • 73
  • 1
    it supposed navigate to links within the same webview if you not using delegate – xmhafiz Oct 20 '16 at 03:38
  • If you don't want the links opened in Safari, then why do you intercept the links and open them in Safari? – rmaddy Oct 20 '16 at 03:39

2 Answers2

3

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 :

Its my webView Output load url

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

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

.plist file code

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
2

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!

KGJ
  • 76
  • 3