-
-
Save mrsweaters/ac24d7cd988814b92bd7 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 Foundation | |
import UIKit | |
import Webkit | |
class BasicChildBrowser:UIViewController, WKNavigationDelegate { | |
let web = WKWebView() | |
let toolbar = UIToolbar() | |
let progressSpinner = UIActivityIndicatorView(activityIndicatorStyle: .White) | |
let progressBar = UIProgressView(progressViewStyle: .Bar) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
web.frame = CGRect(x: 0, y: 22, width: view.frame.width, height: view.frame.height-66) | |
web.autoresizingMask = .FlexibleWidth | .FlexibleHeight | |
web.navigationDelegate = self | |
toolbar.frame = CGRect(x: 0, y: view.frame.height-44, width: view.frame.width, height: 44) | |
toolbar.autoresizingMask = .FlexibleTopMargin | .FlexibleRightMargin | .FlexibleWidth | |
progressSpinner.frame = CGRect(x: 0, y: 0, width: 30, height: 30) | |
progressBar.frame = CGRect(x: 0, y: 20, width: view.frame.width, height: 2) | |
progressBar.alpha = 0 | |
progressBar.tintColor = UIColor.redColor() | |
progressBar.autoresizingMask = .FlexibleWidth | |
var progress = UIBarButtonItem(customView: progressSpinner) | |
var flex = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) | |
var close = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "closeWebview") | |
close.tintColor = UIColor.redColor() | |
toolbar.setItems([progress,flex,close], animated: true) | |
toolbar.barTintColor = UIColor.blackColor() | |
toolbar.barStyle = UIBarStyle.Black | |
view.addSubview(progressBar) | |
view.addSubview(web) | |
view.addSubview(toolbar) | |
view.backgroundColor = UIColor.whiteColor() | |
} | |
func closeWebview(){ | |
dismissViewControllerAnimated(true, nil) | |
} | |
func loadURL(url:String) { | |
web.loadRequest(NSURLRequest(URL: NSURL(string: url))) | |
} | |
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation) { | |
progressSpinner.startAnimating() | |
self.progressBar.setProgress(0, animated: true) | |
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseInOut, animations: { self.progressBar.alpha = 1 }, nil) | |
UIApplication.sharedApplication().networkActivityIndicatorVisible = true | |
} | |
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation){ | |
progressBar.setProgress(webView.estimatedProgress.bridgeToObjectiveC().floatValue, animated: true) | |
} | |
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation) { | |
progressSpinner.stopAnimating() | |
progressBar.setProgress(1, animated: true) | |
UIView.animateWithDuration(0.3, delay: 1, options: .CurveEaseInOut, animations: { self.progressBar.alpha = 0 }, completion: nil) | |
UIApplication.sharedApplication().networkActivityIndicatorVisible = false | |
} | |
func webView(webView: WKWebView, navigation: WKNavigation, withError error: NSError) { | |
progressSpinner.stopAnimating() | |
progressBar.setProgress(1, animated: true) | |
UIView.animateWithDuration(0.3, delay: 1, options: .CurveEaseInOut, animations: { self.progressBar.alpha = 0 }, completion: nil) | |
UIApplication.sharedApplication().networkActivityIndicatorVisible = false | |
let alert:UIAlertController = UIAlertController(title: "Error", message: "Could not load webpage", preferredStyle: .Alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, nil)) | |
presentViewController(alert, animated: true) { | |
self.dismissViewControllerAnimated(true, nil) | |
} | |
} | |
override func prefersStatusBarHidden() -> Bool { | |
return false | |
} | |
} |
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
The MIT License (MIT) | |
Copyright (c) 2014 Aaron Rosenbaum | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment