You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by GitBox <gi...@apache.org> on 2021/02/18 21:46:25 UTC

[GitHub] [cordova-plugin-inappbrowser] YazeedFares commented on issue #804: add getCookies method

YazeedFares commented on issue #804:
URL: https://github.com/apache/cordova-plugin-inappbrowser/issues/804#issuecomment-781655320


   @tinni95 @samulla  @adhishnigam01 
   
   What I did to share the cookie is to implement new methods in the "CDVWKInAppBrowser.m" file then exposed them in the related JS file "inappbrowser.js", then we managed the session cookie based on our need, here is the code I did:
   
   **In inappbrowser.js I added:**
   
   getCookies:function (url, successCallback, errorCallback) {
           exec(successCallback, errorCallback, 'InAppBrowser', 'getCookies', [url]);
    }
   
   **In CDVWKInAppBrowser.m we added the following methods:**
   
   - (void) getCookies:(CDVInvokedUrlCommand*)command{
       WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
       WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
       
       __block NSString *url = command.arguments[0];
       __block CDVPluginResult* pluginResult;
       __block bool cookieFound = NO;
       __block NSString* cookieValue = @"";
   
       [cookieStore getAllCookies:^(NSArray* cookies) {
           NSHTTPCookie* cookie;
           for(cookie in cookies){
               if([url containsString: cookie.domain]){
                   if (cookie.value != nil && [cookie.name isEqual:@"SESSIONID"] && ![cookie.value isEqual:@""]) {
                       cookieFound = YES;
                       cookieValue = cookie.value;
                       break;
                   }
               }
           }
           
           if(cookieFound){
               pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"cookieValue": cookieValue}];
           } else {
               pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No cookie found"];
           }
           
           [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
           
       }];
   }
   
   
   - (void) setCookieFor:(NSString *)url withCookieInfo:(NSString *)cookieInfo {
       WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
       WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
       NSMutableDictionary* cookieProperties = [NSMutableDictionary dictionary];
       
       NSArray *cookieParts = [cookieInfo componentsSeparatedByString:@"="];
       
       NSURL* urlObject = [NSURL URLWithString:url];
       NSString* reducedUrl = [NSString stringWithFormat:
           @"%@://%@/%@",
           urlObject.scheme,
           urlObject.host,
           urlObject.pathComponents[1]];
   
       //set rest of the properties
       if ([cookieParts count] > 1){
           [cookieProperties setObject:cookieParts[0] forKey:NSHTTPCookieName];
           [cookieProperties setObject:cookieParts[1] forKey:NSHTTPCookieValue];
           [cookieProperties setObject:@"/<path>" forKey:NSHTTPCookiePath];
           [cookieProperties setObject:[urlObject host] forKey:NSHTTPCookieDomain];
           //create a NSDate for some future time
           NSDate* expiryDate = [[NSDate date] dateByAddingTimeInterval:2629743];
           [cookieProperties setObject:expiryDate forKey:NSHTTPCookieExpires];
           [cookieProperties setObject:@"TRUE" forKey:NSHTTPCookieSecure];
           NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
           
           [dataStore.httpCookieStore setCookie:cookie completionHandler:^{
               
           }];
           
           [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
       }
   }


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org