您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx接入微信分享(IOS)

2015-08-19 15:16 330 查看
留着记录!

首先注册微信的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();

好了,收工!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: