QQ接入

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


简介

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

第一步:搭建QQSDK


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



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




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

第二步:更改及添加


1.更改类
AppController.mm
  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ 
  2.     return [TencentOAuth HandleOpenURL:url]; 
  3.  
  4. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ 
  5.     return [TencentOAuth HandleOpenURL:url]; 
- (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
  1. #import <Foundation/Foundation.h> 
  2. #import <TencentOpenAPI/TencentOAuth.h> 
  3.  
  4. @interface sdkCall : NSObject<TencentSessionDelegate, TencentApiInterfaceDelegate> 
  5. @property (nonatomic, retain)TencentOAuth *tencentOAuth; 
  6. @property (retain, nonatomic)  UITextView *labelTitle; 
  7. @property (retain, nonatomic)  UITextView *labelAccessToken; 
  8. @property (retain, nonatomic) NSArray* permissions; 
  9. @end 
#import <Foundation/Foundation.h>
#import <TencentOpenAPI/TencentOAuth.h>

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

sdkCall.m
  1. // 
  2. //  sdkCall.m 
  3. //  QQLogin 
  4. // 
  5. //  Created by IDEA-MAC03 on 13-8-7. 
  6. // 
  7. // 
  8.  
  9. #import "sdkCall.h" 
  10. static sdkCall *g_instance = nil; 
  11.  
  12. @implementation sdkCall 
  13.  
  14. + (sdkCall *)getinstance 
  15.     @synchronized(self) 
  16.     { 
  17.         if (nil == g_instance) 
  18.         { 
  19.             //g_instance = [[sdkCall alloc] init]; 
  20.             g_instance = [[super allocWithZone:nil] init]; 
  21.             // [g_instance setPhotos:[NSMutableArray arrayWithCapacity:1]]; 
  22.             // [g_instance setThumbPhotos:[NSMutableArray arrayWithCapacity:1]]; 
  23.         } 
  24.     } 
  25.      
  26.     return g_instance; 
  27.  
  28.  
  29. + (id)allocWithZone:(NSZone *)zone 
  30.     return [[self getinstance] retain]; 
  31.  
  32.  
  33. -(id)init 
  34.     _tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"222222" andDelegate:self]; 
  35.     _tencentOAuth.redirectURI = @"www.qq.com"
  36.     //_permissions = [[NSArray arrayWithObjects:@"get_user_info", @"add_share", nil] retain]; 
  37.     return self; 
  38.  
  39. //登录时,调用TencetnOAuth对象的authorize方法: 
  40. -(void)login 
  41.     NSArray* permissions = [NSArray arrayWithObjects: 
  42.                             kOPEN_PERMISSION_GET_USER_INFO, 
  43.                             kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, 
  44.                             kOPEN_PERMISSION_ADD_ALBUM, 
  45.                             kOPEN_PERMISSION_ADD_IDOL, 
  46.                             kOPEN_PERMISSION_ADD_ONE_BLOG, 
  47.                             kOPEN_PERMISSION_ADD_PIC_T, 
  48.                             kOPEN_PERMISSION_ADD_SHARE, 
  49.                             kOPEN_PERMISSION_ADD_TOPIC, 
  50.                             kOPEN_PERMISSION_CHECK_PAGE_FANS, 
  51.                             kOPEN_PERMISSION_DEL_IDOL, 
  52.                             kOPEN_PERMISSION_DEL_T, 
  53.                             kOPEN_PERMISSION_GET_FANSLIST, 
  54.                             kOPEN_PERMISSION_GET_IDOLLIST, 
  55.                             kOPEN_PERMISSION_GET_INFO, 
  56.                             kOPEN_PERMISSION_GET_OTHER_INFO, 
  57.                             kOPEN_PERMISSION_GET_REPOST_LIST, 
  58.                             kOPEN_PERMISSION_LIST_ALBUM, 
  59.                             kOPEN_PERMISSION_UPLOAD_PIC, 
  60.                             kOPEN_PERMISSION_GET_VIP_INFO, 
  61.                             kOPEN_PERMISSION_GET_VIP_RICH_INFO, 
  62.                             kOPEN_PERMISSION_GET_INTIMATE_FRIENDS_WEIBO, 
  63.                             kOPEN_PERMISSION_MATCH_NICK_TIPS_WEIBO, 
  64.                             nil]; 
  65.  
  66.      
  67.     [_tencentOAuth authorize:permissions inSafari:NO]; 
  68.   //[[[sdkCall getinstance] _tencentOAuth] authorize:permissions inSafari:NO]; 
  69.  
  70.  
  71.  
  72. //登录完成后,会调用TencentSessionDelegate中关于登录的协议方法。 登录成功: 
  73. -(void)tencentDidLogin 
  74.     _labelTitle.text = @"登录完成"
  75.     if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length]) { 
  76.         // 记录登录用户的OpenID、Token以及过期时间 
  77.         _labelAccessToken.text = _tencentOAuth.accessToken; 
  78.         NSLog(@"*********************"); 
  79.         NSLog(_tencentOAuth.accessToken); 
  80.     } 
  81.     else 
  82.     { 
  83.         _labelAccessToken.text = @"登录不成功 没有获取accesstoken"
  84.     } 
  85.  
  86. //非网络错误导致登录失败: 
  87. -(void)tencentDidNotLogin:(BOOL)cancelled 
  88.     if (cancelled) 
  89.     { 
  90.         _labelTitle.text = @"用户取消登录"
  91.     } 
  92.     else 
  93.     { 
  94.         _labelTitle.text = @"登录失败"
  95.     } 
  96.  
  97.  
  98. //网络错误导致登录失败: 
  99. -(void)tencentDidNotNetWork 
  100.     _labelTitle.text=@"无网络连接,请设置网络"
  101.  
  102. - (void)tencentDidUpdate:(TencentOAuth *)tencentOAuth 
  103. @end 
//
//  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
  1. #ifndef __QQLogin__HSpriteCPP__ 
  2. #define __QQLogin__HSpriteCPP__ 
  3.  
  4. #include "cocos2d.h" 
  5. using namespace cocos2d; 
  6. class HSpriteCPP:public cocos2d::CCSprite { 
  7.      
  8. public
  9.     static HSpriteCPP* hspriteWithFile(const char *spName); 
  10.     void myInit(); 
  11.     virtual ~HSpriteCPP(); 
  12. }; 
  13.  
  14. #endif /* defined(__QQLogin__HSpriteCPP__) */ 
#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
  1. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 
  2. #include "HSpriteOC.h" 
  3. #include "sdkCall.h" 
  4. #include "sdkDemoViewController.h" 
  5. #endif 
  6.  
  7. #include "HSpriteCPP.h" 
  8. HSpriteCPP* HSpriteCPP::hspriteWithFile(const char *spName) 
  9.     HSpriteCPP *pobSprite = new HSpriteCPP(); 
  10.     if (pobSprite && pobSprite->initWithFile(spName)) { 
  11.         pobSprite->myInit(); 
  12.         pobSprite->autorelease(); 
  13.         return pobSprite; 
  14.     } 
  15.     CC_SAFE_DELETE(pobSprite); 
  16.     return NULL; 
  17.  
  18. void HSpriteCPP::myInit() 
  19.     #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 
  20.    //iOS代码 
  21.  
  22.     [[sdkCall alloc] init]; 
  23.     [[sdkCall alloc] login]; 
  24.      
  25.     #else 
  26.     #endif 
  27.  
  28. HSpriteCPP::~HSpriteCPP() 
  29.      
#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
  1. #ifndef __HELLOWORLD_SCENE_H__ 
  2. #define __HELLOWORLD_SCENE_H__ 
  3.  
  4. #include "cocos2d.h" 
  5.  
  6. class HelloWorld : public cocos2d::CCLayer 
  7. public
  8.     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) 
  9.     virtual bool init(); 
  10.  
  11.     // there's no 'id' in cpp, so we recommend to return the class instance pointer 
  12.     static cocos2d::CCScene* scene(); 
  13.      
  14.     // a selector callback 
  15.     void menuCloseCallback(CCObject* pSender); 
  16.      
  17.     void OPenQQ(CCObject* pSender); 
  18.  
  19.     // preprocessor macro for "static create()" constructor ( node() deprecated ) 
  20.     CREATE_FUNC(HelloWorld); 
  21. }; 
  22.  
  23. #endif // __HELLOWORLD_SCENE_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(),添加如下代码:

  1. CCMenuItemImage *pQQItem =                   
  2.   CCMenuItemImage::create("Connect_logo_5.png","Connect_logo_5.png",this,menu_selector(HelloWorld::OPenQQ) ); 
  3. pCloseItem->setPosition( ccp(200,200 )); 
  4. CCMenu* pMenu1 = CCMenu::create(pQQItem, NULL); 
  5.     this->addChild(pMenu1, 2); 
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授受的方法:
  1. void HelloWorld::OPenQQ(CCObject* pSender) 
  2.        HSpriteCPP *sp = HSpriteCPP::hspriteWithFile("Icon.png"); 
  3.        sp->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5,CCDirector::sharedDirector()->getWinSize().height*0.5-100)); 
  4.        this->addChild(sp,0); 
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 ,测试没问题。运行效果如下:




小弟不材,各位网友们,若有什么问题,留言反馈下喔~~~
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值