-
-
Save dakeshi/6915b6681cbc29327dcc9b27cef263b8 to your computer and use it in GitHub Desktop.
| /// In AppDelegate.swift | |
| /// Execute remove webdata method when the app receives the memory warning. | |
| /// | |
| /// We can not avoid the infinitely get increased the app size issue | |
| /// when users keep the web searching using the WKWebView. | |
| /// So, you would get iOS full storage message. | |
| /// | |
| /// Set WKWebsiteDataStore.nonPersistentDataStore in the configuration | |
| /// can make lightweight WKWebView. But it can not apply every web site. | |
| /// Imagine the web sites used the localStorage, IndexedDB features. | |
| /// | |
| /// - parameter application: application | |
| func applicationDidReceiveMemoryWarning(application: UIApplication) { | |
| #if DEBUG | |
| print("memory warning...execute remove webviewdata") | |
| #endif | |
| removeWebData() | |
| } | |
| /// Remove web data saved in the app, such as caches, cookies. | |
| func removeWebData() { | |
| if #available(iOS 9.0, *) { | |
| let websiteDataTypes = WKWebsiteDataStore.allWebsiteDataTypes() | |
| let date = NSDate(timeIntervalSince1970: 0) | |
| WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes, modifiedSince: date, completionHandler: { | |
| #if DEBUG | |
| print("remove all data in iOS9 later") | |
| #endif | |
| }) | |
| }else { | |
| // Remove the basic cache. | |
| NSURLCache.sharedURLCache().removeAllCachedResponses() | |
| // Delete system cookie store in the app | |
| let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage() | |
| if let cookies = storage.cookies { | |
| for cookie in cookies { | |
| storage.deleteCookie(cookie) | |
| } | |
| } | |
| do { | |
| // folder 를 삭제하는 대신 contents 를 삭제하는 이유? | |
| // MainVC 가 호출되면 cache, cookie가 발생하므로 로딩시 확인된 cache, cookie 폴더의 | |
| // contents 에 대해서만 삭제 진행. | |
| // Clear web cache | |
| try deleteLibraryFolderContents("Caches") | |
| // Remove all cookies stored by the site. This includes localStorage, sessionStorage, and WebSQL/IndexedDB. | |
| try deleteLibraryFolderContents("Cookies") | |
| // Removes all app cache storage. | |
| try deleteLibraryFolder("WebKit") | |
| } catch { | |
| #if DEBUG | |
| print("Delete library folders error in iOS9 below") | |
| #endif | |
| } | |
| } | |
| } | |
| /** | |
| Delete folder in library | |
| - parameter folder: a folder you want to delete | |
| - throws: throws an error | |
| */ | |
| func deleteLibraryFolder(folder: String) throws { | |
| let manager = NSFileManager.defaultManager() | |
| let library = manager.URLsForDirectory(.LibraryDirectory, inDomains: .UserDomainMask).first! | |
| let dir = library.URLByAppendingPathComponent(folder) | |
| try manager.removeItemAtURL(dir) | |
| } | |
| /** | |
| Delete contents in library folder | |
| - parameter folder: target folder | |
| - throws: throws an error | |
| */ | |
| private func deleteLibraryFolderContents(folder: String) throws { | |
| let manager = NSFileManager.defaultManager() | |
| let library = manager.URLsForDirectory(NSSearchPathDirectory.LibraryDirectory, inDomains: .UserDomainMask)[0] | |
| let dir = library.URLByAppendingPathComponent(folder) | |
| let contents = try manager.contentsOfDirectoryAtPath(dir.path!) | |
| for content in contents { | |
| do { | |
| try manager.removeItemAtURL(dir.URLByAppendingPathComponent(content)) | |
| } catch where ((error as NSError).userInfo[NSUnderlyingErrorKey] as? NSError)?.code == Int(EPERM) { | |
| // "EPERM: operation is not permitted". We ignore this. | |
| #if DEBUG | |
| print("Couldn't delete some library contents.") | |
| #endif | |
| } | |
| } | |
| } |
Dear dakeshi,
You got some great knowledge on WKWebView! I'm trying to load one single WKWebview on the background in my whole app.
When my iOS app opens the a ViewController with it's own class, with 2 textfields and a login button. When the button is pressed, it will fill in the loginform on WKWebView and login.
Then I need to go to the Second viewcontroller with it's OWN class without loding the WKWebView session.
I want to keep the same WKWebView that is logged in. Is it possible to keep the same WKwebview session in more classes?
I don't know How to implement WKProcessPool, could this be a fix?
Is it possible to load a wkwebview in a Classic that can be accessed from anywhere:
Class webviewclass {
var webView = WKWebView()
...
...
init(websiteurl: String()){
}
}
Could you please help me.
Thanks