Skip to content

Commit 710d51c

Browse files
committed
added GetSessionCookieFromServer and SetSessionCookieFromClient.
1 parent 35c00e3 commit 710d51c

File tree

2 files changed

+151
-5
lines changed

2 files changed

+151
-5
lines changed

plugins/WebViewObject.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ private static extern void _CWebViewPlugin_GoForward(
346346
private static extern string _CWebViewPlugin_GetCookies(string url);
347347
[DllImport("__Internal")]
348348
private static extern void _CWebViewPlugin_SetBasicAuthInfo(IntPtr instance, string userName, string password);
349+
[DllImport("__Internal")]
350+
private static extern string _CWebViewPlugin_GetSessionCookieFromServer(IntPtr instance);
351+
[DllImport("__Internal")]
352+
private static extern void _CWebViewPlugin_SetSessionCookieFromClient(IntPtr instance, string cookie);
349353
#elif UNITY_WEBGL
350354
[DllImport("__Internal")]
351355
private static extern void _gree_unity_webview_init(string name);
@@ -999,6 +1003,43 @@ public void SetBasicAuthInfo(string userName, string password)
9991003
#endif
10001004
}
10011005

1006+
public string GetSessionCookieFromServer()
1007+
{
1008+
#if UNITY_WEBPLAYER || UNITY_WEBGL
1009+
//TODO: UNSUPPORTED
1010+
return "";
1011+
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
1012+
//TODO: UNSUPPORTED
1013+
return "";
1014+
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
1015+
//TODO: UNSUPPORTED
1016+
return "";
1017+
#elif UNITY_IPHONE
1018+
if (webView == IntPtr.Zero)
1019+
return "";
1020+
return _CWebViewPlugin_GetSessionCookieFromServer(webView);
1021+
#elif UNITY_ANDROID
1022+
//TODO: UNSUPPORTED
1023+
return "";
1024+
#endif
1025+
}
1026+
1027+
public void SetSessionCookieFromClient(string cookie)
1028+
{
1029+
#if UNITY_WEBPLAYER || UNITY_WEBGL
1030+
//TODO: UNSUPPORTED
1031+
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_EDITOR_LINUX
1032+
//TODO: UNSUPPORTED
1033+
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
1034+
//TODO: UNSUPPORTED
1035+
#elif UNITY_IPHONE
1036+
if (webView == IntPtr.Zero)
1037+
return;
1038+
_CWebViewPlugin_SetSessionCookieFromClient(webView, cookie);
1039+
#elif UNITY_ANDROID
1040+
//TODO: UNSUPPORTED
1041+
#endif
1042+
}
10021043

10031044
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
10041045
void OnApplicationFocus(bool focus)

plugins/iOS/WebView.mm

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

700787
extern "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

727816
void *_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

Comments
 (0)