iOS开发中活动视图控制器UIActivityViewController的应用
在iOS开发中,UIActivityViewController常用来弹出分享面板,其实除了用来社会化分享,UIActivityViewController还有一大应用是用来进行自定义行为。先看如下示例代码:
1 2 3 4 5 6 7 8 9 10 11 12
| NSString * content = @"活动的内容";
NSURL * url = [NSURL URLWithString:@"https://www.baidu.com"];
UIImage * image = [UIImage imageNamed:@"ios"]; UIActivityViewController * con = [[UIActivityViewController alloc]initWithActivityItems:@[content,url,image] applicationActivities:nil];
con.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray * returnedItems, NSError * __nullable activityError){ NSLog(@"%@\n%@",activityType,returnedItems); }; [self presentViewController:con animated:YES completion:nil];
|
活动面板如下图:
需要注意,活动面板可以分为3个部分,最上面为AirDrop传输功能,中间为分享相关功能,最下面为数据处理功能。UIActivityViewController继承自UIViewController,类解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| - (instancetype)init; - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil; - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;
- (instancetype)initWithActivityItems:(NSArray *)activityItems applicationActivities:(nullable NSArray<__kindof UIActivity *> *)applicationActivitie;
@property(nullable, nonatomic, copy) UIActivityViewControllerCompletionWithItemsHandler completionWithItemsHandler;
@property(nullable, nonatomic, copy) NSArray<UIActivityType> *excludedActivityTypes;
@property(nullable, nonatomic, copy) UIActivityViewControllerCompletionHandler completionHandler;
|
上面初始化方法中有提到activityItems这个参数,系统提供的一些分享与活动行为可支持的数据类型列表如下:
系统提供了一些活动类型,例如分享到微博、脸书、进行添加提示、发送信息等,系统提供的活动类型列举如下(UIActivityType实际上就是NSString*):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| UIActivityType const UIActivityTypePostToFacebook; UIActivityType const UIActivityTypePostToTwitter; UIActivityType const UIActivityTypePostToWeibo; UIActivityType const UIActivityTypeMessage; UIActivityType const UIActivityTypeMail; UIActivityType const UIActivityTypePrint; UIActivityType const UIActivityTypeCopyToPasteboard; UIActivityType const UIActivityTypeAssignToContact; UIActivityType const UIActivityTypeSaveToCameraRoll; UIActivityType const UIActivityTypeAddToReadingList; UIActivityType const UIActivityTypePostToFlickr; UIActivityType const UIActivityTypePostToVimeo; UIActivityType const UIActivityTypePostToTencentWeibo; UIActivityType const UIActivityTypeAirDrop; UIActivityType const UIActivityTypeOpenInIBooks;
|
自定义活动行为需要创建继承于UIActivity类的子类,示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #import "CustomActivity.h"
@implementation CustomActivity
+(UIActivityCategory)activityCategory{ return UIActivityCategoryShare; }
-(UIActivityType)activityType{ return @"Custom"; }
-(NSString *)activityTitle{ return @"title"; }
-(UIImage *)activityImage{ return [UIImage imageNamed:@"ios"]; }
-(BOOL)canPerformWithActivityItems:(NSArray *)activityItems{ NSLog(@"%@",activityItems); return YES; }
- (void)prepareWithActivityItems:(NSArray *)activityItems{ }
-(void)performActivity{ NSLog(@"========="); [self activityDidFinish:YES]; }
@end
|
用自定义的活动对UIActivityViewController进行初始化:
1 2 3 4 5 6 7 8 9
| NSString * content = @"活动的内容"; NSURL * url = [NSURL URLWithString:@"https://www.baidu.com"]; UIImage * image = [UIImage imageNamed:@"ios"]; CustomActivity * activity = [[CustomActivity alloc]init]; UIActivityViewController * con = [[UIActivityViewController alloc]initWithActivityItems:@[content,url,image] applicationActivities:@[activity]]; con.completionWithItemsHandler = ^(UIActivityType activityType, BOOL completed, NSArray * returnedItems, NSError * __nullable activityError){ NSLog(@"%@\n%@",activityType,returnedItems); }; [self presentViewController:con animated:YES completion:nil];
|
效果如下图所示:
UIActivity类解析如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
+ (UIActivityCategory)activityCategory;
- (nullable UIActivityType)activityType;
- (nullable NSString *)activityTitle;
- (nullable UIImage *)activityImage;
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems;
- (void)prepareWithActivityItems:(NSArray *)activityItems;
- (nullable UIViewController *)activityViewController;
- (void)performActivity;
- (void)activityDidFinish:(BOOL)completed;
|