From 96baae1e03e773b171cd777d0cd552036fb7d315 Mon Sep 17 00:00:00 2001 From: Eric Busch Date: Sat, 12 Dec 2015 13:39:37 -0600 Subject: [PATCH] Support for Swift 2.1 and iOS 9.2 --- FLWebViewSwift/Base.lproj/LaunchScreen.xib | 11 ++++---- FLWebViewSwift/Base.lproj/Main.storyboard | 8 +++--- FLWebViewSwift/FLUIWebView.swift | 11 +++++--- FLWebViewSwift/FLWKWebView.swift | 19 ++++++-------- FLWebViewSwift/FLWebViewProvider.swift | 5 ++-- .../AppIcon.appiconset/Contents.json | 5 ++++ FLWebViewSwift/Info.plist | 4 +++ FLWebViewSwift/ViewController.swift | 25 +++++++------------ 8 files changed, 46 insertions(+), 42 deletions(-) diff --git a/FLWebViewSwift/Base.lproj/LaunchScreen.xib b/FLWebViewSwift/Base.lproj/LaunchScreen.xib index 73bdc2d..083da0a 100644 --- a/FLWebViewSwift/Base.lproj/LaunchScreen.xib +++ b/FLWebViewSwift/Base.lproj/LaunchScreen.xib @@ -1,7 +1,8 @@ - + - + + @@ -11,16 +12,16 @@ - diff --git a/FLWebViewSwift/Base.lproj/Main.storyboard b/FLWebViewSwift/Base.lproj/Main.storyboard index c2ee5bc..0b4d67d 100644 --- a/FLWebViewSwift/Base.lproj/Main.storyboard +++ b/FLWebViewSwift/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -14,9 +14,9 @@ - + - + diff --git a/FLWebViewSwift/FLUIWebView.swift b/FLWebViewSwift/FLUIWebView.swift index 2e274fb..d705b88 100644 --- a/FLWebViewSwift/FLUIWebView.swift +++ b/FLWebViewSwift/FLUIWebView.swift @@ -13,7 +13,7 @@ extension UIWebView: FLWebViewProvider { delegate = viewController } - func canNavigateBackward() -> Bool { + func canNavigateBack() -> Bool { return self.canGoBack } @@ -30,12 +30,15 @@ extension UIWebView: FLWebViewProvider { return self.request?.URL } - func evaluateJS(javascriptString: String!, completionHandler: (AnyObject, NSError) -> ()) { + func evaluateJavaScriptString(javascriptString: String!, completionHandler: (AnyObject?, NSError?) -> ()) { // Have the WebView evaluate the javascript string - let string = stringByEvaluatingJavaScriptFromString(javascriptString) + guard let string = stringByEvaluatingJavaScriptFromString(javascriptString) else { + completionHandler(nil, nil) + return + } // Call the completion handler from there - completionHandler(string!, NSError(domain: "FLWebView", code: 200, userInfo: nil)) + completionHandler(string, nil) } func setScalesPageToFit(setPages: Bool!) { diff --git a/FLWebViewSwift/FLWKWebView.swift b/FLWebViewSwift/FLWKWebView.swift index 2bf0cc0..5cfc99d 100644 --- a/FLWebViewSwift/FLWKWebView.swift +++ b/FLWebViewSwift/FLWKWebView.swift @@ -14,15 +14,14 @@ extension WKWebView: FLWebViewProvider { return objc_getAssociatedObject(self, associatedObjectKey()) as? NSURLRequest } set(newValue) { - objc_setAssociatedObject(self, associatedObjectKey(), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + objc_setAssociatedObject(self, associatedObjectKey(), newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) } } // A simple convenience initializer, this allows for WKWebView(delegateView:) initialization - convenience init(delegateView: AnyObject) { - self.init() - self.UIDelegate = delegateView as? WKUIDelegate - self.navigationDelegate = delegateView as? WKNavigationDelegate + convenience init(delegateView: ViewController) { + self.init(frame: delegateView.view.frame, configuration: WKWebViewConfiguration()) + setDelegateViews(delegateView) } // We will need to set both the UIDelegate AND navigationDelegate in the case of WebKit @@ -35,7 +34,7 @@ extension WKWebView: FLWebViewProvider { return self.URL } - func canNavigateBackward() -> Bool { + func canNavigateBack() -> Bool { return self.canGoBack } @@ -48,10 +47,8 @@ extension WKWebView: FLWebViewProvider { self.loadRequest(NSURLRequest(URL: NSURL(string: urlNameAsString)!)) } - // Pass this up the chain and let WebKit handle it - func evaluateJS(javascriptString: String!, completionHandler: (AnyObject, NSError) -> ()) { - self.evaluateJavaScript(javascriptString, completionHandler: { (AnyObject, NSError) -> Void in - - }) + func evaluateJavaScriptString(javascriptString: String!, completionHandler: (AnyObject?, NSError?) -> ()) { + evaluateJavaScript(javascriptString, completionHandler: completionHandler) } + } diff --git a/FLWebViewSwift/FLWebViewProvider.swift b/FLWebViewSwift/FLWebViewProvider.swift index d6026ed..c4bb002 100644 --- a/FLWebViewSwift/FLWebViewProvider.swift +++ b/FLWebViewSwift/FLWebViewProvider.swift @@ -10,9 +10,10 @@ protocol FLWebViewProvider: class { func loadRequestFromString(urlNameAsString: String!) - func canNavigateBackward() -> Bool + func canNavigateBack() -> Bool func canNavigateForward() -> Bool - func evaluateJS(javascriptString: String!, completionHandler: (AnyObject, NSError) -> ()) + func evaluateJavaScriptString(javascriptString: String!, completionHandler: (AnyObject?, NSError?) -> ()) + } \ No newline at end of file diff --git a/FLWebViewSwift/Images.xcassets/AppIcon.appiconset/Contents.json b/FLWebViewSwift/Images.xcassets/AppIcon.appiconset/Contents.json index 36d2c80..eeea76c 100644 --- a/FLWebViewSwift/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/FLWebViewSwift/Images.xcassets/AppIcon.appiconset/Contents.json @@ -59,6 +59,11 @@ "idiom" : "ipad", "size" : "76x76", "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" } ], "info" : { diff --git a/FLWebViewSwift/Info.plist b/FLWebViewSwift/Info.plist index 40c6215..e4eb011 100644 --- a/FLWebViewSwift/Info.plist +++ b/FLWebViewSwift/Info.plist @@ -30,6 +30,10 @@ armv7 + NSAppTransportSecurity + + NSAllowsArbitraryLoads + UISupportedInterfaceOrientations UIInterfaceOrientationPortrait diff --git a/FLWebViewSwift/ViewController.swift b/FLWebViewSwift/ViewController.swift index 814b321..56775d7 100644 --- a/FLWebViewSwift/ViewController.swift +++ b/FLWebViewSwift/ViewController.swift @@ -4,33 +4,26 @@ import WebKit class ViewController: UIViewController, UIWebViewDelegate, WKNavigationDelegate, WKUIDelegate { var webView: FLWebViewProvider? - - override func viewDidLoad() { - super.viewDidLoad() - + + override func viewDidAppear(animated: Bool) { + super.viewDidAppear(animated) // Check if WKWebView is available if #available(iOS 8.0, *) { let w = WKWebView(delegateView: self) - w.frame = self.view.frame + w.frame = CGRect(origin: CGPointZero, size: self.view.frame.size) self.webView = w - self.view.addSubview(self.webView as! WKWebView) } else { - // In this case we have to fall back on UIWebView + // Fallback on earlier versions let w = UIWebView(delegateView: self) - w.frame = self.view.frame + w.frame = CGRect(origin: CGPointZero, size: self.view.frame.size) self.webView = w - self.view.addSubview(self.webView as! UIWebView) } + self.view.addSubview(self.webView as! UIView) + // Load a page, in this case we will load our favorite website - self.webView?.loadRequestFromString("https://www.google.com/") + self.webView?.loadRequestFromString("http://www.gowithfloat.com") } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - - }