UIWebView和WKWebView

Nic*_*oll 6 xcode objective-c wkwebview

我想在IOS8上使用WKWebView,但也需要IOS7的兼容性,我看过帖子指的是使用这段代码:

if ([WKWebView class]) {
// do new webview stuff
}
else {
// do old webview stuff
}
Run Code Online (Sandbox Code Playgroud)

但不确定我做错了什么,因为下面的代码给我一个链接器错误:

Undefined symbols for architecture i386:
"_OBJC_CLASS_$_WKWebView", referenced from:
  objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢,这是我的代码:

.h文件:

#import "WebKit/WebKit.h"
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIWebView *contentWebView;
@property (weak, nonatomic) IBOutlet WKWebView *contentWKWebView;

@end
Run Code Online (Sandbox Code Playgroud)

.m文件:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController
@synthesize contentWebView;
@synthesize contentWKWebView;

- (void)viewDidLoad {
[super viewDidLoad];

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"LockBackground" ofType:@"html"];
NSURL * fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSURLRequest * myNSURLRequest = [[NSURLRequest alloc]initWithURL:fileURL];
if ([WKWebView class]) {
    [contentWKWebView loadRequest:myNSURLRequest];
} else {
    [contentWebView loadRequest:myNSURLRequest];
}
}

-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
Run Code Online (Sandbox Code Playgroud)

小智 47

转到您的Project,点击General,向下滚动Linked Frameworks and Libraries,然后添加WebKit.framework为可选.看到这里:Xcode 6 + iOS 8 SDK,但在iOS 7上部署(UIWebKit和WKWebKit)