iOS-史上最全的社交分享封装(QQ,微信,微博,短信,邮件)

1.需求:国内几大常见的社交分享
(只写了具体分享操作的代码,前期的配置和Appdeltegate中的回调url以及第三方注册的相关方法已省略,需要自己添加)
2.SSSocialMessageObject是自己封装的分享对象,按照需求自己定义,包括title,thumImage,url,text(摘要)等
3.有些分享参数不符合条件可能会意外埋坑,比如微信,见代码注释

/**
 具体分享操作

 @param platformType 平台类型
 @param messageObject 具体分享的对象
 @param currentViewController 某些平台需要传的参数
 @param completion 分享完成回调
 */
- (void)shareToPlatform:(SSSocialPlatformType )platformType messageObject:(SSSocialMessageObject *)messageObject currentViewController:(id)currentViewController completion:(SSSocialRequestCompletionHandler)completion{
    NSData *imageData = [[NSData alloc]init];
   //为了满足所有平台的图片类别,将messageObject.thumImage统一处理成NSData类型,需要再去做转换
    if ([messageObject.thumImage isKindOfClass:[NSString class]]) {
        imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:messageObject.thumImage]];  // 如果图片过大,这个处理就会卡顿线程,微信分享的具体结果就是界面卡顿,一直卡顿
    }else if ([messageObject.thumImage isKindOfClass:[UIImage class]]){
        imageData = UIImagePNGRepresentation(messageObject.thumImage);
    }else if ([messageObject.thumImage isKindOfClass:[NSData class]]){
        imageData = messageObject.thumImage;
        
    }
    if (platformType == SSSocialPlatformTypeQQ) {
        
        QQApiNewsObject *object = [QQApiNewsObject objectWithURL:[NSURL URLWithString:messageObject.url] title:messageObject.title description:messageObject.text previewImageData:imageData targetContentType:QQApiURLTargetTypeNews];
        [object setCflag:kQQAPICtrlFlagQQShare];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:object];
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    }else if (platformType == SSSocialPlatformTypeWechatSession || platformType == SSSocialPlatformTypeWechatTimeLine){

        WXMediaMessage *message = [WXMediaMessage message];
        message.title = messageObject.title;
        message.description = messageObject.text;   //@""最大1k,超出限制无法唤起微信
        [message setThumbImage:[UIImage imageWithData:imageData]];
        WXWebpageObject *webpageObject = [WXWebpageObject object];
        webpageObject.webpageUrl = messageObject.url; // 为nil无法唤起微信
        message.mediaObject = webpageObject;
        
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
        req.message = message;
        req.bText = NO;
        
        if (platformType == SSSocialPlatformTypeWechatSession) {
            req.scene = WXSceneSession;
        }else if (platformType == SSSocialPlatformTypeWechatTimeLine){
            req.scene = WXSceneTimeline;
        }
        [WXApi sendReq:req];
    }else if (platformType == SSSocialPlatformTypeSina){
        
        WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
        authRequest.redirectURI = self.redirectURL;
        authRequest.scope = @"all";
        authRequest.userInfo = nil;
        
        WBMessageObject *message = [WBMessageObject message];
        message.text = messageObject.title;
        WBImageObject *image = [WBImageObject object];
        image.imageData = imageData;  // imageData = nil 时将无法唤起微博
        message.imageObject = image;
        WBSendMessageToWeiboRequest *req = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:nil];
        [WeiboSDK sendRequest:req];
    }else if (platformType == SSSocialPlatformTypeMessage){
        
        if ([MFMessageComposeViewController canSendText]) {
            MFMessageComposeViewController *messageViewController = [[MFMessageComposeViewController alloc]init];
            messageViewController.messageComposeDelegate = self;
            messageViewController.body = messageObject.title;
            [currentViewController presentViewController:messageViewController animated:YES completion:nil];

        }else{
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Message" message:@"Your device does not support message function." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
        
    }else if (platformType == SSSocialPlatformTypeEmail){

        MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc]init];
        if (messageObject.text != nil && messageObject.thumImage != nil) {
            
            NSString *imageSource = [NSString stringWithFormat:@"data:image/png;base64,%@",[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
            NSString *email = [NSString stringWithFormat:@"

%@

",messageObject.text,imageSource]; [mailComposeViewController setMessageBody:email isHTML:YES]; } if (messageObject.title != nil) { [mailComposeViewController setSubject:messageObject.title]; } mailComposeViewController.mailComposeDelegate = self; [currentViewController presentViewController:mailComposeViewController animated:YES completion:nil]; } }

你可能感兴趣的:(iOS-史上最全的社交分享封装(QQ,微信,微博,短信,邮件))