Xcode 6 + iOS 8 SDK 但部署在 iOS 7 (UIWebKit & WKWebKit) [英] Xcode 6 + iOS 8 SDK but deploy on iOS 7 (UIWebKit & WKWebKit)

查看:13
本文介绍了Xcode 6 + iOS 8 SDK 但部署在 iOS 7 (UIWebKit & WKWebKit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在 iOS 8 SDK 上使用 Xcode 6 beta 5 + Swift 创建一个应用程序.我们也想部署到 iOS 7.那可能吗?当我们将项目的部署目标设置为 7.0 时,会出现如下编译时错误:

We are creating an app using Xcode 6 beta 5 + Swift on iOS 8 SDK. We'd like to deploy to iOS 7 as well. Is that possible? When we set the deployment target of the project to 7.0, we get compile time errors like this:

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)

我相信这是因为我们使用的是 WKWebKit,它仅受 iOS 8+ 支持.我们可以在 iOS 7 上使用 UIWebKit 而在 iOS 8 上使用 WKWebKit.我们如何定义?

I believe that's because we are using WKWebKit, which is supported by iOS 8+ only. We are ok with using UIWebKit for iOS 7 but WKWebKit for iOS 8. How do we define that?

我们的类定义看起来像这样...

Our class definition looks like this...

import WebKit

class WebViewController: UIViewController, WKNavigationDelegate {
   ...
}

它的调用者是:

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

我正在考虑使用这个片段来调用 presentViewController 但这并没有解决编译时间问题.(NSFoundationVersionNumber 也不能解决编译时问题)

I was thinking about using this fragment to call presentViewController but that doesn't solve the compile time issues. (NSFoundationVersionNumber doesn't solve compile time issues either)

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

    } else {

    }

更新:kkoltzau 有正确答案.我正在为其他人添加更多信息.

首先,转到您的 Project,单击 General,向下滚动到 Linked Frameworks and Libraries,然后添加 WebKit.框架 作为可选.(我也为 UIKit.framework 做了)看截图:

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

至于我的 WebViewController 类.它仍然导入 UIKitWebKit.但是 viewDidLoad() 根据 kkoltzau 的示例设置视图.然后每当我需要加载/重新加载网页时,它都会检查 wkWebView 是否存在.

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

推荐答案

您需要检查 WKWebView 是否可用,如果不可用则回退到 UIWebView.

You will need to check if WKWebView is available, and fall back to UIWebView if its not.

确保 WebKit.framework 的弱链接(设置为可选)

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

目标-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];
}

斯威夫特:

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:

目标-C:

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

斯威夫特:

if let w=wkWebView {
    // WKWebView specific code
}
else if let w=uiWebView {
    // UIWebView specific code
}

这篇关于Xcode 6 + iOS 8 SDK 但部署在 iOS 7 (UIWebKit & WKWebKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