Эх сурвалжийг харах

add WKCookieWebView.swift

Marino Faggiana 6 жил өмнө
parent
commit
60f2b2b2b8

+ 4 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -128,6 +128,7 @@
 		F732BA061D76CE1500E9878B /* CCNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = F732BA041D76CE1500E9878B /* CCNetworking.m */; };
 		F732BA0B1D76DBA500E9878B /* CCNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = F732BA041D76CE1500E9878B /* CCNetworking.m */; };
 		F733B65221997CC2001C1FFA /* TLPhotoPicker.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F733B65121997CC1001C1FFA /* TLPhotoPicker.framework */; };
+		F734A8BF21B59137009DE2E8 /* WKCookieWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F734A8BE21B59137009DE2E8 /* WKCookieWebView.swift */; };
 		F7381EE1218218C9000B1560 /* NCOffline.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7381EDA218218C9000B1560 /* NCOffline.swift */; };
 		F7381EE5218218C9000B1560 /* NCOffline.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7381EDE218218C9000B1560 /* NCOffline.storyboard */; };
 		F738E8421F90FFD100F95C8E /* NCManageEndToEndEncryption.m in Sources */ = {isa = PBXBuildFile; fileRef = F738E8411F90FFD100F95C8E /* NCManageEndToEndEncryption.m */; };
@@ -746,6 +747,7 @@
 		F732BA031D76CE1500E9878B /* CCNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCNetworking.h; sourceTree = "<group>"; };
 		F732BA041D76CE1500E9878B /* CCNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCNetworking.m; sourceTree = "<group>"; };
 		F733B65121997CC1001C1FFA /* TLPhotoPicker.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = TLPhotoPicker.framework; path = Carthage/Build/iOS/TLPhotoPicker.framework; sourceTree = "<group>"; };
+		F734A8BE21B59137009DE2E8 /* WKCookieWebView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WKCookieWebView.swift; sourceTree = "<group>"; };
 		F7381EDA218218C9000B1560 /* NCOffline.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCOffline.swift; sourceTree = "<group>"; };
 		F7381EDE218218C9000B1560 /* NCOffline.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = NCOffline.storyboard; sourceTree = "<group>"; };
 		F738E8401F90FFD100F95C8E /* NCManageEndToEndEncryption.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NCManageEndToEndEncryption.h; sourceTree = "<group>"; };
@@ -2396,6 +2398,7 @@
 			isa = PBXGroup;
 			children = (
 				F7B1FBB01E72E3D1001781FE /* Resources */,
+				F734A8BE21B59137009DE2E8 /* WKCookieWebView.swift */,
 				F7B1FBBF1E72E3D1001781FE /* SwiftModalWebVC.swift */,
 				F7B1FBC01E72E3D1001781FE /* SwiftWebVC.swift */,
 				F7B1FBC11E72E3D1001781FE /* SwiftWebVCActivity.swift */,
@@ -3683,6 +3686,7 @@
 				F762CAF71EACB66200B38484 /* XLFormBaseCell.m in Sources */,
 				F70022E01EC4C9100080073F /* OCXMLListParser.m in Sources */,
 				F70022B31EC4C9100080073F /* OCActivity.m in Sources */,
+				F734A8BF21B59137009DE2E8 /* WKCookieWebView.swift in Sources */,
 				F70022D41EC4C9100080073F /* NSDate+ISO8601.m in Sources */,
 				F762CB151EACB66200B38484 /* XLFormRowNavigationAccessoryView.m in Sources */,
 				F762CB0A1EACB66200B38484 /* XLFormDescriptor.m in Sources */,

+ 0 - 34
iOSClient/Brand/Intro/CCIntro.m

@@ -229,38 +229,4 @@
     [self.intro showInView:self.rootView animateDuration:0];
 }
 
-#pragma --------------------------------------------------------------------------------------------
-#pragma mark ===== Web =====
-#pragma --------------------------------------------------------------------------------------------
-
-- (void)host:(id)sender
-{
-    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
-    
-    SwiftModalWebVC *webVC = [[SwiftModalWebVC alloc] initWithUrlString:[NCBrandOptions sharedInstance].linkLoginHost colorText:[UIColor whiteColor] colorDoneButton:[UIColor blackColor] doneButtonVisible:YES hideToolbar:NO];    
-    webVC.delegateWeb = self;
-    
-    [appDelegate.window.rootViewController presentViewController:webVC animated:YES completion:nil];
-}
-    
-- (void)didStartLoading
-{
-    
-}
-
-- (void)didReceiveServerRedirectForProvisionalNavigationWithUrl:(NSURL *)url
-{
-    
-}
-
-- (void)didFinishLoadingWithSuccess:(BOOL)success url:(NSURL *)url
-{
-    
-}
-
-- (void)webDismiss
-{
-    
-}
-
 @end

+ 4 - 4
iOSClient/Library/SwiftWebVC/SwiftModalWebVC.swift

