1. 程式人生 > >swift 中跳轉WKWebView,連結含有漢字的寫法

swift 中跳轉WKWebView,連結含有漢字的寫法

import UIKit

class HighSpeedRoadVC: UIViewController,WKNavigationDelegate,UIScrollViewDelegate {

var webView : WKWebView = WKWebView()

//    var web : UIWebView = UIWebView()

overridefunc viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

self.navigationController?.navigationBar

.hidden = false

    }

overridefunc viewWillDisappear(animated: Bool) {

super.viewWillDisappear(animated)

self.webView.scrollView.delegate = nil

    }

overridefunc viewDidLoad() {

super.viewDidLoad()

self.title = "河南高速路況"

var webview : WKWebView = WKWebView.init(frame: CGRectMake(

0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))

        webview.navigationDelegate = self

        webview.scrollView.delegate = self

self.view.addSubview(webview)

let baseUrl = "https://m.weibo.cn/k/"

let str = "河南高速路況?from=feed"

let utf8Str = str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet

.URLQueryAllowedCharacterSet())

let url = NSURL(string: baseUrl + utf8Str!)

let request = NSURLRequest(URL: url!)

        webview.loadRequest(request)

self.webView = webview

// 開啟左劃回退功能:

self.webView.allowsBackForwardNavigationGestures = true

    }

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {

NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "WebKitCacheModelPreferenceKey")

NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WebKitDiskImageCacheEnabled")

NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WebKitOfflineWebApplicationCacheEnabled")

NSUserDefaults.standardUserDefaults().synchronize()

    }

func scrollViewDidScroll(scrollView: UIScrollView) {

if (scrollView.contentOffset.y > 100) {

self.navigationItem.title = "河南高速路況"

        } else {

self.navigationItem.title = "河南高速路況"

        }

    }

//載入錯誤時走的方法

func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) {

print("error:\(error)")

    }

//    func webViewDidFinishLoad(webView: UIWebView) {

//        

//        let url : String = (webView.request?.URL?.absoluteString)!

//        

//        let inpuStr : NSMutableString = NSMutableString.init(string: url)

//        

//        inpuStr.replaceOccurrencesOfString("+", withString: " ", options: .LiteralSearch, range: NSMakeRange(0, inpuStr.length))

//        

//        let sURL : String = inpuStr.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

//        

//        

//    }

//

overridefunc didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

    }

/*

     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

     // Get the new view controller using segue.destinationViewController.

     // Pass the selected object to the new view controller.

     }

     */

}