7

I want to use WKWebView for IOS8 but also need compatibility for IOS7, I have seen the posts referring to using this code:

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

But not sure what I am doing wrong as the code below gives me a linker error:

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)

Any help much appreciated, here is my code:

.h file:

#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 file:

#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
Nicoll
  • 257
  • 1
  • 5
  • 16
  • I guess you can't use IBOutlet in this case. If you add WKWebView to your xib or storyboard, the xib or storyboard can't be loaded below iOS 8. – Ryan Dec 23 '14 at 06:19
  • How can a get around this please? – Nicoll Dec 23 '14 at 06:21
  • What about allocate one of webView via code. Not with xib or storyboard. – Ryan Dec 23 '14 at 06:25
  • I only have the one webView with the 2 properties (not sure if this is good or not), should I have 2 webViews? Could you give example how to code it without IBOutlet? Thanks you. – Nicoll Dec 23 '14 at 06:29
  • 1
    `id webView; if([WKWebView class]) { webView = [[WKWebView alloc] init]; } else { webView = [[UIWebView alloc] init]; } ` like this. – Ryan Dec 23 '14 at 06:30
  • Thanks, but I am new to this & not sure where I put this in my code plus what to delete from my code, do I delete my two properties & the UIWebView? Sorry! – Nicoll Dec 23 '14 at 06:35

1 Answers1

50

Go to your Project, click on General, scroll down to Linked Frameworks and Libraries, and add WebKit.framework as Optional. See here: Xcode 6 + iOS 8 SDK but deploy on iOS 7 (UIWebKit & WKWebKit)

Community
  • 1
  • 1
Jochen
  • 566
  • 4
  • 7