系统自带分享的一些问题以及自定义功能

时间:2022-08-31 16:03:15

之前友盟分享更新之后在ios10下会有一些问题,在ipad下显示的也不正确,又重新改回到系统的分享,系统分享可以加入自己所需要的功能,比如点赞、举报等功能,首先介绍一下导入微信和QQ时遇到的问题

分享的按钮点击事件

NSArray *activityItems;//建立数组,里面存储需要分享的内容
    self.sharingText = [NSString stringWithFormat:@"《%@》真棒,太好听了,我推荐给大家,下载地址:http://itunes.apple.com/cn/app/id%@",[[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"],APP_ID];
//需要分享的文字,[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]获取应用名称
    self.sharingImage = [UIImage imageNamed:@“shareImage”];
//需要分享的图片
    if (self.sharingImage != nil) {
        activityItems = @[self.sharingText, self.sharingImage, [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@", APP_ID]]];
    } else {
        activityItems = @[self.sharingText, [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@", APP_ID]]];
    }

    WeixinSessionActivity *weixinSessionActivity = [[WeixinSessionActivity alloc] init];
    WeixinTimelineActivity *weixinTimelineActivity = [[WeixinTimelineActivity alloc] init];
    WeiXinFavoriteActivity *weixinFavoriteActivity = [[WeiXinFavoriteActivity alloc] init];

    JuBaoActivity *juBaoActivity = [[JuBaoActivity alloc] init];//自己写的功能
    QQQQActivity *qQQQActivity = [[QQQQActivity alloc] init];
    QQZoneActivity *qQZoneActivity = [[QQZoneActivity alloc] init];
    //初始化需要添加的分享平台
    NSArray* myItems = [NSArray arrayWithObjects: weixinSessionActivity, weixinTimelineActivity, weixinFavoriteActivity, qQQQActivity, qQZoneActivity, juBaoActivity, nil];//将你后加入的分享平台写入

    NSArray *excludeActivities = @[UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo, UIActivityTypeMessage, UIActivityTypePrint, UIActivityTypeCopyToPasteboard];//里面写你不想出现在分享中的一些系统自带的平台

    // view controller 生成
    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:myItems];//传给系统分享方法

    // 除外サービスを指定
    activityController.excludedActivityTypes = excludeActivities;
    if (isPad && is_iOS8) {//在ios8下还有iPad上弹出的位置需要我们自己去给,一般默认是从底部居中,加上这些,可以让分享的位置出现在你点击按钮的旁边
        activityController.popoverPresentationController.sourceView = (UIButton *)sender;
        activityController.popoverPresentationController.sourceRect = [(UIButton *)sender bounds];
    }
    [self presentViewController:activityController animated:YES completion:nil];

添加自己需要定制的功能

创建一个UIActivity类型的类,在里面写你想要实现的功能,以举报为例子

- (NSString *)activityType {
    return @"举报内容";
}

- (NSString *)activityTitle {
    return @"举报内容";
}

- (UIImage *)activityImage {
    return [UIImage imageNamed:@"jubao"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems{
    return YES;
}

+ (UIActivityCategory)activityCategory {
    return UIActivityCategoryAction; // or return UIActivityCategoryShare, UIActivityCategoryAction
}

- (void)prepareWithActivityItems:(NSArray *)activityItems;{
    BookLogDebug(@"prepare!!");

    [super prepareWithActivityItems:activityItems];


    //此处会实现你点击后的方法,有什么想要实现的可以写在此处,一般都用发送一个通知,然后去点击分享的界面去接收通知,实现方法



// [ProgressHUD showSuccess:@"举报成功"];
    [self activityDidFinish:YES];
}

- (UIViewController *)activityViewController{
    return nil;
}

- (void)performActivity{
    // this is not called.
}

- (void)activityDidFinish:(BOOL)completed{
    BookLogDebug(@"finished!!");

    [super activityDidFinish:completed];
}

定制QQ、微信分享的显示效果
分别在QQActivityBase.m和WeixinActivityBase.m中的performActivity方法里,其实断点打在这里就可以看到之前写在分享数组里的东西已经传过来了image-显示图片、url-appStore的下载地址、title-分享的文字

微信中

SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.scene = scene;
    //    req.bText = NO;
    req.message = WXMediaMessage.message;
    if (scene == WXSceneSession) {//分享给微信朋友
        req.message.title = [NSString stringWithFormat:@"《%@》", AppName];//名称

        req.message.description = title;//描述文字
    }else{//分享朋友圈等
        req.message.title = title;

        req.message.description = [NSString stringWithFormat:@"《%@》", AppName];
    }
    [self setThumbImage:req];

//图片

if (!image) {//如果图片不存在可以添加一张本地的

        image = [UIImage imageNamed:@"lockImage"];
    }
WXImageObject *imageObject = WXImageObject.object;
        imageObject.imageData = UIImageJPEGRepresentation(image, 1);
        req.message.mediaObject = imageObject;
//下载地址
WXWebpageObject *webObject = WXWebpageObject.object;
        webObject.webpageUrl = [url absoluteString];
        req.message.mediaObject = webObject;
[WXApi sendReq:req];
    [self activityDidFinish:YES];

QQ分享

if (!image) {//同理

        image = [UIImage imageNamed:@"lockImage"]; } //QQ分享把所有的分享内容集合到一个方法里了,只需要调用即可 QQApiURLObject *urlObj = [QQApiURLObject objectWithURL:url title:[NSString stringWithFormat:@"%@》随心所欲想听就听", AppName] description:title previewImageData:UIImageJPEGRepresentation(image, 1.0) targetContentType:QQApiURLTargetTypeNews];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:urlObj];
            //将内容分享到qq [QQApiInterface sendReq:req];