iOS WebView - 在SFSafariViewController中打开链接

时间:2016-06-13 20:32:04

标签: ios swift webview sfsafariviewcontroller

所以我试图检测webView中的链接点击,然后在新的SFSafariViewController中显示此网址,但是我遇到了一些问题。

有人有任何想法吗?

这就是我目前正在做的事情:

import UIKit
import SafariServices

class ViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let localfilePath = NSBundle.mainBundle().URLForResource("index", withExtension: "html");
        let myRequest = NSURLRequest(URL: localfilePath!);
        webView.loadRequest(myRequest);

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func loadInView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) {

        if (navigationType == UIWebViewNavigationType.LinkClicked){
            let url = request.URL;

                let sfViewController = SFSafariViewController(URL:url!, entersReaderIfAvailable: true)

                self.presentViewController(sfViewController, animated: true, completion: { () -> Void in
                    print("May the force be with you!")
                })
        }

    }

}

0 个答案:

没有答案