方案一:设置 Plist 文件
使用方法设置参数状态:
defaults read com.apple.sharingd DiscoverableMode | grep 'Off'
同时,可以参考以下代码监听plist 文件更改来修改airdrop 状态
[self myMonitoringMethodWithPath:@"/Users/melon/Library/Preferences/com.apple.sharingd.plist"];
- (void) myMonitoringMethodWithPath:(NSString*) path {
__block typeof(self) blockSelf = self;
int fildes = open([path fileSystemRepresentation], O_EVTONLY);
__block dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fildes, DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
unsigned long flags = dispatch_source_get_data(source);
//Do some stuff
if(flags & DISPATCH_VNODE_DELETE) {
[blockSelf myMonitoringMethodWithPath:path];
}
});
dispatch_source_set_cancel_handler(source, ^(void) {
close(fildes);
});
dispatch_resume(source);
}
方案二:屏蔽端口
经过查询后,我们发现 airdrop 由 /usr/libexec/sharingd
进程提供服务。执行了 man sharingd
后,得到以下结果:
sharingd(8) BSD System Manager's Manual sharingd(8)
NAME
sharingd -- Sharing Daemon that enables AirDrop, Handoff, Instant Hotspot, Shared Computers, and Remote Disc in the Finder.
SYNOPSIS
sharingd
DESCRIPTION
sharingd is used by the Finder to enable AirDrop file sharing, Handoff between iCloud devices, Instant Hotspot discovery, connecting to shared com-
puters, and accessing Remote Discs from other computers.
FILES
/usr/libexec/sharingd
HISTORY
sharingd first appeared in Mac OS X 10.9 and iOS 7.
Darwin August 22, 2019 Darwin
以 sharingd 为关键字查找,最后找到 airdrop 使用了 tcp: 8770 端口,因此,我们屏蔽了8770 端口,airdrop 也就无法使用了。
方案三:关闭进程
airdrop 依赖于 /usr/libexec/sharingd
文件,因此,我们直接将 sharingd 进程干掉即可。
参考
https://stackoverflow.com/questions/52513243/airdrop-disable-detect-and-set-in-macos
http://wildgun.net/2015/12/tcp_8770_used_by_apple_airdrop_handoff/