在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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...