在 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.
}
}