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:filePath];
NSArray *items = [NSArray ArrayWithObjects:url,nil];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
如果只想使用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:filePath];
NSArray *items = [NSArray ArrayWithObjects:url,nil];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
NSArray *excludedActivities = @[UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypeMail,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo];
activityViewController.excludedActivityTypes = excludedActivities;
[self presentViewController:activityViewController animated:YES completion:nil];
关于文件路径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:nil];
}else if([fileComponents count] == 2) {
filePath = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0]
ofType:[fileComponents objectAtIndex:1]];
}
NSURL *url = [NSURL fileURLWithPath:filePath];