方案一:設(shè)置 Plist 文件
使用方法設(shè)置參數(shù)狀態(tài):
defaults read com.apple.sharingd DiscoverableMode | grep 'Off'
同時偎痛,可以參考以下代碼監(jiān)聽plist 文件更改來修改airdrop 狀態(tài)
[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);
}
方案二:屏蔽端口
經(jīng)過查詢后何吝,我們發(fā)現(xiàn) airdrop 由 /usr/libexec/sharingd
進(jìn)程提供服務(wù)铺董。執(zhí)行了 man sharingd
后遣铝,得到以下結(jié)果:
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 為關(guān)鍵字查找爬橡,最后找到 airdrop 使用了 tcp: 8770 端口冻河,因此箍邮,我們屏蔽了8770 端口,airdrop 也就無法使用了叨叙。
方案三:關(guān)閉進(jìn)程
airdrop 依賴于 /usr/libexec/sharingd
文件锭弊,因此,我們直接將 sharingd 進(jìn)程干掉即可擂错。
參考
https://stackoverflow.com/questions/52513243/airdrop-disable-detect-and-set-in-macos
http://wildgun.net/2015/12/tcp_8770_used_by_apple_airdrop_handoff/