2014-12-23 92 views
6

我想用WKWebView爲iOS8上,但還需要IOS7兼容性,我見過的帖子提到使用此代碼:的UIWebView和WKWebView

if ([WKWebView class]) { 
// do new webview stuff 
} 
else { 
// do old webview stuff 
} 

但不知道我在做什麼錯了,因爲下面的代碼給了我一個鏈接錯誤:

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) 

任何幫助非常感謝,這是我的代碼:

.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 

.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 
+0

我想你不能在這種情況下使用IBOutlet。如果您將WKWebView添加到xib或故事板,則iOS 8下無法加載xib或故事板。 – Ryan

+0

如何解決此問題? – Nicoll

+0

怎樣通過代碼分配一個webView。不與xib或故事板。 – Ryan

回答

43

轉到您的Project,點擊General,向下滾動到Linked Frameworks and Libraries,並添加WebKit.framework作爲可選。看到這裏:Xcode 6 + iOS 8 SDK but deploy on iOS 7 (UIWebKit & WKWebKit)

+1

這真棒回答:) – Dilip

+1

這應該被認爲是正確的答案 - 有這個問題我自己,並迅速修復它,因爲這個答案。 :) –

+1

非常感謝。你節省了我的時間。 :) –

相關問題