【发布时间】:2020-12-04 05:03:53
【问题描述】:
我想在 Go 中编写一个能够在 MacOS 上打开自定义文件类型 (.slc) 的应用程序。我创建了一个空白的 xcode 项目来获取所有必要的代码,并通过 cgo 将其实现到我的应用程序中。当我双击一个文件时,应用程序打开但抱怨它无法以这种格式打开文件:
这是我的 Info.plist:
实现如下:
/surge/appDelegate_darwin.go
package surge
//#cgo CFLAGS: -x objective-c
//#cgo LDFLAGS: -framework Cocoa
//#include "appDelegate_darwin.h"
import "C"
/surge/appDelegate_darwin.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@end
/surge/appDelegate_darwin.m
#include "appDelegate_darwin.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
NSLog(@"%@", filename);
YES;
}
-(void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
NSLog(@"%@", filenames);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
@end
附加信息:我使用 wails 框架 (https://wails.app) 为应用添加了一个不错的 vue.js 前端,并使用内置的 wails build 命令。
cgo 和 Objective-c 中的其他实现(如自定义协议处理程序)可以工作。
【问题讨论】:
标签: objective-c macos go cocoa cgo