Skip to content

WKScriptMessageHandler support for WKWebView and WebViewSeparated.bundle #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ Temp
*.sln
*.pidb
*.userprefs
**/*.xcodeproj/*
!**/*.xcodeproj/project.pbxproj
!**/*.xcworkspace/contents.xcworkspacedata
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ below:

### OS X (Editor)

#### App Transport Security

Since Unity 5.3.0, Unity.app is built with ATS (App Transport
Security) enabled and non-secured connection (HTTP) is not
permitted. If you want to open `http://foo/bar.html` with this plugin
Expand Down Expand Up @@ -50,11 +52,19 @@ or invoke the following from your terminal,
/usr/libexec/PlistBuddy -c "Add NSAppTransportSecurity:NSAllowsArbitraryLoads bool true" /Applications/Unity/Unity.app/Contents/Info.plist
```

#### References
##### References

* https://github.com/gree/unity-webview/issues/64
* https://onevcat.zendesk.com/hc/en-us/articles/215527307-I-cannot-open-the-web-page-in-Unity-Editor-

#### WebViewSeparated.bundle

WebViewSeparated.bundle is a variation of WebView.bundle. It is based
on https://github.com/gree/unity-webview/pull/161 . As noted in the
pull-request, it shows a separate window and allows a developer to
utilize the Safari debugger. For enabling it, please define
`WEBVIEW_SEPARATED`.

### iOS

The implementation now supports WKWebView but it is disabled by
Expand Down
Binary file modified dist/unity-webview.unitypackage
Binary file not shown.
Binary file modified dist/unity-webview.zip
Binary file not shown.
28 changes: 28 additions & 0 deletions plugins/Mac/Resources/Info-WebViewSeparated.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion plugins/Mac/Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>net.gree.unitywebview.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
48 changes: 26 additions & 22 deletions plugins/Mac/Sources/WebView.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* Copyright (C) 2011 Keijiro Takahashi
* Copyright (C) 2012 GREE, Inc.
*
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
Expand All @@ -34,20 +34,20 @@
typedef void *MonoMethod;
typedef void *MonoString;

#ifdef __cplusplus
extern "C" {
#endif
MonoDomain *mono_domain_get();
MonoAssembly *mono_domain_assembly_open(
MonoDomain *domain, const char *assemblyName);
MonoAssembly *mono_domain_assembly_open(MonoDomain *domain, const char *assemblyName);
MonoImage *mono_assembly_get_image(MonoAssembly *assembly);
MonoMethodDesc *mono_method_desc_new(
const char *methodString, int useNamespace);
MonoMethodDesc *mono_method_desc_new(const char *methodString, int useNamespace);
MonoMethodDesc *mono_method_desc_free(MonoMethodDesc *desc);
MonoMethod *mono_method_desc_search_in_image(
MonoMethodDesc *methodDesc, MonoImage *image);
MonoObject *mono_runtime_invoke(
MonoMethod *method, void *obj, void **params, MonoObject **exc);
MonoMethod *mono_method_desc_search_in_image(MonoMethodDesc *methodDesc, MonoImage *image);
MonoObject *mono_runtime_invoke(MonoMethod *method, void *obj, void **params, MonoObject **exc);
MonoString *mono_string_new(MonoDomain *domain, const char *text);
#ifdef __cplusplus
}
#endif

static BOOL inEditor;
static MonoDomain *monoDomain;
Expand All @@ -73,16 +73,16 @@ static void UnitySendMessage(
monoDomain = mono_domain_get();
monoDesc = mono_method_desc_new(
"UnitySendMessageDispatcher:Dispatch(string,string,string)", FALSE);

monoAssembly =
mono_domain_assembly_open(monoDomain, [assemblyPath UTF8String]);

if (monoAssembly != 0) {
monoImage = mono_assembly_get_image(monoAssembly);
monoMethod = mono_method_desc_search_in_image(monoDesc, monoImage);
}


if (monoMethod == 0) {
if (inEditor) {
assemblyPath =
Expand All @@ -102,11 +102,11 @@ static void UnitySendMessage(
}
}
}

if (monoMethod == 0) {
return;
}

void *args[] = {
mono_string_new(monoDomain, gameObject),
mono_string_new(monoDomain, method),
Expand Down Expand Up @@ -196,7 +196,7 @@ - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary
} else {
if ([customRequestHeader count] > 0) {
bool isCustomized = YES;

// Check for additional custom header.
for (NSString *key in [customRequestHeader allKeys])
{
Expand All @@ -213,7 +213,7 @@ - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary
return;
}
}

[listener use];
}
}
Expand Down Expand Up @@ -321,20 +321,20 @@ - (void)update:(int)x y:(int)y deltaY:(float)deltaY buttonDown:(BOOL)buttonDown
event = [NSEvent mouseEventWithType:NSLeftMouseDown
location:NSMakePoint(x, y) modifierFlags:nil
timestamp:GetCurrentEventTime() windowNumber:0
context:context eventNumber:nil clickCount:1 pressure:nil];
context:context eventNumber:nil clickCount:1 pressure:1];
[view mouseDown:event];
} else {
event = [NSEvent mouseEventWithType:NSLeftMouseDragged
location:NSMakePoint(x, y) modifierFlags:nil
timestamp:GetCurrentEventTime() windowNumber:0
context:context eventNumber:nil clickCount:0 pressure:nil];
context:context eventNumber:nil clickCount:0 pressure:1];
[view mouseDragged:event];
}
} else if (buttonRelease) {
event = [NSEvent mouseEventWithType:NSLeftMouseUp
location:NSMakePoint(x, y) modifierFlags:nil
timestamp:GetCurrentEventTime() windowNumber:0
context:context eventNumber:nil clickCount:0 pressure:nil];
context:context eventNumber:nil clickCount:0 pressure:0];
[view mouseUp:event];
}

Expand Down Expand Up @@ -452,7 +452,7 @@ - (const char *)getCustomRequestHeaderValue:(const char *)headerKey
if (!result) {
return NULL;
}

const char *s = [result UTF8String];
char *r = (char *)malloc(strlen(s) + 1);
strcpy(r, s);
Expand All @@ -462,7 +462,9 @@ - (const char *)getCustomRequestHeaderValue:(const char *)headerKey
@end

typedef void (*UnityRenderEventFunc)(int eventId);
#ifdef __cplusplus
extern "C" {
#endif
const char *_CWebViewPlugin_GetAppPath();
void *_CWebViewPlugin_Init(
const char *gameObject, BOOL transparent, int width, int height, const char *ua, BOOL ineditor);
Expand All @@ -489,7 +491,9 @@ void _CWebViewPlugin_Update(void *instance, int x, int y, float deltaY,
void _CWebViewPlugin_RemoveCustomHeader(void *instance, const char *headerKey);
void _CWebViewPlugin_ClearCustomHeader(void *instance);
const char *_CWebViewPlugin_GetCustomHeaderValue(void *instance, const char *headerKey);
#ifdef __cplusplus
}
#endif

const char *_CWebViewPlugin_GetAppPath()
{
Expand Down
Loading