Xcode 6 + iOS 8 SDK, but deployment on iOS 7 (UIWebKit & WKWebKit)

We are creating an application using Xcode 6 beta 5 + Swift on the iOS 8 SDK. We would also like to install iOS 7. Is this possible? When we set the project deployment target to 7.0, we get compile-time errors as follows:

Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_WKPreferences", referenced from: __TMaCSo13WKPreferences in WebViewController.o "_OBJC_CLASS_$_WKWebView", referenced from: __TMaCSo9WKWebView in WebViewController.o "_OBJC_CLASS_$_WKWebViewConfiguration", referenced from: __TMaCSo22WKWebViewConfiguration in WebViewController.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

I believe that since we use WKWebKit , which is only supported by iOS 8+. We are fine using UIWebKit for iOS 7, but WKWebKit for iOS 8. How do we define this?

The definition of our class is as follows:

 import WebKit class WebViewController: UIViewController, WKNavigationDelegate { ... } 

and it is called:

  let mainStoryboard = UIStoryboard(name: "Main", bundle: nil) let destinationVC = mainStoryboard.instantiateViewControllerWithIdentifier("WebViewController") as WebViewController presentViewController(destinationVC, animated: true, completion: nil) 

I was thinking about using this snippet to call presentViewController , but this does not solve the compile-time problem. ( NSFoundationVersionNumber also does not solve compilation problems)

  if ((UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8.0) { } else { } 

UPDATE: kkoltzau has the correct answer. I am adding additional information to others.

First go to Project , click General , scroll down to Linked Frameworks and Libraries and add WebKit.framework as optional. (I also did this for UIKit.framework ) See screenshot:

enter image description here

As for my WebViewController class. It still imports UIKit and WebKit . but viewDidLoad() sets up a view based on the kkoltzau example. Then, when I need to load / reload a web page, it checks for the existence of wkWebView .

+16
ios xcode ios7 ios8 swift
Aug 16 '14 at
source share
2 answers

You will need to check if WKWebView is available, and return to UIWebView if it is not.

Make sure you have a weak WebKit.framework link (set to optional)

Objective-C:

 WKWebView *wkWebView = nil; UIWebView *uiWebView = nil; Class wkWebViewClass = NSClassFromString(@"WKWebView"); if(wkWebViewClass) { WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; // ... wkWebView = [[wkWebViewClass alloc] initWithFrame:frame configuration:config]; [self.view addSubview:wkWebView]; } else { uiWebView = [[UIWebView alloc] initWithFrame:frame]; [self.view addSubview:uiWebView]; } 

Swift:

 var wkWebView : WKWebView? var uiWebView : UIWebView? if NSClassFromString("WKWebView") { let config = WKWebViewConfiguration() // ... wkWebView = WKWebView(frame: frame, configuration: config) self.view.addSubview(wkWebView) } else { uiWebView = UIWebView(frame: frame) self.view.addSubview(uiWebView) } 

Then elsewhere in your code:

Objective-C:

 if(wkWebView) { // WKWebView specific code } else { // UIWebView specific code } 

Swift:

 if let w=wkWebView { // WKWebView specific code } else if let w=uiWebView { // UIWebView specific code } 
+17
Aug 16 '14 at 16:25
source share

If you need to support iOS7, you cannot use the WebKit2 ( WK* ) classes, or you need to implement the logic twice, once for iOS8 using WK* and once using UIWeb* , and at runtime select according to the version of the operating system.

+2
Aug 16 '14 at 2:56
source share



All Articles