@@ -88,6 +88,8 @@ - (void)setScrollBounce:(BOOL)enable
8888
8989@end
9090
91+ // cf. https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303
92+
9193@interface CWebViewPlugin : NSObject <WKUIDelegate , WKNavigationDelegate , WKScriptMessageHandler >
9294{
9395 UIView <WebViewProtocol> *webView;
@@ -99,6 +101,8 @@ @interface CWebViewPlugin : NSObject<WKUIDelegate, WKNavigationDelegate, WKScrip
99101 NSRegularExpression *hookRegex;
100102 NSString *basicAuthUserName;
101103 NSString *basicAuthPassword;
104+ NSHTTPCookie *sessionCookieFromServer;
105+ NSHTTPCookie *sessionCookieFromClient;
102106}
103107@end
104108
@@ -119,6 +123,8 @@ - (id)initWithGameObjectName:(const char *)gameObjectName_ transparent:(BOOL)tra
119123 hookRegex = nil ;
120124 basicAuthUserName = nil ;
121125 basicAuthPassword = nil ;
126+ sessionCookieFromServer = nil ;
127+ sessionCookieFromClient = nil ;
122128 if (ua != NULL && strcmp (ua, " " ) != 0 ) {
123129 [[NSUserDefaults standardUserDefaults ]
124130 registerDefaults: @{ @" UserAgent" : [[NSString alloc ] initWithUTF8String: ua] }];
@@ -299,11 +305,18 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
299305
300306 if ([keyPath isEqualToString: @" loading" ] && [[change objectForKey: NSKeyValueChangeNewKey ] intValue ] == 0
301307 && [webView URL ] != nil ) {
302- UnitySendMessage (
303- [gameObjectName UTF8String ],
304- " CallOnLoaded" ,
305- [[[webView URL ] absoluteString ] UTF8String ]);
306-
308+ WKWebView *wkWebView = (WKWebView *)webView;
309+ [wkWebView.configuration.websiteDataStore.httpCookieStore getAllCookies: ^(NSArray <NSHTTPCookie *> * cookies) {
310+ for (NSHTTPCookie *cookie in cookies) {
311+ if (cookie.sessionOnly ) {
312+ sessionCookieFromServer = cookie;
313+ }
314+ }
315+ UnitySendMessage (
316+ [gameObjectName UTF8String ],
317+ " CallOnLoaded" ,
318+ [[[webView URL ] absoluteString ] UTF8String ]);
319+ }];
307320 }
308321}
309322
@@ -695,6 +708,80 @@ - (void)setBasicAuthInfo:(const char *)userName password:(const char *)password
695708 basicAuthUserName = [NSString stringWithUTF8String: userName];
696709 basicAuthPassword = [NSString stringWithUTF8String: password];
697710}
711+
712+ - (const char *)getSessionCookieFromServer
713+ {
714+ if (sessionCookieFromServer == nil ) {
715+ return NULL ;
716+ }
717+ NSDateFormatter *formatter = [[NSDateFormatter alloc ] init ];
718+ formatter.locale = [NSLocale localeWithLocaleIdentifier: @" en_US_POSIX" ];
719+ [formatter setDateFormat: @" EEE, dd MMM yyyy HH:mm:ss zzz" ];
720+ NSHTTPCookie *cookie = sessionCookieFromServer;
721+ NSMutableString *result = [NSMutableString string ];
722+ [result appendString: [NSString stringWithFormat: @" %@ =%@ " , cookie.name, cookie.value]];
723+ if ([cookie.domain length ] > 0 ) {
724+ [result appendString: [NSString stringWithFormat: @" ; " ]];
725+ [result appendString: [NSString stringWithFormat: @" Domain=%@ " , cookie.domain]];
726+ }
727+ if ([cookie.path length ] > 0 ) {
728+ [result appendString: [NSString stringWithFormat: @" ; " ]];
729+ [result appendString: [NSString stringWithFormat: @" Path=%@ " , cookie.path]];
730+ }
731+ if (cookie.expiresDate != nil ) {
732+ [result appendString: [NSString stringWithFormat: @" ; " ]];
733+ [result appendString: [NSString stringWithFormat: @" Expires=%@ " , [formatter stringFromDate: cookie.expiresDate]]];
734+ }
735+ [result appendString: [NSString stringWithFormat: @" ; " ]];
736+ [result appendString: [NSString stringWithFormat: @" Version=%zd " , cookie.version]];
737+ const char *s = [result UTF8String ];
738+ char *r = (char *)malloc (strlen (s) + 1 );
739+ strcpy (r, s);
740+ return r;
741+ }
742+
743+ - (void )setSessionCookieFromClient : (const char *)cookie
744+ {
745+ sessionCookieFromClient = nil ;
746+ if (cookie == nil || strlen (cookie) == 0 ) {
747+ return ;
748+ }
749+ NSError *err = nil ;
750+ NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern: @" ([^=; ]+)=([^=;]+)" options: 0 error: &err];
751+ if (err != nil ) {
752+ return ;
753+ }
754+ NSMutableDictionary *props = [[NSMutableDictionary alloc ] init ];
755+ NSString *str = [NSString stringWithUTF8String: cookie];
756+ NSArray *matches = [re matchesInString: str options: 0 range: NSMakeRange (0 , strlen (cookie))];
757+ [matches enumerateObjectsUsingBlock: ^(NSTextCheckingResult *result, NSUInteger idx, BOOL *stop) {
758+ NSString *k = [str substringWithRange: [result rangeAtIndex: 1 ]];
759+ NSString *v = [str substringWithRange: [result rangeAtIndex: 2 ]];
760+ if (idx == 0 ) {
761+ [props setValue: k forKey: NSHTTPCookieName ];
762+ [props setValue: v forKey: NSHTTPCookieValue ];
763+ } else if ([k isEqualToString: @" Domain" ]) {
764+ [props setValue: v forKey: NSHTTPCookieDomain ];
765+ } else if ([k isEqualToString: @" Path" ]) {
766+ [props setValue: v forKey: NSHTTPCookiePath ];
767+ } else if ([k isEqualToString: @" Version" ]) {
768+ [props setValue: v forKey: NSHTTPCookieVersion ];
769+ } else if ([k isEqualToString: @" Expires" ]) {
770+ NSDateFormatter *formatter = [[NSDateFormatter alloc ] init ];
771+ formatter.locale = [NSLocale localeWithLocaleIdentifier: @" en_US_POSIX" ];
772+ [formatter setDateFormat: @" EEE, dd MMM yyyy HH:mm:ss zzz" ];
773+ NSDate *d = [formatter dateFromString: v];
774+ [props setValue: d forKey: k];
775+ }
776+ }];
777+ if (props.count == 0 ) {
778+ return ;
779+ }
780+ sessionCookieFromClient = [[NSHTTPCookie alloc ] initWithProperties: props];
781+ WKWebView *wkWebView = (WKWebView *)webView;
782+ WKHTTPCookieStore *cookieStore = wkWebView.configuration .websiteDataStore .httpCookieStore ;
783+ [cookieStore setCookie: sessionCookieFromClient completionHandler: ^{}];
784+ }
698785@end
699786
700787extern " C" {
@@ -722,6 +809,8 @@ void _CWebViewPlugin_SetMargins(
722809 const char *_CWebViewPlugin_GetCookies (const char *url);
723810 const char *_CWebViewPlugin_GetCustomHeaderValue (void *instance, const char *headerKey);
724811 void _CWebViewPlugin_SetBasicAuthInfo (void *instance, const char *userName, const char *password);
812+ const char *_CWebViewPlugin_GetSessionCookieFromServer (void *instance);
813+ void _CWebViewPlugin_SetSessionCookieFromClient (void *instance, const char *cookie);
725814}
726815
727816void *_CWebViewPlugin_Init (const char *gameObjectName, BOOL transparent, const char *ua, BOOL enableWKWebView)
@@ -903,4 +992,20 @@ void _CWebViewPlugin_SetBasicAuthInfo(void *instance, const char *userName, cons
903992 [webViewPlugin setBasicAuthInfo: userName password: password];
904993}
905994
995+ const char *_CWebViewPlugin_GetSessionCookieFromServer (void *instance)
996+ {
997+ if (instance == NULL )
998+ return NULL ;
999+ CWebViewPlugin *webViewPlugin = (__bridge CWebViewPlugin *)instance;
1000+ return [webViewPlugin getSessionCookieFromServer ];
1001+ }
1002+
1003+ void _CWebViewPlugin_SetSessionCookieFromClient (void *instance, const char *cookie)
1004+ {
1005+ if (instance == NULL )
1006+ return ;
1007+ CWebViewPlugin *webViewPlugin = (__bridge CWebViewPlugin *)instance;
1008+ [webViewPlugin setSessionCookieFromClient: cookie];
1009+ }
1010+
9061011#endif // !(__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0)
0 commit comments