UIActivityViewController是iOS原生弹窗式分享
UIActivityViewController的使用
// 1、设置分享的内容,并将内容添加到数组中
UIImage *shareImage = [UIImage imageNamed:@"shareImage.png"];
NSURL *shareUrl = [NSURL URLWithString:@"https://www.jianshu.com/u/e12f189a320d"];
NSArray *activityItemsArray = @[shareImage,shareUrl];
// 自定义的CustomActivity,继承自UIActivity
CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle:@"" ActivityImage:[UIImage imageNamed:@"custom.png"] URL:shareUrl ActivityType:@"Custom"];
NSArray *activityArray = @[customActivity];
// 2、初始化控制器,添加分享内容至控制器
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:activityArray];
activityVC.modalInPopover = YES;
NSMutableArray *excludeArray = [@[UIActivityTypeAirDrop,UIActivityTypeMessage,UIActivityTypePrint,UIActivityTypePostToTwitter,UIActivityTypePostToFacebook,UIActivityTypeOpenInIBooks,UIActivityTypePostToVimeo,UIActivityTypePostToFlickr,UIActivityTypeAddToReadingList,UIActivityTypeSaveToCameraRoll,UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard,UIActivityTypeMail] mutableCopy];
activityVC.excludedActivityTypes = excludeArray;
// 3、设置回调
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// ios8.0 之后用此方法回调
UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
NSLog(@"activityType == %@",activityType);
if (completed == YES) {
NSLog(@"completed");
}else{
NSLog(@"cancel");
}
};
activityVC.completionWithItemsHandler = itemsBlock;
}else{
// ios8.0 之前用此方法回调
UIActivityViewControllerCompletionHandler handlerBlock = ^(UIActivityType __nullable activityType, BOOL completed){
NSLog(@"activityType == %@",activityType);
if (completed == YES) {
NSLog(@"completed");
}else{
NSLog(@"cancel");
}
};
activityVC.completionHandler = handlerBlock;
}
// 4、调用控制器
[self presentViewController:activityVC animated:YES completion:nil];
CustomActivity.h
- (instancetype)initWithTitle:(NSString *)title ActivityImage:(UIImage *)activityImage URL:(NSURL *)url ActivityType:(NSString *)activityType;
CustomActivity.m
@interface CustomActivity ()
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSURL *url;
@property (nonatomic, copy) UIImage *image;
@property (nonatomic, copy) NSString *type;
@end
- (instancetype)initWithTitle:(NSString *)title ActivityImage:(UIImage *)activityImage URL:(NSURL *)url ActivityType:(NSString *)activityType{
self = [super init];
if (self) {
self.title = title;
self.url = url;
self.image = activityImage;
self.type = activityType;
}
return self;
}
/**
决定自定义CustomActivity在UIActivityViewController中显示的位置。
最上层:AirDrop
中层:Share,即UIActivityCategoryShare
中层:Action,即UIActivityCategoryAction
*/
+ (UIActivityCategory)activityCategory{
return UIActivityCategoryAction;
}
- (NSString *)activityType{
return _type;
}
- (NSString *)activityTitle {
return _title;
}
/**
这个得注意,当self.activityCategory = UIActivityCategoryAction时,系统默认会渲染图片,所以不能重写为 - (UIImage *)activityImage {return _image;}
*/
- (UIImage *)_activityImage {
return _image;
}
- (NSURL *)activityUrl{
return _url;
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
return YES;
}
/**
准备分享所进行的方法,通常在这个方法里面,把item中的东西保存下来,items就是要传输的数据。
*/
- (void)prepareWithActivityItems:(NSArray *)activityItems {
}
/**
1、这里就可以关联外面的app进行分享操作了
2、也可以进行一些数据的保存等操作
3、操作的最后必须使用下面方法告诉系统分享结束了
*/
- (void)performActivity {
[self activityDidFinish:YES];
}
网友评论