1.UIActivityViewController是继承自UIViewController,是拥有VC的特性
a.初始化 init , initWithActivityItems:applicationActivities
UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:@[@"Steve Jobs by waiter lsaacson"] applicationActivities:nil];
b.点击某个选项之后所能处理的操作 completionWithItemsHandler(Block, >8.0) completionHandler(Block, <8.0)
activity.completionWithItemsHandler = ^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
NSLog(@"hahahhahaha");
if (completed) {
NSLog(@"completed");
}else{
NSLog(@"cancel");
}
};
c.剔除不需要的操作项 excludedActivityTypes
//该属性 表示去掉不想显示的按钮
activity.excludedActivityTypes = @[UIActivityTypeAirDrop,UIActivityTypeMessage];
d.展示改控制器 (中间一栏的分享型服务,下面一栏的操作型服务)
[self.navigationController presentViewController:activity animated:YES completion:nil]; /*
[self.navigationController pushViewController:activity animated:YES]; 会报错
Snapshotting a view that has not been rendered results in an empty snapshot.
Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
*/
2.自定义选项 Item (UIActivity子类即可)
#import <UIKit/UIKit.h> @interface HGLActivity : UIActivity @end
#import "HGLActivity.h"
NSString *const UIActivityTypeHGLCustomMine = @"HGLCustomActivityMine";
@implementation HGLActivity // default returns nil. subclass may override to return custom activity type that is reported to completion handler
- (NSString *)activityType{
return UIActivityTypeHGLCustomMine;
} // default returns nil. subclass must override and must return non-nil value
- (NSString *)activityTitle{ //展示的问题介绍
return NSLocalizedString(@"HGL Custom", @"");
} // default returns nil. subclass must override and must return non-nil value
- (UIImage *)activityImage{ //展示的图标
UIImage *im = [UIImage imageNamed:@"xl"];
return im;
} //可以被响应
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems{
return YES;
} - (void)prepareWithActivityItems:(NSArray *)activityItems{
NSLog(@"prepareWithActivityItems");
//点击图片之后触发
// UIActivityViewController 传递的 activityItems
} - (void)performActivity{
NSLog(@"performActivity");
// 调用prepareWithActivityItems 之后出发
} + (UIActivityCategory)activityCategory{
// return UIActivityCategoryAction; //下面一栏的操作型服务
return UIActivityCategoryShare ; //中间一栏的分享型服务
} @end
*初始化是UIActivityViewController 最后一个参数为数组
UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:@[@"Steve Jobs by waiter lsaacson",[[NSBundle mainBundle] URLForResource:@"hgl29" withExtension:@"txt"]] applicationActivities:@[[[HGLActivity alloc]init]]];