Ошибка WKWebView: "Запрошенный URL не найден на этом сервере"
В xcode 7 beta3
Когда я использую object-c, все в порядке, но когда я использую swift, я получаю сообщение об ошибке: "Запрошенный URL не найден на этом сервере", Почему?
вот код:
#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()
@property(strong,nonatomic) WKWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
_webView = [[WKWebView alloc] initWithFrame:self.view.frame];
[_webView loadRequest:request];
[self.view addSubview:_webView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
swift:
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate {
var webview: WKWebView?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webview = WKWebView(frame: self.view.frame)
self.webview!.navigationDelegate = self
let req = NSURLRequest( URL: NSURL( fileURLWithPath: "http://www.apple.com"))
webview?.loadRequest(req)
self.view.addSubview(webview!)
}
// MARK: WKNavigationDelegate
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) {
NSLog("commit")
}
func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) {
NSLog("err: " + error.localizedDescription)
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
NSLog("err2: " + error.localizedDescription)
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
NSLog("finish")
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
NSLog("start")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
1 ответ
Используйте этот код:
let req = NSURLRequest(URL: NSURL(string: "http://www.apple.com")!)
Вместо этого:
let req = NSURLRequest( URL: NSURL( fileURLWithPath: "http://www.apple.com"))
И это будет работать нормально.