diff --git a/v3/pkg/application/application_darwin.go b/v3/pkg/application/application_darwin.go index 4a88b9a25..90607e0e5 100644 --- a/v3/pkg/application/application_darwin.go +++ b/v3/pkg/application/application_darwin.go @@ -10,6 +10,8 @@ package application #include "application_darwin.h" #include "application_darwin_delegate.h" #include "webview_window_darwin.h" +#include "custom_protocol.h" + #include extern void registerListener(unsigned int event); @@ -177,6 +179,8 @@ import ( "github.com/wailsapp/wails/v3/pkg/events" ) +var openUrlBuffer = make(chan string, 100) + type macosApp struct { applicationMenu unsafe.Pointer parent *App @@ -229,6 +233,9 @@ func (m *macosApp) setApplicationMenu(menu *Menu) { } func (m *macosApp) run() error { + + C.StartCustomProtocolHandler() + if m.parent.options.SingleInstance != nil { cUniqueID := C.CString(m.parent.options.SingleInstance.UniqueID) defer C.free(unsafe.Pointer(cUniqueID)) @@ -262,9 +269,25 @@ func (m *macosApp) GetFlags(options Options) map[string]any { func newPlatformApp(app *App) *macosApp { C.init() - return &macosApp{ + result := &macosApp{ parent: app, } + go result.startUrlOpenProcessor() + return result +} + +func (m *macosApp) startUrlOpenProcessor() { + for url := range openUrlBuffer { + if m.parent.options.Mac.OnUrlOpen != nil { + m.parent.options.Mac.OnUrlOpen(url) + } + } +} + +//export HandleCustomProtocol +func HandleCustomProtocol(url *C.char) { + goUrl := C.GoString(url) + openUrlBuffer <- goUrl } //export processApplicationEvent diff --git a/v3/pkg/application/custom_protocol.h b/v3/pkg/application/custom_protocol.h new file mode 100644 index 000000000..98635f3bf --- /dev/null +++ b/v3/pkg/application/custom_protocol.h @@ -0,0 +1,14 @@ +#ifndef CustomProtocol_h +#define CustomProtocol_h + +#import + +extern void HandleCustomProtocol(char*); + +@interface CustomProtocolSchemeHandler : NSObject ++ (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent; +@end + +void StartCustomProtocolHandler(void); + +#endif /* CustomProtocol_h */ \ No newline at end of file diff --git a/v3/pkg/application/custom_protocol.m b/v3/pkg/application/custom_protocol.m new file mode 100644 index 000000000..b6361cff3 --- /dev/null +++ b/v3/pkg/application/custom_protocol.m @@ -0,0 +1,20 @@ +#include "CustomProtocol.h" + +@implementation CustomProtocolSchemeHandler ++ (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { + [event paramDescriptorForKeyword:keyDirectObject]; + + NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; + + HandleCustomProtocol((char*)[[[event paramDescriptorForKeyword:keyDirectObject] stringValue] UTF8String]); +} +@end + +void StartCustomProtocolHandler(void) { + NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager]; + + [appleEventManager setEventHandler:[CustomProtocolSchemeHandler class] + andSelector:@selector(handleGetURLEvent:withReplyEvent:) + forEventClass:kInternetEventClass + andEventID: kAEGetURL]; +} \ No newline at end of file