Skip to content

Commit

Permalink
Merge pull request #433 from gree/feature/get_cookies
Browse files Browse the repository at this point in the history
introduced GetCookies(url).
  • Loading branch information
KojiNakamaru authored Jun 23, 2019
2 parents 782b3c5 + 010cbc5 commit 8f7dfd6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
5 changes: 5 additions & 0 deletions plugins/Android/src/net/gree/unitywebview/CWebViewPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,9 @@ public void ClearCookies()
}
}

public String GetCookies(String url)
{
CookieManager cookieManager = CookieManager.getInstance();
return cookieManager.getCookie(url);
}
}
30 changes: 25 additions & 5 deletions plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ private static extern void _CWebViewPlugin_SetFrame(
private static extern void _CWebViewPlugin_ClearCustomHeader(IntPtr instance);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_ClearCookies();
[DllImport("__Internal")]
private static extern string _CWebViewPlugin_GetCookies(string url);
#endif

public static bool IsWebViewAvailable() {
Expand Down Expand Up @@ -702,7 +704,6 @@ public void AddCustomHeader(string headerKey, string headerValue)
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;

_CWebViewPlugin_AddCustomHeader(webView, headerKey, headerValue);
#elif UNITY_ANDROID
if (webView == null)
Expand All @@ -722,7 +723,6 @@ public string GetCustomHeaderValue(string headerKey)
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return null;

return _CWebViewPlugin_GetCustomHeaderValue(webView, headerKey);
#elif UNITY_ANDROID
if (webView == null)
Expand All @@ -738,7 +738,6 @@ public void RemoveCustomHeader(string headerKey)
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;

_CWebViewPlugin_RemoveCustomHeader(webView, headerKey);
#elif UNITY_ANDROID
if (webView == null)
Expand All @@ -756,7 +755,6 @@ public void ClearCustomHeader()
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE
if (webView == IntPtr.Zero)
return;

_CWebViewPlugin_ClearCustomHeader(webView);
#elif UNITY_ANDROID
if (webView == null)
Expand All @@ -774,7 +772,6 @@ public void ClearCookies()
#elif UNITY_IPHONE && !UNITY_EDITOR
if (webView == IntPtr.Zero)
return;

_CWebViewPlugin_ClearCookies();
#elif UNITY_ANDROID && !UNITY_EDITOR
if (webView == null)
Expand All @@ -784,6 +781,29 @@ public void ClearCookies()
}


public string GetCookies(string url)
{
#if UNITY_WEBPLAYER
//TODO: UNSUPPORTED
return "";
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
//TODO: UNSUPPORTED
return "";
#elif UNITY_IPHONE && !UNITY_EDITOR
if (webView == IntPtr.Zero)
return "";
return _CWebViewPlugin_GetCookies(url);
#elif UNITY_ANDROID && !UNITY_EDITOR
if (webView == null)
return "";
return webView.Call<string>("GetCookies", url);
#else
//TODO: UNSUPPORTED
return "";
#endif
}


#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
void OnApplicationFocus(bool focus)
{
Expand Down
48 changes: 47 additions & 1 deletion plugins/iOS/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ - (void)dispose
gameObjectName = nil;
}

+(void)clearCookies
+ (void)clearCookies
{
// cf. https://dev.classmethod.jp/smartphone/remove-webview-cookies/
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
Expand All @@ -213,6 +213,10 @@ +(void)clearCookies
[[NSFileManager defaultManager] removeItemAtPath:webKitPath error:nil];

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
if (cookieStorage == nil) {
// cf. https://stackoverflow.com/questions/33876295/nshttpcookiestorage-sharedhttpcookiestorage-comes-up-empty-in-10-11
cookieStorage = [NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:@"Cookies"];
}
[[cookieStorage cookies] enumerateObjectsUsingBlock:^(NSHTTPCookie *cookie, NSUInteger idx, BOOL *stop) {
[cookieStorage deleteCookie:cookie];
}];
Expand All @@ -227,6 +231,42 @@ +(void)clearCookies
}
}

+ (const char *)getCookies:(const char *)url
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSMutableString *result = [NSMutableString string];
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
if (cookieStorage == nil) {
// cf. https://stackoverflow.com/questions/33876295/nshttpcookiestorage-sharedhttpcookiestorage-comes-up-empty-in-10-11
cookieStorage = [NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:@"Cookies"];
}
[[cookieStorage cookiesForURL:[NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]]]
enumerateObjectsUsingBlock:^(NSHTTPCookie *cookie, NSUInteger idx, BOOL *stop) {
[result appendString:[NSString stringWithFormat:@"%@=%@", cookie.name, cookie.value]];
if ([cookie.domain length] > 0) {
[result appendString:[NSString stringWithFormat:@"; "]];
[result appendString:[NSString stringWithFormat:@"Domain=%@", cookie.domain]];
}
if ([cookie.path length] > 0) {
[result appendString:[NSString stringWithFormat:@"; "]];
[result appendString:[NSString stringWithFormat:@"Path=%@", cookie.path]];
}
if (cookie.expiresDate != nil) {
[result appendString:[NSString stringWithFormat:@"; "]];
[result appendString:[NSString stringWithFormat:@"Expires=%@", [formatter stringFromDate:cookie.expiresDate]]];
}
[result appendString:[NSString stringWithFormat:@"; "]];
[result appendString:[NSString stringWithFormat:@"Version=%zd", cookie.version]];
[result appendString:[NSString stringWithFormat:@"\n"]];
}];
const char *s = [result UTF8String];
char *r = (char *)malloc(strlen(s) + 1);
strcpy(r, s);
return r;
}

- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {

Expand Down Expand Up @@ -612,6 +652,7 @@ void _CWebViewPlugin_SetMargins(
void _CWebViewPlugin_RemoveCustomHeader(void *instance, const char *headerKey);
void _CWebViewPlugin_ClearCustomHeader(void *instance);
void _CWebViewPlugin_ClearCookies();
const char *_CWebViewPlugin_GetCookies(const char *url);
const char *_CWebViewPlugin_GetCustomHeaderValue(void *instance, const char *headerKey);
}

Expand Down Expand Up @@ -725,6 +766,11 @@ void _CWebViewPlugin_ClearCookies()
[CWebViewPlugin clearCookies];
}

const char *_CWebViewPlugin_GetCookies(const char *url)
{
return [CWebViewPlugin getCookies:url];
}

const char *_CWebViewPlugin_GetCustomHeaderValue(void *instance, const char *headerKey)
{
CWebViewPlugin *webViewPlugin = (__bridge CWebViewPlugin *)instance;
Expand Down
5 changes: 5 additions & 0 deletions sample/Assets/Scripts/SampleWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ void OnGUI()
}
}
GUI.enabled = true;

if (GUI.Button(new Rect(700, 10, 80, 80), "c")) {
Debug.Log(webViewObject.GetCookies(Url));
}
GUI.enabled = true;
}
#endif
}

0 comments on commit 8f7dfd6

Please sign in to comment.