iOS-QQ的分享与登录

QQ登录与分享 注意:修改必要的工程配置属性。在工程配置中的“Build Settings”一栏中找到“Linking”配置区,给“Other Linker Flags”配置项添加属性值“-fobjc-arc”。
1、首先导入QQ的SDK


2、导入需要的framework 添加SDK依赖的系统库文件。分别是”Security.framework”, “libiconv.dylib”,“SystemConfiguration.framework”,“CoreGraphics.Framework”、“libsqlite3.dylib”、“CoreTelephony.framework”、“libstdc++.dylib”、“libz.dylib”。
3、添加URL Type

4、在plist文件里添加iOS9 URL Schemes 白名单

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>wechat</string>
        <string>weixin</string>
        <string>sinaweibohd</string>
        <string>sinaweibo</string>
        <string>sinaweibosso</string>
        <string>weibosdk</string>
        <string>weibosdk2.5</string>
        <string>mqqapi</string>
        <string>mqq</string>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqqconnect</string>
        <string>mqqopensdkdataline</string>
        <string>mqqopensdkgrouptribeshare</string>
        <string>mqqopensdkfriend</string>
        <string>mqqopensdkapi</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqopensdkapiV3</string>
        <string>mqzoneopensdk</string>
        <string>wtloginmqq</string>
        <string>wtloginmqq2</string>
        <string>mqqwpa</string>
        <string>mqzone</string>
        <string>mqzonev2</string>
        <string>mqzoneshare</string>
        <string>wtloginqzone</string>
        <string>mqzonewx</string>
        <string>mqzoneopensdkapiV2</string>
        <string>mqzoneopensdkapi19</string>
        <string>mqzoneopensdkapi</string>
        <string>mqzoneopensdk</string>
        <string>alipay</string>
        <string>alipayshare</string>
    </array>

5、向终端程序注册第三方应用,并在第三方应用实现从qq返回在AppDelegate.m中引入#import头文件,然后写入如下:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options{    [TencentOAuth HandleOpenURL:url];    return YES;}- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{    [TencentOAuth HandleOpenURL:url];        return YES;}- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {    [TencentOAuth HandleOpenURL:url];    return YES;}

6、QQ分享—>在控制器里面添加分享代码

#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/TencentMessageObject.h>
#import <TencentOpenAPI/TencentApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/QQApiInterface.h> t遵循TencentSessionDelegate协议
TencentOAuth *_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"xxxxxxxxxxxx" andDelegate:self];如果不写TencentOAuth *_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"xxxxxxxxxxxx" andDelegate:self]; 
//会提示(EQQAPIAPPNOTREGISTED )App未注册的错误
//分享给QQ好友文字
- (void)sendTextMessageToQQ
{
    QQApiTextObject *txtObj = [QQApiTextObject objectWithText:@"QQ互联测试"];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    
    [self handleSendResult:sent];
}
- (void)sendImageMessageToQQ
{
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"IconWX"];
    NSData* data = [NSData dataWithContentsOfFile:path];
    
    QQApiImageObject* img = [QQApiImageObject objectWithData:data previewImageData:data title:@"分享图片" description:@"分享123456图片到QQ"];
    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];
    
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    [self handleSendResult:sent];
}
- (void)sendWebMessageToQQ
{
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"IconWX"];
    NSData* data = [NSData dataWithContentsOfFile:path];
    NSURL* url = [NSURL URLWithString:@"http://sports.qq.com/a/20120510/000650.htm"];
    
    QQApiNewsObject* img = [QQApiNewsObject objectWithURL:url title:@"天公作美伦敦奥运圣火点燃成功 火炬传递开启" description:@"腾讯体育讯 当地时间5月10日中午,阳光和全世界的目光聚焦于希腊最高女祭司手中的火炬上,5秒钟内世界屏住呼吸。火焰骤然升腾的瞬间,古老的号角声随之从赫拉神庙传出——第30届伦敦夏季奥运会圣火在古奥林匹亚遗址点燃。取火仪式前,国际奥委会主席罗格、希腊奥委会主席卡普拉洛斯和伦敦奥组委主席塞巴斯蒂安-科互赠礼物,男祭司继北京奥运会后,再度出现在采火仪式中。" previewImageData:data];
    //    [img setCflag:kQQAPICtrlFlagQQShare];
    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];
    
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    [self handleSendResult:sent];
    
}

- (void)sendWebMessageToQQZone
{
    //   发送网络图片
    NSURL *previewURL = [NSURL URLWithString:@"http://img1.gtimg.com/sports/pics/hv1/87/16/1037/67435092.jpg"];
    NSURL* urlq = [NSURL URLWithString:@"http://sports.qq.com/a/20120510/000650.htm"];
    
    QQApiNewsObject* imgq = [QQApiNewsObject objectWithURL:urlq title:@"天公作美伦敦奥运圣火点燃成功 火炬传递开启" description:@"腾讯体育讯 当地时间5月10日中午,阳光和全世界的目光聚焦于希腊最高女祭司手中的火炬上,5秒钟内世界屏住呼吸。火焰骤然升腾的瞬间,古老的号角声随之从赫拉神庙传出——第30届伦敦夏季奥运会圣火在古奥林匹亚遗址点燃。取火仪式前,国际奥委会主席罗格、希腊奥委会主席卡普拉洛斯和伦敦奥组委主席塞巴斯蒂安-科互赠礼物,男祭司继北京奥运会后,再度出现在采火仪式中。" previewImageURL:previewURL];
    [imgq setCflag:kQQAPICtrlFlagQZoneShareOnStart];
    SendMessageToQQReq* reqq = [SendMessageToQQReq reqWithContent:imgq];
    
    QQApiSendResultCode sentq = [QQApiInterface sendReq:reqq];
    [self handleSendResult:sentq];
}
//QQ分享的操作
- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
        case EQQAPIAPPNOTREGISTED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        default:
        {
            break;
        }
    }
}

7、QQ登录

#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/TencentMessageObject.h>
#import <TencentOpenAPI/TencentApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/QQApiInterface.h>
//遵循TencentSessionDelegate协议 添加属性TencentOAuth //*_tencentOAuth;      //初始化
_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105236091" andDelegate:self];     _tencentOAuth.redirectURI = @“www.qq.com";                                     //qq登录
- (void)onClickQQLoginBtn
{
    NSArray* permissions = [NSArray arrayWithObjects:
                            kOPEN_PERMISSION_GET_USER_INFO,
                            kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
                            kOPEN_PERMISSION_ADD_ALBUM,
                            kOPEN_PERMISSION_ADD_ONE_BLOG,
                            kOPEN_PERMISSION_ADD_SHARE,
                            kOPEN_PERMISSION_ADD_TOPIC,
                            kOPEN_PERMISSION_CHECK_PAGE_FANS,
                            kOPEN_PERMISSION_GET_INFO,
                            kOPEN_PERMISSION_GET_OTHER_INFO,
                            kOPEN_PERMISSION_LIST_ALBUM,
                            kOPEN_PERMISSION_UPLOAD_PIC,
                            kOPEN_PERMISSION_GET_VIP_INFO,
                            kOPEN_PERMISSION_GET_VIP_RICH_INFO,
                            nil];
    //@[@"get_user_info", @"get_simple_userinfo", @"add_t"]
    //授权登录
    [_tencentOAuth authorize:permissions inSafari:NO];
}                                                                                                              登录成功之后调用代理方法(协议必须实现的三个方法)                                                 //qq登录成功之后  在协议的方法中返回信息
- (void)tencentDidLogin
{
    if ([_tencentOAuth.accessToken length] > 0 && _tencentOAuth.accessToken) {
        NSLog(@"success%@-->%@-->%@-->%@-->%@",_tencentOAuth.openId, _tencentOAuth.accessToken, _tencentOAuth.appId, _tencentOAuth.openId, _tencentOAuth.expirationDate);
        
        [[NSUserDefaults standardUserDefaults] setObject:@{@"accessToken":_tencentOAuth.accessToken, @"expirationDate":_tencentOAuth.expirationDate, @"openId":_tencentOAuth.openId, @"appId":_tencentOAuth.appId} forKey:QQSaveToken];
        [_tencentOAuth getUserInfo]; //调用此方法会在- (void)getUserInfoResponse:(APIResponse *)response中返回数据
    }
    else {
        NSLog(@"fail%@-->%@-->%@-->%@-->%@",_tencentOAuth.openId, _tencentOAuth.accessToken, _tencentOAuth.appId, _tencentOAuth.openId, _tencentOAuth.expirationDate);
    } 
}
- (void)getUserInfoResponse:(APIResponse *)response
{
//    NSLog(@"--->respons:%@",response.jsonResponse);
    [[NSUserDefaults standardUserDefaults] setObject:@{@"headerImage":response.jsonResponse[@"figureurl_qq_2"], @"nickname":response.jsonResponse[@"nickname"], @"description":[NSString stringWithFormat:@"%@-%@ %@", response.jsonResponse[@"province"], response.jsonResponse[@"city"], response.jsonResponse[@"gender"]]} forKey:QQResponse_UserInfo];
    [self parseQQUserShow];
}
- (void)parseQQUserShow
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:QQResponse_UserInfo]) {
        
        NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:QQResponse_UserInfo];
        NSLog(@"-->QQ个人信息UserShow%@", dict);
        [_WXheaderImageView sd_setImageWithURL:[NSURL URLWithString:dict[@"headerImage"]] placeholderImage:nil options:SDWebImageProgressiveDownload];
        _WXnameLabel.text = dict[@"nickname"];
        _WXdescriptionLabel.text = dict[@"description"];
    }
}
- (void)tencentDidNotLogin:(BOOL)cancelled
{
    NSLog(@"tencentDidNotLogin");
    if (cancelled)
    {
        NSLog(@"tencentDidNotLogin用户取消登录");
    }else{
        NSLog(@"tencentDidNotLogin登录失败");
    }
}
- (void)tencentDidNotNetWork
{
    NSLog(@"tencentDidNotNetWork无网络连接,请设置网络");
}

8、iOS开发 -键加入qq群

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,601评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,367评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,249评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,539评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,967评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,929评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,098评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,825评论 0 207
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,609评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,796评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,282评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,603评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,277评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,159评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,959评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,079评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,874评论 2 277

推荐阅读更多精彩内容