cocos2dx-IOS微信分享

一、进入微信开放平台点击打开链接

  • 点击进入“开放平台”

  • 点击进入“资源中心”

  • 点击“移动应用”
    cocos2dx-IOS微信分享_第1张图片
  • 点击“iOS接入指南”后,点击文档最下方微信SDK Sample Demo源码

二、把下载好的demo找到图中文件,放到一个文件夹SDK1.6.2中

cocos2dx-IOS微信分享_第2张图片

C2DShareInstance.h C2DShareInstance.mm(.mm是用于C++与IOS混编)是用于cocos2dx 与 iOS 进行交互的,内部实现如下:
  1. //C2DShareInstance.h
    #ifndef C2DShareInstance_h
    #define C2DShareInstance_h
    
    #include "cocos2d.h"
    
    class C2DXShareInstance
    {
    protected:
        C2DXShareInstance(){}   //构造函数是私有的
        static C2DXShareInstance *m_pInstance;
        
    public:
        static C2DXShareInstance *GetInstance();
        //直接分享
        void shareContentClickHandler(std::string strLinkURL, std::string strLinkTagName, std::string strLinkTitle, std::string strLinkDescription, std::string strImage, int wxType);
    };

  2. CD2ShareInstance.cpp实现
    //
    //  C2DShareInstance.m
    //  MyGame
    //
    //  Created by apple on 16/1/22.
    //
    //
    
    #import 
    //#import "AppController.h"
    #include "C2DShareInstance.h"
    
    //winxin
    #import "WXApiManager.h"
    #import "WXApiRequestHandler.h"
    #import "RespForWeChatViewController.h"
    #import "Constant.h"
    
    C2DXShareInstance *C2DXShareInstance::m_pInstance = NULL;
    
    C2DXShareInstance* C2DXShareInstance::GetInstance()
    {
        if(m_pInstance == NULL)  //判断是否第一次调用
        {
            //向微信注册
            [WXApi registerApp:@"wx0d5597c5317a4696"];
            m_pInstance = new C2DXShareInstance();
        }
        return m_pInstance;
    }
    
    void C2DXShareInstance::shareContentClickHandler(std::string strLinkURL, std::string strLinkTagName, std::string strLinkTitle, std::string strLinkDescription, std::string strImage, int wxType)
    {
        NSString *nsLinkURL = [[NSString alloc] initWithCString:strLinkURL.c_str()];
        NSString *nsLinkTagName = [[NSString alloc] initWithCString:strLinkTagName.c_str()];
        NSString *nsLinkTitle = [[NSString alloc] initWithCString:strLinkTitle.c_str()];
        NSString *nsLinkDescription = [[NSString alloc] initWithCString:strLinkDescription.c_str()];
        NSString *nsImage= [[NSString alloc] initWithCString:strImage.c_str()];
        
        UIImage *thumbImage = [UIImage imageNamed:nsImage];
        [WXApiRequestHandler sendLinkURL:nsLinkURL
                                 TagName:nsLinkTagName
                                   Title:nsLinkTitle
                             Description:nsLinkDescription
                              ThumbImage:thumbImage
                                 InScene:(WXScene)wxType];
        
        [nsLinkURL release];
        [nsLinkTagName release];
        [nsLinkTitle release];
        [nsLinkDescription release];
        [nsImage release];
        
    //    [WXApiRequestHandler sendLinkURL:kLinkURL
    //                             TagName:kLinkTagName
    //                               Title:kLinkTitle
    //                         Description:kLinkDescription
    //                          ThumbImage:thumbImage
    //                             InScene:WXSceneTimeline];
    }
    
    
    #import 
    
    @interface ShareViewController : UIViewController
    @end
    
    
    
    @interface ShareViewController()
    //@property (nonatomic) enum WXScene currentScene;
    //@property (nonatomic, strong) NSString *appId;
    @end
    
    @implementation ShareViewController
    
    #pragma mark - View Lifecycle
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
        } else {
            return YES;
        }
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    
    #pragma mark - WXApiManagerDelegate
    - (void)managerDidRecvGetMessageReq:(GetMessageFromWXReq *)req {
        // 微信请求App提供内容, 需要app提供内容后使用sendRsp返回
        NSString *strTitle = [NSString stringWithFormat:@"微信请求App提供内容"];
        NSString *strMsg = [NSString stringWithFormat:@"openID: %@", req.openID];
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
                                                        message:strMsg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        alert.tag = kRecvGetMessageReqAlertTag;
        [alert show];
        [alert release];
    }
    
    - (void)managerDidRecvShowMessageReq:(ShowMessageFromWXReq *)req {
        WXMediaMessage *msg = req.message;
    
        //显示微信传过来的内容
        WXAppExtendObject *obj = msg.mediaObject;
    
        NSString *strTitle = [NSString stringWithFormat:@"微信请求App显示内容"];
        NSString *strMsg = [NSString stringWithFormat:@"openID: %@, 标题:%@ \n内容:%@ \n附带信息:%@ \n缩略图:%lu bytes\n附加消息:%@\n", req.openID, msg.title, msg.description, obj.extInfo, (unsigned long)msg.thumbData.length, msg.messageExt];
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
                                                        message:strMsg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    - (void)managerDidRecvLaunchFromWXReq:(LaunchFromWXReq *)req {
        WXMediaMessage *msg = req.message;
    
        //从微信启动App
        NSString *strTitle = [NSString stringWithFormat:@"从微信启动"];
        NSString *strMsg = [NSString stringWithFormat:@"openID: %@, messageExt:%@", req.openID, msg.messageExt];
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
                                                        message:strMsg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    - (void)managerDidRecvMessageResponse:(SendMessageToWXResp *)response {
        NSString *strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
        NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", response.errCode];
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
                                                        message:strMsg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    - (void)managerDidRecvAddCardResponse:(AddCardToWXCardPackageResp *)response {
        NSMutableString* cardStr = [[[NSMutableString alloc] init] autorelease];
        for (WXCardItem* cardItem in response.cardAry) {
            [cardStr appendString:[NSString stringWithFormat:@"cardid:%@ cardext:%@ cardstate:%u\n",cardItem.cardId,cardItem.extMsg,(unsigned int)cardItem.cardState]];
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add card resp"
                                                        message:cardStr
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    - (void)managerDidRecvAuthResponse:(SendAuthResp *)response {
        NSString *strTitle = [NSString stringWithFormat:@"Auth结果"];
        NSString *strMsg = [NSString stringWithFormat:@"code:%@,state:%@,errcode:%d", response.code, response.state, response.errCode];
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle
                                                        message:strMsg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
    
    @end
    其中使用单例模式在调用的C2DXShareInstance* C2DXShareInstance::GetInstance()时候,向微信注册,这个"wx0d5597c5317a4696"是要填写自己AppID。AppID获得,是在管理中心申请的,通过后会告诉你。(这个应该是与该App相关联,如果应用没有注册,那么分享不会成功。)

  3. SDK1.6.2加入你要实现分享的工程当中。
    cocos2dx-IOS微信分享_第3张图片
  4. 添加CoreTelephony.framework libWeChatSDK.a libSqlite3.0tbd libz.tbd库文件,让其能链接编辑。如果不明白怎么添加,可以看看官方文档,写的很详细。

  5. 最后在要分享的地方添加代码
    #if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
         std::string strLinkURL = "http://www.baidu.com";
         std::string strLinkTitle = CUtilEx::getString("app_name");
         std::string strLinkDescription = CUtilEx::getString("room_shared");
         std::string strImage = "Icon-40.png";
         C2DXShareInstance::GetInstance()->shareContentClickHandler(strLinkURL, "", strLinkTitle, "", strImage,1);
    #endif

三、注意事项:

  1. demo是纯IOS,我们使用的时候,要把它剥离出来
  2. 这里只实现了微信分享。微信支付,微信登陆等等没有实现,需要时自行添加。
  3. 关键地方AppID要时自己应用在微信开放平台申请的AppID。

你可能感兴趣的:(cocos2dx)