我一男同事曾经对一晒自己健身照片的女同事留言: "健身三分钟, 拍照两小时", 夸张地道出了当下 '秀出自己' 的时代趋势. 基于以上需求, 手机就有了现在分享功能. 那么App中的分享功能, 是如何实现的呢? 首先看一下效果.
点击分享按钮, 弹出要分享的路径. 其中微博路径需要首先在设置中登录微博账户. 其余的腾讯微博, Twitter等与此同理. 路径也可以自定义实现, 第二个微信图标便是.
点击新浪微博路径后, 弹出系统自定义界面. 中部的文字和右边的按钮图片可以在创建分享控制器UIActivityViewController时传递. 点击自定义微信路径后的分享控制器, 也需要自定义实现.
打开UIActivityViewController类的.h头文件, 可以看到其初始化的方法.
- (instancetype)initWithActivityItems:(NSArray *)activityItems applicationActivities:(nullable NSArray<__kindof UIActivity *> *)applicationActivities
其接受两个数组activityItems和applicationActivities. 其中activityItems表示要分享的具体内容, 比如微博分享界面中间的文字内容, 微信分享界面上的文字和图像; applicationActivities的对象, 指定成了UIActivity的泛型. 打开UIActivity的头文件, 可以看到需要重写的方法.
有了上面的知识, 首先定义泛型UIActivity的实体类CustomActivity. .m实现文件如下.
#import "CustomActivity.h"
#import "WeiXinController.h"
NSString *const CustomActivityMine = @"CustomActivityMine";
@interface CustomActivity ()
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, strong) NSArray *items_toShare;
@property (nonatomic, strong) UINavigationController *nav;
@end
@implementation CustomActivity
- (instancetype)initWithTitie:(NSString *)title withImageName:(NSString *)imageName{
self = [super init];
if (self) {
self.title = title;
self.imageName = imageName;
}
return self;
}
//弹出的控制器
- (UIViewController *)activityViewController{
UIStoryboard *storyB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
WeiXinController *weixinC = [storyB instantiateViewControllerWithIdentifier:@"WeiXinController"];
weixinC.items_toShare = self.items_toShare;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:weixinC];
weixinC.title = @"微信";
weixinC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(complete)];
self.nav = nav;
return nav;
}
- (void)complete {
[self.nav dismissViewControllerAnimated:true completion:^{
NSLog(@"完成发布");
}];
}
//标识路径
- (UIActivityType)activityType{
return CustomActivityMine;
}
//路径名
- (NSString *)activityTitle{
return self.title;
}
//路径图标
- (UIImage *)activityImage{
return [UIImage imageNamed:self.imageName];
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems{
NSLog(@"%@", activityItems);
return true;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems{
self.items_toShare = activityItems;
NSLog(@"%@", activityItems);
}
- (void)activityDidFinish:(BOOL)completed{
NSLog(@"activityDidFinish");
}
//路径位置
+ (UIActivityCategory)activityCategory{
return UIActivityCategoryShare;
}
@end
WeiXinController为点击自定义微信分享路径后要跳转的控制器. 点击分享按钮后会首先执行canPerformWithActivityItems方法, 当返回false时, 该分享路径就会被隐藏. 点击路径跳转控制器之前, 会依次执行prepareWithActivityItems, activityViewController方法, 可以在这两个方法中设计要展示的页面.
activityCategory方法控制分享路径的位置, UIActivityCategoryAction位于分享栏的底部, UIActivityCategoryShare位于分享栏. 两种位置显示的图像背景都会被系统自动渲染, 所以实际中需要把背景色设置为透明.
然后使用自定义CustomActivity.
- (void)share {
//@"我是冬风破10_IOS,欢迎关注我"
NSString *title = @"微信";
NSString *imageName = @"weixin";
CustomActivity * custom = [[CustomActivity alloc] initWithTitie:title withImageName:imageName];
NSArray *activities = @[custom];
NSString *text = @"这一刻的想法...";
UIImage *image = [UIImage imageNamed:@"weixin"];
NSString *subTitle = @"摇一摇, 打开未知世界. ";
NSArray *items = @[text, image, subTitle];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:activities];
if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
activityVC.completionHandler = ^(UIActivityType _Nullable activityType, BOOL completed) {
NSLog(@"completionHandler");
};
}else{
activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
NSLog(@"completionWithItemsHandler");
};
}
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:true completion:nil];
}
completionHandler和completionWithItemsHandler是分享完成后的回调函数, 一般是弹出分享成功的提示框. excludedActivityTypes控制不显示的分享路径, 可以在相关头文件中看到系统可以控制的分享路径.
UIKIT_EXTERN UIActivityType const UIActivityTypePostToFacebook
UIKIT_EXTERN UIActivityType const UIActivityTypePostToTwitter
UIKIT_EXTERN UIActivityType const UIActivityTypePostToWeibo
UIKIT_EXTERN UIActivityType const UIActivityTypeMessage
UIKIT_EXTERN UIActivityType const UIActivityTypeMail
UIKIT_EXTERN UIActivityType const UIActivityTypePrint
UIKIT_EXTERN UIActivityType const UIActivityTypeCopyToPasteboard
UIKIT_EXTERN UIActivityType const UIActivityTypeAssignToContact
UIKIT_EXTERN UIActivityType const UIActivityTypeSaveToCameraRoll
UIKIT_EXTERN UIActivityType const UIActivityTypeAddToReadingList
UIKIT_EXTERN UIActivityType const UIActivityTypePostToFlickr
UIKIT_EXTERN UIActivityType const UIActivityTypePostToVimeo
UIKIT_EXTERN UIActivityType const UIActivityTypePostToTencentWeibo
UIKIT_EXTERN UIActivityType const UIActivityTypeAirDrop
UIKIT_EXTERN UIActivityType const UIActivityTypeOpenInIBooks
喜欢和关注都是对我的鼓励和支持~