Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView and WKWebView

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
like image 459
Nicoll Avatar asked Dec 23 '14 06:12

Nicoll


People also ask

What does mobile Safari UI WKWebView mean?

In the simplest terms, WKWebView allows mobile app developers to integrate mobile web content into their User Interface through the use of web views. The WKWebView API renders a complete mobile browser experience within the app itself allowing users to interact with web content while remaining in the app.

Is WKWebView same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

What is Apple UIWebView?

A view that embeds web content in your app.

Is UIWebView deprecated?

As of iOS 12.0, 'UIWebView' has been explicitly marked as deprecated. Apple is encouraging developers to adopt WKWebView instead, which was introduced in iOS 8.0.


1 Answers

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)

like image 127
Jochen Avatar answered Nov 09 '22 02:11

Jochen