iOS实现获取系统分享面板item点击回调
UIActivityViewController
提供了completionWithItemsHandler
的block属性,用于回调点击事件
@property(nullable, nonatomic, copy) UIActivityViewControllerCompletionWithItemsHandler completionWithItemsHandler NS_AVAILABLE_IOS(8_0); // set to nil after call
注意!这个block调用后需要置nilset to nil after call
以下方法获取item点击事件有点多余,不过可用该方法对不同的item区分处理分享的数据源
初始化UIActivityViewController
对象
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:nil];
入参的items数组就是分享的内容,比如分享链接,分享文件。当你想要拿到分享面板item的点击事件的时候,这里的入参就不能是简单的URL,Image,而是要实现UIActivityItemSource
协议的对象
UIActivityItemSource
@protocol UIActivityItemSource <NSObject>
@required
/// 返回占位类型,返回分享的具体数据,这个数据不一定要准备,但是类型要一致
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController; // called to determine data type. only the class of the return type is consulted. it should match what -itemForActivityType: returns later
/// 返回对应的type准确的分享数据
- (nullable id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(nullable UIActivityType)activityType; // called to fetch data after an activity is selected. you can return nil.
@optional
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(nullable UIActivityType)activityType; // if activity supports a Subject field. iOS 7.0
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(nullable UIActivityType)activityType; // UTI for item if it is an NSData. iOS 7.0. will be called with nil activity and then selected activity
- (nullable UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(nullable UIActivityType)activityType suggestedSize:(CGSize)size; // if activity supports preview image. iOS 7.0
@end
那么我们要实现获取点击对应item的事件回调,那么我们初始化UIActivityViewController
的时候,就需要传入实现UIActivityItemSource
的对象,这样当我们点击对应的cell的时候,就可以在实现UIActivityItemSource
的对象中的- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(nullable UIActivityType)activityType;
代理方法回调到点击item时机
关于UIActivityType看这个文章
https://www.cnblogs.com/iOSCain/p/5717316.html
网友评论