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

后端 未结 2 1173
走了就别回头了
走了就别回头了 2020-12-13 10:27

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

相关标签:
2条回答
  • 2020-12-13 11:02

    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.

    0 讨论(0)
  • 2020-12-13 11:09

    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
    }
    
    0 讨论(0)
提交回复
热议问题