cocos2dx接入微信分享(IOS)

11 篇文章 0 订阅

留着记录!

首先注册微信的id

在工程中新建WeiXinShare.h和.mm文件

头文件


#ifndef __ProjectTPP_IOS__WeiXinShare__
#define __ProjectTPP_IOS__WeiXinShare__

#include <iostream>

#endif 

class WeiXinShare {
    
public:
    static void registerWeixinForIOS(char* appID);
    static void ShareToTimeLine();
    static void ShareToFriend();
public:
    static void setUIViewController(void* viewController);
    static void setShareContent();
};

源文件

#include "WeiXinShare.h"
#import "WXApi.h"


static WXScene TimeLine_scene = WXSceneTimeline; // 发送到微信朋友圈
static WXScene Firend_scene = WXSceneSession; // 发送到微信好友
static UIViewController* m_viewController;


static NSString* SHARE_APP_NAME;
static NSString* SHARE_CAPTION;
static NSString* SHARE_DESCRIPTION;
static NSString* SHARE_LINK = @"https://www.baidu.com";

void WeiXinShare::setShareContent(){
    SHARE_APP_NAME = @"分享测试";
    SHARE_CAPTION = @"分享测试";
    SHARE_DESCRIPTION =@"分享测试";
    
}

void WeiXinShare::setUIViewController(void* viewController){
    m_viewController = (UIViewController*)viewController;
}


void WeiXinShare::registerWeixinForIOS(char *appID){
    [WXApi registerApp:[NSString stringWithUTF8String:appID] withDescription:nil];
}

void WeiXinShare::ShareToTimeLine(){
    setShareContent();
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = SHARE_DESCRIPTION;
    message.description = SHARE_DESCRIPTION;
    [message setThumbImage:[UIImage imageNamed:@"icon_114.png"]];
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = SHARE_LINK;
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
    req.bText = NO;
    req.message = message;
    req.scene = TimeLine_scene;
    
    [WXApi sendReq:req];
}

void WeiXinShare::ShareToFriend(){
	setShareContent();
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = SHARE_CAPTION;
    message.description = SHARE_DESCRIPTION;
    [message setThumbImage:[UIImage imageNamed:@"icon_114.png"]];
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = SHARE_LINK;
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[[SendMessageToWXReq alloc] init]autorelease];
    req.bText = NO;
    req.message = message;
    req.scene = Firend_scene;
    
    [WXApi sendReq:req];
}
然后在AppController.h中的AppController加入WXApiDelegate

@interface AppController : NSObject <UIApplicationDelegate, MPInterstitialAdControllerDelegate, VungleSDKDelegate, AdColonyDelegate, AdColonyAdDelegate, UnityAdsDelegate,WXApiDelegate> {

}

在源文件的didFinishLaunchingWithOptions函数中注册微信分享

    //weixin
    WeiXinShare::registerWeixinForIOS("12345678");

修改以下两函数

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return  [WXApi handleOpenURL:url delegate:self];
}

还要添加微信的回调,觉得可写可不写

//微信

-(void) onReq:(BaseReq*)req
{
//    if([req isKindOfClass:[GetMessageFromWXReq class]])
//    {
//        GetMessageFromWXReq *temp = (GetMessageFromWXReq *)req;
//        
//        // 微信请求App提供内容, 需要app提供内容后使用sendRsp返回
//        NSString *strTitle = [NSString stringWithFormat:@"微信请求App提供内容"];
//        NSString *strMsg = [NSString stringWithFormat:@"openID: %@", temp.openID];
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        alert.tag = 1000;
//        [alert show];
//        [alert release];
//    }
//    else if([req isKindOfClass:[ShowMessageFromWXReq class]])
//    {
//        ShowMessageFromWXReq* temp = (ShowMessageFromWXReq*)req;
//        WXMediaMessage *msg = temp.message;
//        
//        //显示微信传过来的内容
//        WXAppExtendObject *obj = msg.mediaObject;
//        
//        NSString *strTitle = [NSString stringWithFormat:@"微信请求App显示内容"];
//        NSString *strMsg = [NSString stringWithFormat:@"openID: %@, 标题:%@ \n内容:%@ \n附带信息:%@ \n缩略图:%u bytes\n附加消息:%@\n", temp.openID, msg.title, msg.description, obj.extInfo, msg.thumbData.length, msg.messageExt];
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
//        [alert release];
//    }
//    else if([req isKindOfClass:[LaunchFromWXReq class]])
//    {
//        LaunchFromWXReq *temp = (LaunchFromWXReq *)req;
//        WXMediaMessage *msg = temp.message;
//        
//        //从微信启动App
//        NSString *strTitle = [NSString stringWithFormat:@"从微信启动"];
//        NSString *strMsg = [NSString stringWithFormat:@"openID: %@, messageExt:%@", temp.openID, msg.messageExt];
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
//        [alert release];
//    }
}

-(void) onResp:(BaseResp*)resp
{
//    if([resp isKindOfClass:[SendMessageToWXResp class]])
//    {
//        NSString *strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
//        NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
//        [alert release];
//    }
//    else if([resp isKindOfClass:[SendAuthResp class]])
//    {
//        SendAuthResp *temp = (SendAuthResp*)resp;
//        
//        NSString *strTitle = [NSString stringWithFormat:@"Auth结果"];
//        NSString *strMsg = [NSString stringWithFormat:@"code:%@,state:%@,errcode:%d", temp.code, temp.state, temp.errCode];
//        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
//        [alert release];
//    }
//    else if ([resp isKindOfClass:[AddCardToWXCardPackageResp class]])
//    {
//        AddCardToWXCardPackageResp* temp = (AddCardToWXCardPackageResp*)resp;
//        NSMutableString* cardStr = [[[NSMutableString alloc] init] autorelease];
//        for (WXCardItem* cardItem in temp.cardAry) {
//            [cardStr appendString:[NSString stringWithFormat:@"cardid:%@ cardext:%@ cardstate:%lu\n",cardItem.cardId,cardItem.extMsg,cardItem.cardState]];
//        }
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add card resp" message:cardStr delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
//        [alert release];
//    }
}

这样就可以在你想分享的地方调用

WeiXinShare::ShareToTimeLine();

好了,收工!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值