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 t
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.
You will need to check if WKWebView is available, and fall back to UIWebView if its not.
Make sure you weak link WebKit.framework (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
}