自定义集合分享(如QQ,微信等)
自定义集合分享(如QQ,微信等)
萍水相逢OSC 发表于2年前
自定义集合分享(如QQ,微信等)
  • 发表于 2年前
  • 阅读 148
  • 收藏 0
  • 点赞 2
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: 通过集合自己需要的一些第三方分享平台,来实现类似于友盟的分享

开始准备步骤跟友盟一样的,这里忽略(例如注册账号,在项目中加入AppKey等)


以下代码是集合分享的调用方法(.h文件):

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MBProgressHUD.h>

#import "WXApiObject.h"

@interface SharesObjects : NSObject

@property (nonatomic, strong) MBProgressHUD *HUD;

//微信
+ (BOOL)sendWeiXinLinkURL:(NSString *)urlString
            TagName:(NSString *)tagName
              Title:(NSString *)title
        Description:(NSString *)description
         ThumbImage:(UIImage *)thumbImage
            InScene:(enum WXScene)scene;

//QQ
+ (void)sendQQWithURL:(NSURL*)url
                title:(NSString*)title
          description:(NSString*)description
     previewImageData:(NSData*)data;

//WeiBo
+ (void)sendWeiBoWithURL:(NSURL*)url
                    title:(NSString*)title
              description:(NSString*)description
         previewImageData:(NSData*)data;

@end

以下代码是完成分享的调用方法(.m文件):

#import "SharesObjects.h"

#import "WXApi.h"
#import "WeiboSDK.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/TencentOAuth.h>

@implementation SharesObjects

#pragma mark - 分享微信
+ (WXMediaMessage *)messageWithTitle:(NSString *)title
                         Description:(NSString *)description
                              Object:(id)mediaObject
                          MessageExt:(NSString *)messageExt
                       MessageAction:(NSString *)action
                          ThumbImage:(UIImage *)thumbImage
                            MediaTag:(NSString *)tagName {
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = description;
    message.mediaObject = mediaObject;
    message.messageExt = messageExt;
    message.messageAction = action;
    message.mediaTagName = tagName;
    [message setThumbImage:thumbImage];
    
    return message;
}

+ (SendMessageToWXReq *)requestWithText:(NSString *)text
                         OrMediaMessage:(WXMediaMessage *)message
                                  bText:(BOOL)bText
                                InScene:(enum WXScene)scene {
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = bText;
    req.scene = scene;
    if (bText)
        req.text = text;
    else
        req.message = message;
    
    return req;
}


+ (BOOL)sendWeiXinLinkURL:(NSString *)urlString
            TagName:(NSString *)tagName
              Title:(NSString *)title
        Description:(NSString *)description
         ThumbImage:(UIImage *)thumbImage
            InScene:(enum WXScene)scene {
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = urlString;
    
    WXMediaMessage *message = [self messageWithTitle:title
                                         Description:description
                                              Object:ext
                                          MessageExt:nil
                                       MessageAction:nil
                                          ThumbImage:thumbImage
                                            MediaTag:tagName];
    
    SendMessageToWXReq* req = [self requestWithText:nil
                                     OrMediaMessage:message
                                              bText:NO
                                            InScene:scene];
    return [WXApi sendReq:req];
}

#pragma mark - 分享QQ好友

+ (void)sendQQWithURL:(NSURL*)url title:(NSString*)title description:(NSString*)description previewImageData:(NSData*)data
{
    TencentOAuth *tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1104759057" andDelegate:nil];
    tencentOAuth.redirectURI = @"www.qq.com";
    
    QQApiNewsObject *newsObj = [QQApiNewsObject
                                objectWithURL:url
                                title:title
                                description:description
                                previewImageData:data];

    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    //将内容分享到qzone
//    QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    
    [self handleSendResult:sent];
}

+ (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
        case EQQAPIAPPNOTREGISTED:
        {
            NSLog(@"App未注册");
            
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            NSLog(@"发送参数错误");
            
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            NSLog(@"未安装手机QQ");
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            NSLog(@"API接口不支持");
            break;
        }
        case EQQAPISENDFAILD:
        {
            NSLog(@"发送失败");
            break;
        }
        default:
        {
            break;
        }
    }
}

#pragma mark - 微博分享

+ (void)sendWeiBoWithURL:(NSURL*)url title:(NSString*)title description:(NSString*)description previewImageData:(NSData*)data
{
    WBMessageObject *message = [WBMessageObject message];
    
//    //标题、图片
    message.text = [NSString stringWithFormat:@"%@%@", description, url.absoluteString];
    WBImageObject *image = [WBImageObject object];
    image.imageData = data;
    message.imageObject = image;
    
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"http://www.sina.com";
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:nil];

    [WeiboSDK sendRequest:request];
}

@end

 

共有 人打赏支持
粉丝 103
博文 16
码字总数 4469
×
萍水相逢OSC
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: