2015-09-22 84 views
0

我想在加載WebView時獲取錯誤狀態碼。 當我打開了連接我用這個代碼:獲取錯誤狀態碼webview swift 2

let simpler = "http://www.example.com" 
let url = NSURL(string: simpler) 
let request = NSURLRequest(URL: url!) 
webview.loadRequest(request) 

我發現這個代碼,但我得到錯誤,所以它不工作:

Check URL of Loaded WebView

我想在viewDidLoad中的錯誤

+1

檢查出UIWebViewDelegate的didFailLoadWithError – Gruntcakes

+0

我想看看如果在webview中我獲得404,501錯誤等。 – ewan

回答

0

試試這個: -

 var url:NSURL = NSURL(string: "http://www.example.com")! 

     var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)! 

     var postLength:NSString = String(postData.length) 

     var request:NSMutableURLRequest = NSMutableURLRequest(URL: url) 
     request.HTTPMethod = "POST" 
     request.HTTPBody = postData 
     //request.setValue(postLength as String, forHTTPHeaderField: "Content-Length") 
     request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 
     request.setValue("*/*", forHTTPHeaderField: "Accept") 


     var reponseError: NSError? 
     var response: NSURLResponse? 

     var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError) 

      let res = response as! NSHTTPURLResponse!; 

      NSLog("Response code: %ld", res.statusCode);