Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UIWebView和WKWebView_Objective C_Xcode_Wkwebview - Fatal编程技术网

Objective c UIWebView和WKWebView

Objective c UIWebView和WKWebView,objective-c,xcode,wkwebview,Objective C,Xcode,Wkwebview,我想在IOS8中使用WKWebView,但也需要与IOS7兼容,我已经看到一些帖子提到使用以下代码: if ([WKWebView class]) { // do new webview stuff } else { // do old webview stuff } 但不确定我做错了什么,因为下面的代码给我一个链接器错误: Undefined symbols for architecture i386: "_OBJC_CLASS_$_WKWebView", referenced from:

我想在IOS8中使用WKWebView,但也需要与IOS7兼容,我已经看到一些帖子提到使用以下代码:

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

转到您的
项目
,单击
常规
,向下滚动到
链接的框架和库
,然后添加
WebKit.framework
,作为可选内容。请参见此处:

我想在这种情况下您不能使用IBOutlet。如果将WKWebView添加到您的xib或情节提要中,则无法在iOS 8下加载xib或情节提要。a如何解决此问题?如何通过代码分配一个webView。不适用于xib或故事板。我只有一个具有两个属性的webView(不确定这是否好),我应该有两个webView吗?您能举例说明如何在没有IBOutlet的情况下编写代码吗?谢谢。
id webView;如果([WKWebView类]){webView=[[WKWebView alloc]init];}或者{webView=[[UIWebView alloc]init];}
像这样。这是一个很棒的答案:)这应该被认为是正确的答案-我自己有这个问题,并且由于这个答案很快就解决了。:)
#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