@@ -21,14 +21,14 @@ public class SwiftModalWebVC: UINavigationController {
     
     weak var webViewDelegate: UIWebViewDelegate? = nil
     
-    @objc public convenience init(urlString: String, colorText: UIColor, colorDoneButton: UIColor, doneButtonVisible: Bool, hideToolbar: Bool = false) {
+    @objc public convenience init(urlString: String, colorText: UIColor, colorDoneButton: UIColor, doneButtonVisible: Bool, hideToolbar: Bool = false, useRedirectCookieHandling:Bool) {
         let url = URL(string: urlString)!
-        self.init(request: URLRequest(url: url), colorText: colorText, colorDoneButton: colorDoneButton, doneButtonVisible: doneButtonVisible, hideToolbar: hideToolbar)
+        self.init(request: URLRequest(url: url), colorText: colorText, colorDoneButton: colorDoneButton, doneButtonVisible: doneButtonVisible, hideToolbar: hideToolbar, useRedirectCookieHandling: useRedirectCookieHandling)
     }
     
-    public init(request: URLRequest, colorText: UIColor = UIColor.white, colorDoneButton: UIColor = UIColor.black, doneButtonVisible: Bool = false, hideToolbar: Bool = false) {
+    public init(request: URLRequest, colorText: UIColor = UIColor.white, colorDoneButton: UIColor = UIColor.black, doneButtonVisible: Bool = false, hideToolbar: Bool = false, useRedirectCookieHandling: Bool) {
         
-        let webViewController = SwiftWebVC(aRequest: request, hideToolbar: hideToolbar)
+        let webViewController = SwiftWebVC(aRequest: request, hideToolbar: hideToolbar, useRedirectCookieHandling: useRedirectCookieHandling)
         webViewController.storedStatusColor = UINavigationBar.appearance().barStyle
         
         super.init(rootViewController: webViewController)

+ 11 - 94
iOSClient/Library/SwiftWebVC/SwiftWebVC.swift

@@ -23,6 +23,7 @@ public class SwiftWebVC: UIViewController {
     var buttonColor: UIColor? = nil
     var titleColor: UIColor? = nil
     var closing: Bool! = false
+    var useRedirectCookieHandling: Bool = false
     
     lazy var backBarButtonItem: UIBarButtonItem =  {
         var tempBackBarButtonItem = UIBarButtonItem(image: SwiftWebVC.bundledImage(named: "SwiftWebVCBack"),
@@ -69,8 +70,8 @@ public class SwiftWebVC: UIViewController {
     }()
     
     
-    lazy var webView: WKWebView = {
-        var tempWebView = WKWebView(frame: UIScreen.main.bounds)
+    lazy var webView: WKCookieWebView = {
+        var tempWebView = WKCookieWebView(frame: UIScreen.main.bounds, configuration: WKWebViewConfiguration(), useRedirectCookieHandling: useRedirectCookieHandling)
         tempWebView.uiDelegate = self
         tempWebView.navigationDelegate = self
         return tempWebView;
@@ -91,16 +92,17 @@ public class SwiftWebVC: UIViewController {
         webView.navigationDelegate = nil;
     }
     
-    public convenience init(urlString: String, hideToolbar: Bool) {
-        self.init(pageURL: URL(string: urlString)!, hideToolbar: hideToolbar)
+    public convenience init(urlString: String, hideToolbar: Bool, useRedirectCookieHandling: Bool) {
+        self.init(pageURL: URL(string: urlString)!, hideToolbar: hideToolbar, useRedirectCookieHandling: useRedirectCookieHandling)
     }
     
-    public convenience init(pageURL: URL, hideToolbar: Bool) {
-        self.init(aRequest: URLRequest(url: pageURL), hideToolbar: hideToolbar)
+    public convenience init(pageURL: URL, hideToolbar: Bool, useRedirectCookieHandling: Bool) {
+        self.init(aRequest: URLRequest(url: pageURL), hideToolbar: hideToolbar, useRedirectCookieHandling: useRedirectCookieHandling)
     }
     
-    public convenience init(aRequest: URLRequest, hideToolbar: Bool) {
+    public convenience init(aRequest: URLRequest, hideToolbar: Bool, useRedirectCookieHandling: Bool) {
         self.init()
+        self.useRedirectCookieHandling = useRedirectCookieHandling
         self.request = aRequest
         self.request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
         let language = NSLocale.preferredLanguages[0] as String
@@ -114,7 +116,7 @@ public class SwiftWebVC: UIViewController {
         
         webView.customUserAgent = userAgent
        
-        webView.load(request)
+        _ = webView.load(request)
     }
     
     ////////////////////////////////////////////////
@@ -334,25 +336,7 @@ extension SwiftWebVC: WKNavigationDelegate {
     }
     
     public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
-        if #available(iOS 11.0, *) {
-            let url = request.url!.host!
-            webView.loadDiskCookies(for: url){
-                decisionHandler(.allow)
-            }
-        } else {
-            decisionHandler(.allow)
-        }
-    }
-    
-    public func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
-        if #available(iOS 11.0, *) {
-            let url = request.url!.host!
-            webView.writeDiskCookies(for: url){
-                decisionHandler(.allow)
-            }
-        } else {
-            decisionHandler(.allow)
-        }
+        decisionHandler(.allow)
     }
     
     public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
@@ -371,70 +355,3 @@ extension SwiftWebVC: WKNavigationDelegate {
     }
     
 }
-
-@available(iOS 11, *)
-
-extension WKWebView {
-    
-    enum PrefKey {
-        static let cookie = "cookies"
-    }
-    
-    func writeDiskCookies(for domain: String, completion: @escaping () -> ()) {
-        fetchInMemoryCookies(for: domain) { data in
-            print("write cookies data", data)
-            UserDefaults.standard.setValue(data, forKey: PrefKey.cookie + domain)
-            completion();
-        }
-    }
-    
-    func loadDiskCookies(for domain: String, completion: @escaping () -> ()) {
-        if let diskCookie = UserDefaults.standard.dictionary(forKey: (PrefKey.cookie + domain)){
-            fetchInMemoryCookies(for: domain) { freshCookie in
-                
-                print("read cookies data", freshCookie)
-                
-                let mergedCookie = diskCookie.merging(freshCookie) { (_, new) in new }
-                
-                for (_, cookieConfig) in mergedCookie {
-                    let cookie = cookieConfig as! Dictionary<String, Any>
-                    
-                    var expire : Any? = nil
-                    
-                    if let expireTime = cookie["Expires"] as? Double{
-                        expire = Date(timeIntervalSinceNow: expireTime)
-                    }
-                    
-                    let newCookie = HTTPCookie(properties: [
-                        .domain: cookie["Domain"] as Any,
-                        .path: cookie["Path"] as Any,
-                        .name: cookie["Name"] as Any,
-                        .value: cookie["Value"] as Any,
-                        .secure: cookie["Secure"] as Any,
-                        .expires: expire as Any
-                        ])
-                    
-                    self.configuration.websiteDataStore.httpCookieStore.setCookie(newCookie!)
-                }
-                
-                completion()
-            }
-            
-        }
-        else{
-            completion()
-        }
-    }
-    
-    func fetchInMemoryCookies(for domain: String, completion: @escaping ([String: Any]) -> ()) {
-        var cookieDict = [String: AnyObject]()
-        WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
-            for cookie in cookies {
-                if cookie.domain.contains(domain) {
-                    cookieDict[cookie.name] = cookie.properties as AnyObject?
-                }
-            }
-            completion(cookieDict)
-        }
-    }
-}

+ 1 - 1
iOSClient/Login/CCLoginWeb.swift

@@ -47,7 +47,7 @@ public class CCLoginWeb: UIViewController {
             urlString =  urlBase+k_flowEndpoint
         }
         
-        let webVC = SwiftModalWebVC(urlString: urlString, colorText: UIColor.black, colorDoneButton: UIColor.black, doneButtonVisible: true, hideToolbar: true)
+        let webVC = SwiftModalWebVC(urlString: urlString, colorText: UIColor.black, colorDoneButton: UIColor.black, doneButtonVisible: true, hideToolbar: true, useRedirectCookieHandling: true)
         webVC.delegateWeb = self
 
         vc.present(webVC, animated: false, completion: nil)

+ 4 - 4
iOSClient/Main/CCMore.swift

@@ -408,14 +408,14 @@ class CCMore: UIViewController, UITableViewDelegate, UITableViewDataSource, CCLo
             
             if (self.splitViewController?.isCollapsed)! {
                 
-                let webVC = SwiftWebVC(urlString: item.url, hideToolbar: false)
+                let webVC = SwiftWebVC(urlString: item.url, hideToolbar: false, useRedirectCookieHandling: false)
                 webVC.delegate = self
                 self.navigationController?.pushViewController(webVC, animated: true)
                 self.navigationController?.navigationBar.isHidden = false
                 
             } else {
                 
-                let webVC = SwiftModalWebVC(urlString: item.url, colorText: UIColor.white, colorDoneButton: UIColor.black, doneButtonVisible: true)
+                let webVC = SwiftModalWebVC(urlString: item.url, colorText: UIColor.white, colorDoneButton: UIColor.black, doneButtonVisible: true, useRedirectCookieHandling: false)
                 webVC.delegateWeb = self
                 self.present(webVC, animated: true, completion: nil)
             }
@@ -450,14 +450,14 @@ class CCMore: UIViewController, UITableViewDelegate, UITableViewDataSource, CCLo
             
             if (self.splitViewController?.isCollapsed)! {
                 
-                let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true)
+                let webVC = SwiftWebVC(urlString: item.url, hideToolbar: true, useRedirectCookieHandling: false)
                 webVC.delegate = self
                 self.navigationController?.pushViewController(webVC, animated: true)
                 self.navigationController?.navigationBar.isHidden = false
                 
             } else {
                 
-                let webVC = SwiftModalWebVC(urlString: item.url, colorText: UIColor.white, colorDoneButton: UIColor.black, doneButtonVisible: true, hideToolbar: false)
+                let webVC = SwiftModalWebVC(urlString: item.url, colorText: UIColor.white, colorDoneButton: UIColor.black, doneButtonVisible: true, hideToolbar: false, useRedirectCookieHandling: false)
                 webVC.delegateWeb = self
                 self.present(webVC, animated: true, completion: nil)
             }