banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

[iOS]使用UIActivityViewController實現AirDrop功能

AirDrop 是 iOS7 中新增的一個近距離分享功能,兩個相鄰的 iOS 設備可以透過 AirDrop 實現傳遞圖片、文件等功能,有了 AirDrop 基本上可拋棄類似 Dropbox 的 ShareLink 和 PublicFolder 功能。

iOS7 的 SDK 中已經在 UIActivityViewController 中內置了 AirDrop 功能,使用時與分享到 twitter 和 facebook 的方式基本一致,只需要將需要傳遞的文件 URL 設置給 UIActivityViewController 對象的 ActivityItems 對象即可,區別在於:如果傳遞文件,那麼 URL 應該是 “file://” 開頭,而不是常見的 “http://” 開頭 參考代碼:

NSString *filePath = "Documents/aaa.png"
NSURL *url = [NSURL fileURLWithPath];
NSArray *items = [NSArray ArrayWithObjects,nil];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems applicationActivities];
[self presentViewController animated completion];

如果只想使用 AirDrop 而不使用其他分享功能,只需要設置 UIActivityViewController 的 excludedActivityTypes 屬性即可。 以下是可以隱藏的對象

#6.0 開始支持
UIKIT_EXTERN NSString *const UIActivityTypePostToFacebook
UIKIT_EXTERN NSString *const UIActivityTypePostToTwitter
UIKIT_EXTERN NSString *const UIActivityTypePostToWeibo
UIKIT_EXTERN NSString *const UIActivityTypeMessage
UIKIT_EXTERN NSString *const UIActivityTypeMail
UIKIT_EXTERN NSString *const UIActivityTypePrint
UIKIT_EXTERN NSString *const UIActivityTypeCopyToPasteboard
UIKIT_EXTERN NSString *const UIActivityTypeAssignToContact
UIKIT_EXTERN NSString *const UIActivityTypeSaveToCameraRoll
#7.0 以後新增
UIKIT_EXTERN NSString *const UIActivityTypeAddToReadingList
UIKIT_EXTERN NSString *const UIActivityTypePostToFlickr
UIKIT_EXTERN NSString *const UIActivityTypePostToVimeo
UIKIT_EXTERN NSString *const UIActivityTypePostToTencentWeibo
UIKIT_EXTERN NSString *const UIActivityTypeAirDrop

具體用法如下,這裡只保留 AirDrop:

NSArray *excludedActivities = @[UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypeMail,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo];
activityViewController.excludedActivityTypes = excludedActivities;

完整的代碼:

NSString *filePath = "Documents/aaa.png"
NSURL *url = [NSURL fileURLWithPath];
NSArray *items = [NSArray ArrayWithObjects,nil];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems applicationActivities];
NSArray *excludedActivities = @[UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypeMail,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo];
activityViewController.excludedActivityTypes = excludedActivities;
[self presentViewController animated completion];

關於文件路徑 URL,一般 APP 的用戶數據都在 APP 的 Documents 目錄下,使用 "Documents/aaa.png" 的方式即可取得 如果需要使用 Bundle 路徑下的文件可以使用以下方式

NSString *fileName=@"aaa.png"
NSArray *fileComponents = [filename componentsSeparatedByString:@"."];
NSString *filePath = nil;
if([fileComponents count] == 0) {
}else if([fileComponents count] == 1) {
filePath = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0]
ofType];
}else if([fileComponents count] == 2) {
filePath = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0]
ofType:[fileComponents objectAtIndex:1]];
}
NSURL *url = [NSURL fileURLWithPath];

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。