Skip to content

Instantly share code, notes, and snippets.

@xcdx80
Created January 16, 2015 14:21
Show Gist options
  • Save xcdx80/f5841db5ec724cdc8f18 to your computer and use it in GitHub Desktop.
Save xcdx80/f5841db5ec724cdc8f18 to your computer and use it in GitHub Desktop.
WKWebView
// Main ViewController (ViewController.swift)
import UIKit
class ViewController: UIViewController {
//Changed the class and variable names to hide the company's and project's identity
let myAPI: MyCustomAPI = MyCustomAPI()
override func viewDidLoad() {
super.viewDidLoad()
myAPI.renderWidget("widget1", onView: self.view)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func loadView() {
super.loadView()
}
}
// MyCustomAPI.swift
import Foundation
import UIKit
import WebKit
struct MyCustomAPI: SomeProtocol {
//SomeProtocol only specifies this function
func renderWidget(id: String, onView view: UIView) {
let model: WidgetModel = WidgetModel(name: "Example Widget", source: "index.html", type: "web", id: id)
let WebViewClient = MyWebViewClient(model: model, containerView: view)
let widget = WebViewClient.view
view.addSubview(widget)
}
}
//WebViewclient.swift
import Foundation
import WebKit
import UIkit
class MyWebViewClient: UIViewController, WKNavigationDelegate {
private let widgetModel: WidgetModel? = nil
private let containerView: UIView? = nil
var webview: WKWebView!
func loadURLWithString(URLString: String) {
if let URL = NSURL(fileURLWithPath: URLString) {
loadURL(URL)
}
}
func loadURL(URL: NSURL) {
self.webview.loadRequest(NSURLRequest(URL: URL))
}
// MARK: WKNavigationDelegate
func webView(webView: WKWebView, didCommitNavigation navigation: WKNavigation!) {
NSLog("commit")
}
func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) {
showError(error.localizedDescription)
}
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
showError(error.localizedDescription)
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
NSLog("finish")
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
NSLog("start")
}
// MARK: Some Private Methods
private func showError(errorString: String?) {
var alertView = UIAlertController(title: "Error", message: errorString, preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertView, animated: true, completion: nil)
}
// MARK: Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.webview = WKWebView(frame: self.view.frame, configuration: WKWebViewConfiguration())
//I think my problem is here
self.webview.navigationDelegate = self
self.view.addSubview(self.webview)
//This will send the path to a local .html file that will be loaded inside the webview
loadURLWithString(NSBundle.mainBundle().pathForResource((self.widgetModel?.source)!, ofType: nil, inDirectory: "widgets/\((self.widgetModel?.id)!)")!)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: Initializers
init(model: WidgetModel, containerView: UIView) {
super.init()
self.widgetModel = model
self.containerView = containerView
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment