Xcode 6 + iOS 8 SDK 但部署在 iOS 7 (UIWebKit & WKWebKit)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25341238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:23:57  来源:igfitidea点击:

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

iosxcodeios7swiftios8

提问by Dean

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:

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

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's because we are using WKWebKit, which is supported by iOS 8+ only. We are ok with using UIWebKitfor iOS 7 but WKWebKitfor iOS 8. How do we define that?

我相信那是因为我们正在使用WKWebKit,它仅受 iOS 8+ 支持。我们可以UIWebKit用于 iOS 7 但WKWebKit用于 iOS 8。我们如何定义它?

Our class definition looks like this...

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

import WebKit

class WebViewController: UIViewController, WKNavigationDelegate {
   ...
}

and it's called by:

它被称为:

        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 fragment to call presentViewControllerbut that doesn't solve the compile time issues. (NSFoundationVersionNumberdoesn't solve compile time issues either)

我正在考虑使用此片段进行调用,presentViewController但这并不能解决编译时问题。(NSFoundationVersionNumber也不能解决编译时问题)

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

    } else {

    }

UPDATE: kkoltzau has the correct answer. I'm adding some more info for others.

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

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

首先,转到您的Project,单击General,向下滚动到Linked Frameworks and Libraries,然后添加WebKit.framework为 Optional。(我也这样做了UIKit.framework)见截图:

enter image description here

enter image description here

As for my WebViewControllerclass. It still imports UIKitand 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.

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

采纳答案by kevin

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

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

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

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

Objective-C:

目标-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:

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

回答by Leo Natan

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

如果需要支持iOS7,则不能使用WebKit2( WK*)类,或者需要实现两次逻辑,一次为iOS8使用WK*,一次使用UIWeb*,运行时根据操作系统版本进行选择。