cocos2d-x 学习日志(3)之QQ平台接入

转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/rexuefengye/article/details/9833851


简介

本文档主要介绍接入QQ平台的一些内容,便于查阅和使用。

第一步:搭建QQSDK


1.下载对应的 SDK, 将sdk这个目录添加到工程中,这样可以在代码中使用QQAPI了。本人选择如下iOS SDK,如下图

cocos2d-x 学习日志(3)之QQ平台接入_第1张图片

2.下载SDK后,打开文件如图:

cocos2d-x 学习日志(3)之QQ平台接入_第2张图片


3.打开《iOS SDK使用说明(MAC).webloc》按流程搭建好SDK。

第二步:更改及添加


1.更改类
AppController.mm
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    return [TencentOAuth HandleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [TencentOAuth HandleOpenURL:url];
}

2.添加类
sdkCall.h
#import 
#import 

@interface sdkCall : NSObject
@property (nonatomic, retain)TencentOAuth *tencentOAuth;
@property (retain, nonatomic)  UITextView *labelTitle;
@property (retain, nonatomic)  UITextView *labelAccessToken;
@property (retain, nonatomic) NSArray* permissions;
@end

sdkCall.m
//
//  sdkCall.m
//  QQLogin
//
//  Created by IDEA-MAC03 on 13-8-7.
//
//

#import "sdkCall.h"
static sdkCall *g_instance = nil;

@implementation sdkCall

+ (sdkCall *)getinstance
{
    @synchronized(self)
    {
        if (nil == g_instance)
        {
            //g_instance = [[sdkCall alloc] init];
            g_instance = [[super allocWithZone:nil] init];
            // [g_instance setPhotos:[NSMutableArray arrayWithCapacity:1]];
            // [g_instance setThumbPhotos:[NSMutableArray arrayWithCapacity:1]];
        }
    }
    
    return g_instance;
}


+ (id)allocWithZone:(NSZone *)zone
{
    return [[self getinstance] retain];
}


-(id)init
{
    _tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"222222" andDelegate:self];
    _tencentOAuth.redirectURI = @"www.qq.com";
    //_permissions = [[NSArray arrayWithObjects:@"get_user_info", @"add_share", nil] retain];
    return self;
}

//登录时,调用TencetnOAuth对象的authorize方法:
-(void)login
{
    NSArray* permissions = [NSArray arrayWithObjects:
                            kOPEN_PERMISSION_GET_USER_INFO,
                            kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
                            kOPEN_PERMISSION_ADD_ALBUM,
                            kOPEN_PERMISSION_ADD_IDOL,
                            kOPEN_PERMISSION_ADD_ONE_BLOG,
                            kOPEN_PERMISSION_ADD_PIC_T,
                            kOPEN_PERMISSION_ADD_SHARE,
                            kOPEN_PERMISSION_ADD_TOPIC,
                            kOPEN_PERMISSION_CHECK_PAGE_FANS,
                            kOPEN_PERMISSION_DEL_IDOL,
                            kOPEN_PERMISSION_DEL_T,
                            kOPEN_PERMISSION_GET_FANSLIST,
                            kOPEN_PERMISSION_GET_IDOLLIST,
                            kOPEN_PERMISSION_GET_INFO,
                            kOPEN_PERMISSION_GET_OTHER_INFO,
                            kOPEN_PERMISSION_GET_REPOST_LIST,
                            kOPEN_PERMISSION_LIST_ALBUM,
                            kOPEN_PERMISSION_UPLOAD_PIC,
                            kOPEN_PERMISSION_GET_VIP_INFO,
                            kOPEN_PERMISSION_GET_VIP_RICH_INFO,
                            kOPEN_PERMISSION_GET_INTIMATE_FRIENDS_WEIBO,
                            kOPEN_PERMISSION_MATCH_NICK_TIPS_WEIBO,
                            nil];

    
    [_tencentOAuth authorize:permissions inSafari:NO];
  //[[[sdkCall getinstance] _tencentOAuth] authorize:permissions inSafari:NO];

}


//登录完成后,会调用TencentSessionDelegate中关于登录的协议方法。 登录成功:
-(void)tencentDidLogin
{
    _labelTitle.text = @"登录完成";
    if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length]) {
        // 记录登录用户的OpenID、Token以及过期时间
        _labelAccessToken.text = _tencentOAuth.accessToken;
        NSLog(@"*********************");
        NSLog(_tencentOAuth.accessToken);
    }
    else
    {
        _labelAccessToken.text = @"登录不成功 没有获取accesstoken";
    }
}

//非网络错误导致登录失败:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
    if (cancelled)
    {
        _labelTitle.text = @"用户取消登录";
    }
    else
    {
        _labelTitle.text = @"登录失败";
    }
}


//网络错误导致登录失败:
-(void)tencentDidNotNetWork
{
    _labelTitle.text=@"无网络连接,请设置网络";
}

- (void)tencentDidUpdate:(TencentOAuth *)tencentOAuth
{
}
@end

HSpriteCPP.h
#ifndef __QQLogin__HSpriteCPP__
#define __QQLogin__HSpriteCPP__

#include "cocos2d.h"
using namespace cocos2d;
class HSpriteCPP:public cocos2d::CCSprite {
    
public:
    static HSpriteCPP* hspriteWithFile(const char *spName);
    void myInit();
    virtual ~HSpriteCPP();
};

#endif /* defined(__QQLogin__HSpriteCPP__) */

HSpriteCPP.mm 
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "HSpriteOC.h"
#include "sdkCall.h"
#include "sdkDemoViewController.h"
#endif

#include "HSpriteCPP.h"
HSpriteCPP* HSpriteCPP::hspriteWithFile(const char *spName)
{
    HSpriteCPP *pobSprite = new HSpriteCPP();
    if (pobSprite && pobSprite->initWithFile(spName)) {
        pobSprite->myInit();
        pobSprite->autorelease();
        return pobSprite;
    }
    CC_SAFE_DELETE(pobSprite);
    return NULL;
}

void HSpriteCPP::myInit()
{
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
   //iOS代码

    [[sdkCall alloc] init];
    [[sdkCall alloc] login];
    
    #else
    #endif
}

HSpriteCPP::~HSpriteCPP()
{
    
}

注: HSpriteCPP.mm 后缀是.mm,为了实现OC和C++混编,可参考:《cocos2d-x 学习日志(1)之Xcode中c++&Object-C混编,详细介绍如何在cocos2dx中访问object函数以及Apple Api》

3.运行测试:

HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    void OPenQQ(CCObject* pSender);

    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp,在init(),添加如下代码:

CCMenuItemImage *pQQItem = 			        
  CCMenuItemImage::create("Connect_logo_5.png","Connect_logo_5.png",this,menu_selector(HelloWorld::OPenQQ) );
pCloseItem->setPosition( ccp(200,200 ));
CCMenu* pMenu1 = CCMenu::create(pQQItem, NULL);
    this->addChild(pMenu1, 2);

调用弹出QQ授受的方法:
void HelloWorld::OPenQQ(CCObject* pSender)
{
       HSpriteCPP *sp = HSpriteCPP::hspriteWithFile("Icon.png");
       sp->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5,CCDirector::sharedDirector()->getWinSize().height*0.5-100));
       this->addChild(sp,0);
}

本人使用 cocos2d-x 2.0.4  ,测试没问题。运行效果如下:

cocos2d-x 学习日志(3)之QQ平台接入_第3张图片 cocos2d-x 学习日志(3)之QQ平台接入_第4张图片


小弟不材,各位网友们,若有什么问题,留言反馈下喔~~~

你可能感兴趣的:(cocos2d-x 学习日志(3)之QQ平台接入)