如何解决在MacOS Catalina上使用自定义协议处理程序运行go应用
我正在尝试编写一个能响应MacOS Catalina中自定义协议处理程序的golang应用程序。我通过使用cgo来遵循本教程:https://blakewilliams.me/posts/handling-macos-url-schemes-with-go
经过一些测试,我陷入了以下错误:
Undefined symbols for architecture x86_64:
"_HandleURL",referenced from:
-[BrowseAppDelegate handleGetURLEvent:withReplyEvent:] in _x003.o
ld: symbol(s) not found for architecture x86_64
我的main.go:
// main.go
package main
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include "browse.h"
*/
import "C"
import (
"os/exec"
)
var urlListener chan string = make(chan string)
func main() {
go C.RunApp()
url := <-urlListener
// replace with implementation
cmd := exec.Command("open","-a","Safari",url)
cmd.Run()
}
//export HandleURL
func HandleURL(u *C.char) {
urlListener <- C.GoString(u)
}
我的浏览器。h:
// browse.h
#import <Cocoa/Cocoa.h>
extern void HandleURL(char*);
@interface BrowseAppDelegate: NSObject<NSApplicationDelegate>
- (void)handleGetURLEvent:(NSAppleEventDescriptor *) event withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
@end
void RunApp();
我的浏览器。m:
// browse.m
#include "browse.h"
@implementation BrowseAppDelegate
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self
andSelector:@selector(handleGetURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass andEventID:kAEGetURL];
}
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
HandleURL((char*)[[[event paramDescriptorForKeyword:keyDirectObject] stringValue] UTF8String]);
}
@end
void RunApp() {
[NSAutoreleasePool new];
[NSApplication sharedApplication];
BrowseAppDelegate *app = [BrowseAppDelegate alloc];
[NSApp setDelegate:app];
[NSApp run];
}
可可中是否有任何更改会破坏代码,或者有更好的方法来获取打开应用程序所用的网址?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。