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];

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。