如何使用UIActivityViewController
如何使用UIActivityViewController
SheamUs_lcc 发表于1年前
如何使用UIActivityViewController
  • 发表于 1年前
  • 阅读 10
  • 收藏 0
  • 点赞 2
  • 评论 0

腾讯云实验室 1小时搭建人工智能应用,让技术更容易入门 免费体验 >>>   

AirDrop是iOS7中新增的一个近距离分享功能,两个相邻的iOS设备可以通过AirDrop实现传递图片、文件等功能,有了AirDrop基本上可抛弃类似Dropbox的ShareLink和PublicFolder功能。

iOS7的SDK中已经在UIActivityViewController中内置了AirDrop功能,使用时与分享到twitter和facebook的方式基本一致,只需要将需要传递的文件URL设置给UIActivityViewController对象的ActivityItems对象即可,区别在于:如果传递文件,那么URL应该是“file://”开头,而不是常见的“http://”开头  

参考代码: 

1
2
3
4
5
6
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属性即可。  
以下是可以隐藏的对象 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSArray *excludedActivities = @[UIActivityTypePostToFacebook, 
                UIActivityTypePostToTwitter,
                UIActivityTypePostToWeibo, 
                UIActivityTypeMessage, 
                UIActivityTypeMail, 
                UIActivityTypePrint,
                UIActivityTypeCopyToPasteboard, 
                UIActivityTypeAssignToContact, 
                UIActivityTypeSaveToCameraRoll, 
                UIActivityTypeAddToReadingList,
                UIActivityTypePostToFlickr, 
                UIActivityTypePostToVimeo, 
                UIActivityTypePostToTencentWeibo]; 
activityViewController.excludedActivityTypes = excludedActivities;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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路径下的文件可以使用以下方式 

1
2
3
4
5
6
7
8
9
10
11
12
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];
共有 人打赏支持
粉丝 0
博文 1
码字总数 0
×
SheamUs_lcc
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: