Xcode 6 + iOS 8 SDK,但在iOS 7上部署(UIWebKit& WKWebKit) [英] Xcode 6 + iOS 8 SDK but deploy on 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有正确的答案。我正在为其他人添加更多信息。
首先,转到项目
,点击在常规
上,向下滚动到链接的框架和库
,并添加 WebKit.framework
作为可选。 (我也是为 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
上课。它仍然导入 UIKit
和 WebKit
。但 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)
Objective-C:
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:
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
}
这篇关于Xcode 6 + iOS 8 SDK,但在iOS 7上部署(UIWebKit& WKWebKit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!