fix:1、添加跳链接的webview的插件。2、修复bug

This commit is contained in:
2026-07-14 16:12:11 +08:00
parent 110660f299
commit bb84cea894
79 changed files with 4455 additions and 512 deletions
+93 -11
View File
@@ -182,6 +182,7 @@ extern "C" void UnitySendMessage(const char *, const char *, const char *);
UIView <WebViewProtocol> *webView;
NSString *gameObjectName;
NSMutableDictionary *customRequestHeader;
BOOL googleAppRedirectionEnabled;
BOOL alertDialogEnabled;
NSRegularExpression *allowRegex;
NSRegularExpression *denyRegex;
@@ -207,6 +208,7 @@ static NSMutableArray *_instances = [[NSMutableArray alloc] init];
gameObjectName = [NSString stringWithUTF8String:gameObjectName_];
customRequestHeader = [[NSMutableDictionary alloc] init];
googleAppRedirectionEnabled = false;
alertDialogEnabled = true;
allowRegex = nil;
denyRegex = nil;
@@ -222,7 +224,8 @@ static NSMutableArray *_instances = [[NSMutableArray alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];
[controller addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"unityControl"];
[controller addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"saveDataURL"];
NSString *str = @"\
{
NSString *str = @"\
window.Unity = { \
call: function(msg) { \
window.webkit.messageHandlers.unityControl.postMessage(msg); \
@@ -232,8 +235,11 @@ window.Unity = { \
} \
}; \
";
WKUserScript *script = [[WKUserScript alloc] initWithSource:str injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
[controller addUserScript:script];
}
if (!zoom) {
str = [str stringByAppendingString:@"\
NSString *str = @"\
(function() { \
var meta = document.querySelector('meta[name=viewport]'); \
if (meta == null) { \
@@ -244,12 +250,10 @@ window.Unity = { \
var head = document.getElementsByTagName('head')[0]; \
head.appendChild(meta); \
})(); \
"
];
";
WKUserScript *script = [[WKUserScript alloc] initWithSource:str injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[controller addUserScript:script];
}
WKUserScript *script
= [[WKUserScript alloc] initWithSource:str injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[controller addUserScript:script];
configuration.userContentController = controller;
configuration.allowsInlineMediaPlayback = true;
if (@available(iOS 10.0, *)) {
@@ -368,6 +372,42 @@ window.Unity = { \
}];
}
+ (void)clearCookie:(const char *)name of:(const char *)url
{
NSURL *nsurl = [NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]];
if (nsurl == nil) {
return;
}
NSString *nsname = [NSString stringWithUTF8String:name];
if (@available(iOS 9.0, *)) {
WKHTTPCookieStore *cookieStore = WKWebsiteDataStore.defaultDataStore.httpCookieStore;
[cookieStore
getAllCookies:^(NSArray<NSHTTPCookie *> *array) {
[array
enumerateObjectsUsingBlock:^(NSHTTPCookie *cookie, NSUInteger idx, BOOL *stop) {
if ([cookie.name isEqualToString:nsname]
&& [cookie.domain isEqualToString:nsurl.host]
&& [cookie.path isEqualToString:nsurl.path]) {
[cookieStore deleteCookie:cookie completionHandler:^{}];
}
}];
}];
} else {
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) {
if ([cookie.name isEqualToString:nsname]
&& [cookie.domain isEqualToString:nsurl.host]
&& [cookie.path isEqualToString:nsurl.path]) {
[cookieStorage deleteCookie:cookie];
}
}];
}
}
+ (void)clearCookies
{
[CWebViewPlugin resetSharedProcessPool];
@@ -606,7 +646,11 @@ window.Unity = { \
return NO;
}
if ([url rangeOfString:@"//itunes.apple.com/"].location != NSNotFound) {
[[UIApplication sharedApplication] openURL:nsurl];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:nsurl options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:nsurl];
}
return NO;
} else if ([url hasPrefix:@"unity:"]) {
UnitySendMessage([gameObjectName UTF8String], "CallFromJS", [[url substringFromIndex:6] UTF8String]);
@@ -652,7 +696,11 @@ window.Unity = { \
return;
}
if ([url rangeOfString:@"//itunes.apple.com/"].location != NSNotFound) {
[[UIApplication sharedApplication] openURL:nsurl];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:nsurl options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:nsurl];
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
} else if ([url hasPrefix:@"unity:"]) {
@@ -669,7 +717,11 @@ window.Unity = { \
&& ![url hasPrefix:@"http:"]
&& ![url hasPrefix:@"https:"]) {
if([[UIApplication sharedApplication] canOpenURL:nsurl]) {
[[UIApplication sharedApplication] openURL:nsurl];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:nsurl options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:nsurl];
}
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
@@ -683,7 +735,7 @@ window.Unity = { \
if (navigationAction.targetFrame != nil && navigationAction.targetFrame.isMainFrame) {
// If the custom header is not attached, give it and make a request again.
if (![self isSetupedCustomHeader:[navigationAction request]]) {
NSLog(@"navi ... %@", navigationAction);
//NSLog(@"navi ... %@", navigationAction);
[wkWebView loadRequest:[self constructionCustomHeader:navigationAction.request]];
decisionHandler(WKNavigationActionPolicyCancel);
return;
@@ -691,6 +743,14 @@ window.Unity = { \
}
}
UnitySendMessage([gameObjectName UTF8String], "CallOnStarted", [url UTF8String]);
// cf. https://stackoverflow.com/questions/37086605/disable-wkwebview-for-opening-links-to-redirect-to-apps-installed-on-my-iphone/76948270#76948270
if (!googleAppRedirectionEnabled
&& [url hasPrefix:@"https://www.google.com/"]
&& navigationAction.navigationType == WKNavigationTypeLinkActivated) {
[webView load:navigationAction.request];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
@@ -852,6 +912,13 @@ window.Unity = { \
webView.userInteractionEnabled = enabled;
}
- (void)setGoogleAppRedirectionEnabled:(BOOL)enabled
{
if (webView == nil)
return;
googleAppRedirectionEnabled = enabled;
}
- (void)setAlertDialogEnabled:(BOOL)enabled
{
alertDialogEnabled = enabled;
@@ -1044,6 +1111,7 @@ extern "C" {
void *instance, float left, float top, float right, float bottom, BOOL relative);
void _CWebViewPlugin_SetVisibility(void *instance, BOOL visibility);
void _CWebViewPlugin_SetInteractionEnabled(void *instance, BOOL enabled);
void _CWebViewPlugin_SetGoogleAppRedirectionEnabled(void *instance, BOOL enabled);
void _CWebViewPlugin_SetAlertDialogEnabled(void *instance, BOOL visibility);
void _CWebViewPlugin_SetScrollbarsVisibility(void *instance, BOOL visibility);
void _CWebViewPlugin_SetScrollBounceEnabled(void *instance, BOOL enabled);
@@ -1060,6 +1128,7 @@ extern "C" {
void _CWebViewPlugin_AddCustomHeader(void *instance, const char *headerKey, const char *headerValue);
void _CWebViewPlugin_RemoveCustomHeader(void *instance, const char *headerKey);
void _CWebViewPlugin_ClearCustomHeader(void *instance);
void _CWebViewPlugin_ClearCookie(const char *url, const char *name);
void _CWebViewPlugin_ClearCookies();
void _CWebViewPlugin_SaveCookies();
void _CWebViewPlugin_GetCookies(void *instance, const char *url);
@@ -1131,6 +1200,14 @@ void _CWebViewPlugin_SetInteractionEnabled(void *instance, BOOL enabled)
[webViewPlugin setInteractionEnabled:enabled];
}
void _CWebViewPlugin_SetGoogleAppRedirectionEnabled(void *instance, BOOL enabled)
{
if (instance == NULL)
return;
CWebViewPlugin *webViewPlugin = (__bridge CWebViewPlugin *)instance;
[webViewPlugin setGoogleAppRedirectionEnabled:enabled];
}
void _CWebViewPlugin_SetAlertDialogEnabled(void *instance, BOOL enabled)
{
if (instance == NULL)
@@ -1259,6 +1336,11 @@ void _CWebViewPlugin_ClearCustomHeader(void *instance)
[webViewPlugin clearCustomRequestHeader];
}
void _CWebViewPlugin_ClearCookie(const char *url, const char *name)
{
[CWebViewPlugin clearCookie:name of:url];
}
void _CWebViewPlugin_ClearCookies()
{
[CWebViewPlugin clearCookies];