美文网首页iOS
iOS-QQ的分享与登录

iOS-QQ的分享与登录

作者: BestVast | 来源:发表于2016-04-10 16:50 被阅读2356次

    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无网络连接,请设置网络");
    }
    
    

    相关文章

      网友评论

      本文标题:iOS-QQ的分享与登录

      本文链接:https://www.haomeiwen.com/subject/wwwblttx.html