Created
July 3, 2017 14:56
-
-
Save caseylmanus/be71e88cb6c5696a01629b2ce9e20c12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SystemConfiguration | |
class ViewController: UIViewController, UIWebViewDelegate { | |
@IBOutlet weak var LoadingScreenView: UIView! | |
@IBOutlet weak var ActivityIndicatorView: UIActivityIndicatorView! | |
@IBOutlet weak var WebView: UIWebView! | |
@IBOutlet weak var NetworkErrorView: UIView! | |
var _hasLoaded:Bool = false | |
override func loadView() { | |
setUserAgent() | |
super.loadView(); | |
let reachability = Reachability.reachabilityForInternetConnection() | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability) | |
reachability.startNotifier() | |
NSNotificationCenter.defaultCenter().addObserver(self,selector:"didBecomeActive:",name:UIApplicationDidBecomeActiveNotification, object:nil) | |
} | |
func reachabilityChanged(note: NSNotification) { | |
if !hasConnectivity(){ | |
showNetworkError() | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
WebView.delegate = self | |
WebView.scrollView.bounces = false | |
loadApp() | |
} | |
func didBecomeActive(notification: NSNotification){ | |
if !hasConnectivity(){ | |
showNetworkError() | |
} | |
} | |
func loadApp(){ | |
if hasConnectivity(){ | |
if(_hasLoaded){ | |
showApp() | |
} else { | |
showWaitScreen() | |
let url:NSURL = NSURL(string: "https://rentshare.com/mobile")! | |
let request:NSURLRequest = NSURLRequest(URL: url) | |
WebView.loadRequest(request) | |
} | |
} else { | |
showNetworkError() | |
} | |
} | |
@IBAction func RetryNetwork(sender: AnyObject) { | |
loadApp() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func webViewDidStartLoad(webView: UIWebView) { | |
fixNavigatorStandalone() | |
} | |
func fixNavigatorStandalone(){ | |
//this bit of code is here because the window.navigator.standalone property can't be written in the uiwebview | |
WebView.stringByEvaluatingJavaScriptFromString("var nav = {}; nav.__proto__ = window.navigator.__proto__;for(key in window.navigator) { nav[key] = window.navigator[key];} nav.standalone = true; window.navigator = nav;") | |
} | |
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { | |
var openInApp:Bool = true | |
let url = request.URL.absoluteString!.lowercaseString | |
let host:String = request.URL.host!.lowercaseString | |
if url.rangeOfString("referred_from_mobile=1") != nil { | |
openInApp = false | |
} | |
if navigationType == UIWebViewNavigationType.LinkClicked && host.rangeOfString("rentshare.com") == nil { | |
openInApp = false | |
} | |
if !openInApp { | |
UIApplication.sharedApplication().openURL(request.URL) | |
} | |
return openInApp | |
} | |
func webViewDidFinishLoad(webView: UIWebView) { | |
fixNavigatorStandalone() | |
showApp() | |
} | |
func webView(webView: UIWebView, didFailLoadWithError error: NSError) { | |
showNetworkError() | |
} | |
func showNetworkError(){ | |
NetworkErrorView.hidden = false | |
WebView.hidden = true | |
LoadingScreenView.hidden = true | |
setBackground() | |
} | |
func showApp(){ | |
if(WebView.hidden){ | |
WebView.alpha = 0.0 | |
WebView.hidden = false | |
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { | |
self.LoadingScreenView.alpha = 0.0 | |
}, completion: { finished in | |
self.LoadingScreenView.hidden = true | |
self.LoadingScreenView.alpha = 1.0 | |
}) | |
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { | |
self.WebView.alpha = 1.0 | |
}, completion: nil) | |
NetworkErrorView.hidden = true | |
} | |
self.view.backgroundColor = UIColor.whiteColor() | |
_hasLoaded = true | |
} | |
func showWaitScreen(){ | |
WebView.hidden = true | |
NetworkErrorView.hidden = true | |
LoadingScreenView.hidden = false | |
setBackground() | |
} | |
func hasConnectivity() -> Bool { | |
let reachability: Reachability = Reachability.reachabilityForInternetConnection() | |
return reachability.isReachable(); | |
} | |
override func viewDidLayoutSubviews() { | |
let top:CGFloat = statusBarHeight() | |
WebView.frame = CGRectMake(0, top, self.view.frame.width, self.view.frame.height - top) | |
setBackground() | |
centerView(LoadingScreenView) | |
centerView(NetworkErrorView) | |
} | |
func centerView(view:UIView){ | |
let center:CGPoint = self.view.center | |
view.center = center; | |
} | |
func setBackground(){ | |
let color:UIColor = UIColor(red: 0.753, green: 0.753, blue: 0.753, alpha: 1) | |
self.view.backgroundColor = color | |
} | |
func statusBarHeight() -> CGFloat { | |
let statusBarSize = UIApplication.sharedApplication().statusBarFrame.size | |
return Swift.min(statusBarSize.width, statusBarSize.height) | |
} | |
func setUserAgent() { | |
let web = UIWebView(frame: CGRectZero) | |
var userAgent: String = web.stringByEvaluatingJavaScriptFromString("window.navigator.userAgent")! | |
userAgent = userAgent + " - iOSWebView" | |
println(userAgent) | |
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent":userAgent]) | |
NSUserDefaults.standardUserDefaults().synchronize() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment