首页 > 代码库 > UIActivityViewController的相关用法(三)--自定义分享方式(比第三方分享库轻便)

UIActivityViewController的相关用法(三)--自定义分享方式(比第三方分享库轻便)

使用UIActivityViewController自定义分享的方式,

以下代码创建一个UIActivityViewController,可以用AirDrop、邮件、QQ、微信分享,AirDrop分享是数据传输,其他三项是文本分享,可以在实现了UIActivityItemSource协议的item类中控制,

// 创建UIActivityViewController,并呈现-(void)createAndShowActivityViewController{    // 构造自定义Activities,只选择系统的 mail 和 airDrop 类型    NSArray *activities = @[[QQActivity new],                            [WeChatActivity new]];    NSArray *excludedActivityTypes = @[UIActivityTypePostToFacebook,                                    UIActivityTypePostToTwitter,                                    UIActivityTypePostToWeibo,                                    UIActivityTypeMessage,                                    //UIActivityTypeMail,                                    UIActivityTypePrint,                                    UIActivityTypeCopyToPasteboard,                                    UIActivityTypeAssignToContact,                                    UIActivityTypeSaveToCameraRoll,                                    UIActivityTypeAddToReadingList,                                    UIActivityTypePostToFlickr,                                    UIActivityTypePostToVimeo,                                    UIActivityTypePostToTencentWeibo];        // 构造待发送的数据项    SharedItem *item = [[SharedItem alloc] initWithData:self.data andLink:@"http://www.baidu.com?pid=12j12kjdlsaj&d=jsdlkjfch9802"];        //构建UIActivityViewController    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:activities];    activityViewController.excludedActivityTypes = excludedActivityTypes;        activityViewController.completionHandler = ^(NSString *activityType, BOOL completed){        // UIActivityViewController 退出时调用此Block        // 无论是否成功,直接退出        [self.delegate backToMainView];    };    [self presentViewController:activityViewController animated:YES completion:nil];}

只贴出微信的Activity类

#import <UIKit/UIKit.h>@interface WeChatActivity : UIActivity@end#import "WeChatActivity.h"@implementation WeChatActivity-(NSString*)activityType{    //相当于 UIActivityTypeAirDrop, 可以用来判断是什么Activity类型    return @"com.hsun.ShareViaWeChat";}-(NSString*)activityTitle{    // 显示的Title    return @"微信";}-(UIImage*)activityImage{    // 显示的图标,ios8及以后是彩色,大小可以查看此方法的文档    return [UIImage imageNamed:@"weixin"];}-(void)prepareWithActivityItems:(NSArray *)activityItems{    //在此做操作,items就是要传输的数据,可以在此直接调用微信的api    NSLog(@"%@",activityItems);}+(UIActivityCategory)activityCategory{    // 决定在UIActivityViewController中显示的位置,最上面是AirDrop,中间是Share,下面是Action    return UIActivityCategoryShare;}-(void)performActivity{    // 没有自定义的UIViewController才会调用此方法。在这里可以做一些操作,需要在最后消除掉UIActivityviewController    [self activityDidFinish:YES];}-(BOOL)canPerformWithActivityItems:(NSArray *)activityItems{    // 可以根据item的不同类型决定是否可以让行为服务操作    return YES;}@end

对应的Item类(在此可以控制不同的Activity做不同的数据传输)

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface SharedItem : NSObject<UIActivityItemSource>-(instancetype)initWithData:(NSData*)data andLink:(NSString*)link;@end#import "SharedItem.h"@interface SharedItem ()@property (nonatomic, strong) NSData *data;@property (nonatomic, strong) NSString *link;@end@implementation SharedItem-(instancetype)initWithData:(NSData *)data andLink:(NSString *)link{    self = [super init];    if (self) {        _data = data;        _link = link;    }    return self;}-(instancetype)init{    @throw [NSException exceptionWithName:@"Do not use" reason:@"Please use initWithData:andLink to initiate." userInfo:nil];    return nil;}#pragma mark - UIActivityItemSource-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController{    return [NSData data];}-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType{    // 如果是通过 AirDrop,直接返回NSData    if ([activityType isEqualToString:UIActivityTypeAirDrop]) {        return self.data;    }    // 其他都是返回链接    return self.link;}-(NSString*)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType{    return @"分享数据";}-(NSString*)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType{    // 如果是通过 AirDrop,直接返回NSData    if ([activityType isEqualToString:UIActivityTypeAirDrop]) {         return @"com.hsun.customUTI.hsundata";    }    // 其他都是返回链接    return nil;}

 

UIActivityViewController的相关用法(三)--自定义分享方式(比第三方分享库轻便)