Reputation: 529
In my SWIFT project, I use WKWebView for iOS 8 and fall back to UIWebView for iOS 7, using this line of code:
var webView: WKWebView?
@IBOutlet weak var containerView: UIWebView!
However, this is not allowed in SWIFT 2 in the new Xcode 7. What is the best alternative if I still want to support iOS 7?
Upvotes: 1
Views: 680
Reputation: 529
Here's the example code for just the purpose.
var subWKView: UIView?
//on view load, initiate the WKWebView and fall back to UIWebView
override func loadView() {
if #available(iOS 8.0, *) {
//this is only allowed to be used in the func block
let webView: WKWebView!
webView = WKWebView()
//code for initiating WKWebView using addSubview
//reference to the WKWebView
self.subWKView = webView
} else {
//use UIView
}
}
//Use WKWebView in other func blocks
func useWKWebView() {
if #available(iOS 8.0, *) {
//cast to WKWebView
let webView = self.subWKView as! WKWebView
//use the webView
} else {
//use UIView
}
}
Upvotes: 0
Reputation: 21
I had the same problem. I added this dictionary to my info.plist. Now the WKWebView loads as expected.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Upvotes: 1