Skip to content
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

Use WKWebView on macOS #161

Closed
wants to merge 3 commits into from
Closed
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
Binary file modified dist/unity-webview.unitypackage
Binary file not shown.
Binary file modified dist/unity-webview.zip
Binary file not shown.
4 changes: 3 additions & 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 All @@ -22,6 +22,8 @@
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
130 changes: 113 additions & 17 deletions plugins/Mac/Sources/WebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ static void UnitySendMessage(
mono_runtime_invoke(monoMethod, 0, args, 0);
}

@interface CWebViewPlugin : NSObject
@interface CWebViewPlugin : NSObject<WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>
{
WebView *webView;
NSWindow *window;
NSWindowController *windowController;
WKWebView *webView;
NSString *gameObject;
NSString *ua;
NSBitmapImageRep *bitmap;
Expand All @@ -127,28 +129,55 @@ - (id)initWithGameObject:(const char *)gameObject_ transparent:(BOOL)transparent
{
self = [super init];
monoMethod = 0;
webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
webView.hidden = YES;

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];
WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.javaScriptEnabled = true;
preferences.plugInsEnabled = true;
[controller addScriptMessageHandler:self name:@"unityControl"];
configuration.userContentController = controller;
// configuration.preferences = preferences;
NSRect frame = NSMakeRect(0, 0, width / 2, height / 2);
webView = [[WKWebView alloc] initWithFrame:frame
configuration:configuration];
[[[webView configuration] preferences] setValue:@YES forKey:@"developerExtrasEnabled"];
webView.UIDelegate = self;
webView.navigationDelegate = self;
webView.hidden = NO;
if (transparent) {
[webView setDrawsBackground:NO];
// [webView setDrawsBackground:NO];
}
[webView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
[webView setFrameLoadDelegate:(id)self];
[webView setPolicyDelegate:(id)self];
// [webView setFrameLoadDelegate:(id)self];
// [webView setPolicyDelegate:(id)self];
webView.UIDelegate = self;
webView.navigationDelegate = self;
gameObject = [[NSString stringWithUTF8String:gameObject_] retain];
if (ua_ != NULL && strcmp(ua_, "") != 0) {
ua = [[NSString stringWithUTF8String:ua_] retain];
[webView setCustomUserAgent:ua];
}

window = [[[NSWindow alloc] initWithContentRect:frame
styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered
defer:NO] autorelease];
[window setContentView:webView];
[window orderFront:NSApp];
windowController = [[NSWindowController alloc] initWithWindow:window];

return self;
}

- (void)dealloc
{
@synchronized(self) {
if (webView != nil) {
[webView setFrameLoadDelegate:nil];
[webView setPolicyDelegate:nil];
// [webView setFrameLoadDelegate:nil];
// [webView setPolicyDelegate:nil];
webView.UIDelegate = nil;
webView.navigationDelegate = nil;
[webView stopLoading:nil];
[webView release];
webView = nil;
Expand All @@ -165,10 +194,20 @@ - (void)dealloc
[bitmap release];
bitmap = nil;
}
if (window != nil) {
[window release];
window = nil;
}
if (windowController != nil){
[windowController release];
windowController = nil;
}
}
[super dealloc];
}


/*
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
{
UnitySendMessage([gameObject UTF8String], "CallOnError", [[error description] UTF8String]);
Expand All @@ -189,14 +228,65 @@ - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary
[listener use];
}
}
*/

- (void)webView:(WKWebView*)wkWebView didCommitNavigation:(null_unspecified WKNavigation *)navigation
{
UnitySendMessage([gameObject UTF8String], "CallOnLoaded", "Unknown URL");

}

- (void)webView:(WKWebView *)wkWebView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if (webView == nil) {
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
NSURL *url = [navigationAction.request URL];
//if ([url.absoluteString rangeOfString:@"//itunes.apple.com/"].location != NSNotFound) {
// [[UIApplication sharedApplication] openURL:url];
// decisionHandler(WKNavigationActionPolicyCancel);
//} else
if ([url.absoluteString hasPrefix:@"unity:"]) {
UnitySendMessage([gameObject UTF8String], "CallFromJS", [[url.absoluteString substringFromIndex:6] UTF8String]);
decisionHandler(WKNavigationActionPolicyCancel);
} else if (navigationAction.navigationType == WKNavigationTypeLinkActivated
&& (!navigationAction.targetFrame || !navigationAction.targetFrame.isMainFrame)) {
// cf. for target="_blank", cf. http://qiita.com/ShingoFukuyama/items/b3a1441025a36ab7659c
[webView loadRequest:navigationAction.request];
decisionHandler(WKNavigationActionPolicyCancel);
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}


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

// Log out the message received
NSLog(@"Received event %@", message.body);
UnitySendMessage([gameObject UTF8String], "CallFromJS",
[[NSString stringWithFormat:@"%@", message.body] UTF8String]);

/*
// Then pull something from the device using the message body
NSString *version = [[UIDevice currentDevice] valueForKey:message.body];

// Execute some JavaScript using the result?
NSString *exec_template = @"set_headline(\"received: %@\");";
NSString *exec = [NSString stringWithFormat:exec_template, version];
[webView evaluateJavaScript:exec completionHandler:nil];
*/
}

- (void)setRect:(int)width height:(int)height
{
if (webView == nil)
return;
NSRect frame;
frame.size.width = width;
frame.size.height = height;
frame.size.width = width / 2;
frame.size.height = height / 2;
frame.origin.x = 0;
frame.origin.y = 0;
webView.frame = frame;
Expand All @@ -210,7 +300,7 @@ - (void)setVisibility:(BOOL)visibility
{
if (webView == nil)
return;
webView.hidden = visibility ? NO : YES;
// webView.hidden = visibility ? NO : YES;
}

- (void)loadURL:(const char *)url
Expand All @@ -220,7 +310,13 @@ - (void)loadURL:(const char *)url
NSString *urlStr = [NSString stringWithUTF8String:url];
NSURL *nsurl = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:nsurl];
[[webView mainFrame] loadRequest:request];

if ([nsurl.absoluteString hasPrefix:@"file:"]) {
NSURL *top = [NSURL URLWithString:[[nsurl absoluteString] stringByDeletingLastPathComponent]];
[webView loadFileURL:nsurl allowingReadAccessToURL:top];
} else {
[webView loadRequest:request];
}
}

- (void)loadHTML:(const char *)html baseURL:(const char *)baseUrl
Expand All @@ -230,15 +326,15 @@ - (void)loadHTML:(const char *)html baseURL:(const char *)baseUrl
NSString *htmlStr = [NSString stringWithUTF8String:html];
NSString *baseStr = [NSString stringWithUTF8String:baseUrl];
NSURL *baseNSUrl = [NSURL URLWithString:baseStr];
[[webView mainFrame] loadHTMLString:htmlStr baseURL:baseNSUrl];
[webView loadHTMLString:htmlStr baseURL:baseNSUrl];
}

- (void)evaluateJS:(const char *)js
{
if (webView == nil)
return;
NSString *jsStr = [NSString stringWithUTF8String:js];
[webView stringByEvaluatingJavaScriptFromString:jsStr];
[webView evaluateJavaScript:jsStr completionHandler:nil];
}

- (BOOL)canGoBack
Expand Down Expand Up @@ -274,7 +370,7 @@ - (void)update:(int)x y:(int)y deltaY:(float)deltaY buttonDown:(BOOL)buttonDown
if (webView == nil)
return;

NSView *view = [[[webView mainFrame] frameView] documentView];
NSView *view = webView;
NSGraphicsContext *context = [NSGraphicsContext currentContext];
NSEvent *event;
NSString *characters;
Expand Down Expand Up @@ -320,7 +416,7 @@ - (void)update:(int)x y:(int)y deltaY:(float)deltaY buttonDown:(BOOL)buttonDown
CFRelease(cgEvent);
[view scrollWheel:scrollEvent];
}

@synchronized(self) {
if (bitmap == nil)
bitmap = [[webView bitmapImageRepForCachingDisplayInRect:webView.frame] retain];
Expand Down
4 changes: 2 additions & 2 deletions plugins/Mac/WebView.bundle.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 34 additions & 7 deletions plugins/Mac/WebView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
81E2C1FF14C5684A004CE5C2 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0420;
LastUpgradeCheck = 0820;
};
buildConfigurationList = 81E2C20214C5684A004CE5C2 /* Build configuration list for PBXProject "WebView" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -183,11 +183,22 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = YES;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand All @@ -198,9 +209,12 @@
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "";
MACOSX_DEPLOYMENT_TARGET = 10.6;
MACOSX_DEPLOYMENT_TARGET = 10.10;
ONLY_ACTIVE_ARCH = YES;
OTHER_LDFLAGS = (
"-L/Applications/Unity/Unity.app/Contents/Frameworks/MonoEmbedRuntime/osx",
Expand All @@ -214,18 +228,31 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = YES;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = "";
MACOSX_DEPLOYMENT_TARGET = 10.6;
MACOSX_DEPLOYMENT_TARGET = 10.10;
OTHER_LDFLAGS = (
"-L/Applications/Unity/Unity.app/Contents/Frameworks/MonoEmbedRuntime/osx",
"-lmono.0",
Expand All @@ -237,13 +264,13 @@
81E2C21B14C5684A004CE5C2 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Resources/Prefix.pch;
INFOPLIST_FILE = Resources/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Bundles";
LD_RUNPATH_SEARCH_PATHS = "";
LIBRARY_SEARCH_PATHS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = "net.gree.unitywebview.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = bundle;
};
Expand All @@ -252,13 +279,13 @@
81E2C21C14C5684A004CE5C2 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = Resources/Prefix.pch;
INFOPLIST_FILE = Resources/Info.plist;
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Bundles";
LD_RUNPATH_SEARCH_PATHS = "";
LIBRARY_SEARCH_PATHS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = "net.gree.unitywebview.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = bundle;
};
Expand Down
Loading