我有一个macOS应用程序,我想通过空投只支持共享。是否有一种方法可以让用户直接进入空投对话框,而不需要通过共享菜单UI?谢谢。
评论中建议的链接有助于找到相关资源。以下是通过空投直接共享的确切代码。
std::string filepath = "path_to_file";
NSString* fileUrl = [NSString stringWithUTF8String:filepath.c_str()];
NSURL *url = [NSURL fileURLWithPath:fileUrl];
std::string fileName = "file_name";
NSString* fileName = [NSString fileName.c_str()];
NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameSendViaAirDrop];
NSArray *items = @[fileName, url];
if (![service canPerformWithItems:items]) {
NSLog(@"Can't share that kind of stuff, sorry!");
return;
}
[service performWithItems:items];