Skip to content

Instantly share code, notes, and snippets.

@Bigigrammer
Created April 16, 2017 06:30
Show Gist options
  • Save Bigigrammer/0b29249f7ba10264a1d76251ccc1e5d5 to your computer and use it in GitHub Desktop.
Save Bigigrammer/0b29249f7ba10264a1d76251ccc1e5d5 to your computer and use it in GitHub Desktop.
Example of how to use CustomWKWebView.
import UIKit
import WebKit
class ViewController: UIViewController, CustomWKWebDelegate, WKNavigationDelegate, URLSessionDownloadDelegate {
var testWeb: CustomWKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let webFrameY: CGFloat = 20
let webFrameWidth = self.view.frame.width
let webFrameHeight = self.view.frame.height - webFrameY
let webFrame = CGRect(x: 0, y: webFrameY, width: webFrameWidth, height: webFrameHeight)
testWeb = CustomWKWebView(frame: webFrame)
testWeb.delegate = self
testWeb.navigationDelegate = self
self.view.addSubview(testWeb)
testWeb.loadURL(url: "http://www.google.com")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
testWeb.getIconImage()
}
func urlDidGet(customWebView: CustomWKWebView, arrURL: [URL]) {
if arrURL.isEmpty == false{
for temp in arrURL{
download(url: temp)
}
}
}
func download(url: URL?){
guard let url = url else{print("canceled"); return}
let defaultSession = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
let task = defaultSession.downloadTask(with: url)
task.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Downloaded: \(location)")
var image: UIImage?
do{
let data = try Data(contentsOf: location)
image = UIImage(data: data)
}catch let error as NSError{
print(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment