on master help sign in
Make some callbacks work in wkwebview
afef776Tom Smalley
jsaddle-wkwebview/cbits-cocoa/AppDelegate.m+42−1
@@ -2,6 +2,8 @@
2#import <Cocoa/Cocoa.h>
2#import <Cocoa/Cocoa.h>
3#import <WebKit/WebKit.h>
3#import <WebKit/WebKit.h>
4
4
5+extern void callIO(HsStablePtr);
6+extern void callWithCString(const char * _Nonnull, HsStablePtr);
5extern void callWithWebView(WKWebView *, HsStablePtr);
7extern void callWithWebView(WKWebView *, HsStablePtr);
6
8
7@interface AppDelegate : NSObject <NSApplicationDelegate>
9@interface AppDelegate : NSObject <NSApplicationDelegate>
@@ -11,6 +13,10 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
11- (instancetype)initApp:(HsStablePtr)handler progName:(NSString *)progName;
13- (instancetype)initApp:(HsStablePtr)handler progName:(NSString *)progName;
12@end
14@end
13
15
16+HsStablePtr global_willFinishLaunchingWithOptions = 0;
17+HsStablePtr global_didFinishLaunchingWithOptions = 0;
18+HsStablePtr global_applicationUniversalLink = 0;
19+
14@implementation AppDelegate
20@implementation AppDelegate
15
21
16- (void)applicationWillTerminate:(NSNotification *)aNotification {
22- (void)applicationWillTerminate:(NSNotification *)aNotification {
@@ -37,12 +43,26 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
37 WKWebView *webView = [[WKWebView alloc] initWithFrame: [_window.contentView frame] configuration:theConfiguration];
43 WKWebView *webView = [[WKWebView alloc] initWithFrame: [_window.contentView frame] configuration:theConfiguration];
38 [_window setContentView:webView];
44 [_window setContentView:webView];
39 callWithWebView(webView, _handler);
45 callWithWebView(webView, _handler);
46+ callIO(global_willFinishLaunchingWithOptions);
47+ // Listen to URL events
48+ NSAppleEventManager *manager = [NSAppleEventManager sharedAppleEventManager];
49+ [manager
50+ setEventHandler:self
51+ andSelector:@selector(applicationUniversalLink:)
52+ forEventClass:kInternetEventClass
53+ andEventID:kAEGetURL];
54+}
55+
56+- (void)applicationUniversalLink:(NSAppleEventDescriptor *)event {
57+ NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
58+ callWithCString([url UTF8String], global_applicationUniversalLink);
40}
59}
41
60
42-(void)applicationDidFinishLaunching:(NSNotification *)notification {
61-(void)applicationDidFinishLaunching:(NSNotification *)notification {
43 [_window orderFrontRegardless];
62 [_window orderFrontRegardless];
44 [_window center];
63 [_window center];
45 [NSApp activateIgnoringOtherApps:YES];
64 [NSApp activateIgnoringOtherApps:YES];
65+ callIO(global_didFinishLaunchingWithOptions);
46}
66}
47
67
48-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app {
68-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app {
@@ -51,8 +71,29 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
51
71
52@end
72@end
53
73
54−void runInWKWebView(HsStablePtr handler, const char * _Nonnull progName) {
74+void runInWKWebView(HsStablePtr handler,
75+ const char * _Nonnull progName,
76+ HsStablePtr hs_willFinishLaunchingWithOptions,
77+ HsStablePtr hs_didFinishLaunchingWithOptions,
78+ HsStablePtr hs_applicationDidBecomeActive,
79+ HsStablePtr hs_applicationWillResignActive,
80+ HsStablePtr hs_applicationDidEnterBackground,
81+ HsStablePtr hs_applicationWillEnterForeground,
82+ HsStablePtr hs_applicationWillTerminate,
83+ HsStablePtr hs_applicationSignificantTimeChange,
84+ HsStablePtr hs_applicationUniversalLink,
85+ const uint64_t hs_requestAuthorizationWithOptions,
86+ const uint64_t hs_requestAuthorizationOptionBadge,
87+ const uint64_t hs_requestAuthorizationOptionSound,
88+ const uint64_t hs_requestAuthorizationOptionAlert,
89+ const uint64_t hs_requestAuthorizationOptionCarPlay,
90+ const uint64_t hs_registerForRemoteNotifications,
91+ HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
92+ HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError) {
55 @autoreleasepool {
93 @autoreleasepool {
94+ global_willFinishLaunchingWithOptions = hs_willFinishLaunchingWithOptions;
95+ global_didFinishLaunchingWithOptions = hs_didFinishLaunchingWithOptions;
96+ global_applicationUniversalLink = hs_applicationUniversalLink;
56 NSApplication *application = [NSApplication sharedApplication];
97 NSApplication *application = [NSApplication sharedApplication];
57 AppDelegate *appDelegate = [[AppDelegate alloc] initApp:handler progName:[NSString stringWithCString:progName encoding:NSUTF8StringEncoding]];
98 AppDelegate *appDelegate = [[AppDelegate alloc] initApp:handler progName:[NSString stringWithCString:progName encoding:NSUTF8StringEncoding]];
58 [application setDelegate:appDelegate];
99 [application setDelegate:appDelegate];