QQ登錄與分享 注意:修改必要的工程配置屬性。在工程配置中的“Build Settings”一欄中找到“Linking”配置區(qū)雌澄,給“Other Linker Flags”配置項(xiàng)添加屬性值“-fobjc-arc”贱案。
1、首先導(dǎo)入QQ的SDK
2、導(dǎo)入需要的framework 添加SDK依賴的系統(tǒng)庫(kù)文件腰懂。分別是”Security.framework”, “l(fā)ibiconv.dylib”刁俭,“SystemConfiguration.framework”,“CoreGraphics.Framework”散劫、“l(fā)ibsqlite3.dylib”稚机、“CoreTelephony.framework”、“l(fā)ibstdc++.dylib”获搏、“l(fā)ibz.dylib”赖条。
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纬乍、向終端程序注冊(cè)第三方應(yīng)用,并在第三方應(yīng)用實(shí)現(xiàn)從qq返回在AppDelegate.m中引入#import頭文件裸卫,然后寫(xiě)入如下:
- (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協(xié)議
TencentOAuth *_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"xxxxxxxxxxxx" andDelegate:self];如果不寫(xiě)TencentOAuth *_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"xxxxxxxxxxxx" andDelegate:self];
//會(huì)提示(EQQAPIAPPNOTREGISTED )App未注冊(cè)的錯(cuò)誤
//分享給QQ好友文字
- (void)sendTextMessageToQQ
{
QQApiTextObject *txtObj = [QQApiTextObject objectWithText:@"QQ互聯(lián)測(cè)試"];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
//將內(nèi)容分享到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:@"天公作美倫敦奧運(yùn)圣火點(diǎn)燃成功 火炬?zhèn)鬟f開(kāi)啟" description:@"騰訊體育訊 當(dāng)?shù)貢r(shí)間5月10日中午,陽(yáng)光和全世界的目光聚焦于希臘最高女祭司手中的火炬上墓贿,5秒鐘內(nèi)世界屏住呼吸茧泪◎寻保火焰驟然升騰的瞬間,古老的號(hào)角聲隨之從赫拉神廟傳出——第30屆倫敦夏季奧運(yùn)會(huì)圣火在古奧林匹亞遺址點(diǎn)燃调炬。取火儀式前语盈,國(guó)際奧委會(huì)主席羅格、希臘奧委會(huì)主席卡普拉洛斯和倫敦奧組委主席塞巴斯蒂安-科互贈(zèng)禮物缰泡,男祭司繼北京奧運(yùn)會(huì)后刀荒,再度出現(xiàn)在采火儀式中。" previewImageData:data];
// [img setCflag:kQQAPICtrlFlagQQShare];
SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];
QQApiSendResultCode sent = [QQApiInterface sendReq:req];
[self handleSendResult:sent];
}
- (void)sendWebMessageToQQZone
{
// 發(fā)送網(wǎng)絡(luò)圖片
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:@"天公作美倫敦奧運(yùn)圣火點(diǎn)燃成功 火炬?zhèn)鬟f開(kāi)啟" description:@"騰訊體育訊 當(dāng)?shù)貢r(shí)間5月10日中午棘钞,陽(yáng)光和全世界的目光聚焦于希臘最高女祭司手中的火炬上缠借,5秒鐘內(nèi)世界屏住呼吸∫瞬拢火焰驟然升騰的瞬間泼返,古老的號(hào)角聲隨之從赫拉神廟傳出——第30屆倫敦夏季奧運(yùn)會(huì)圣火在古奧林匹亞遺址點(diǎn)燃。取火儀式前姨拥,國(guó)際奧委會(huì)主席羅格绅喉、希臘奧委會(huì)主席卡普拉洛斯和倫敦奧組委主席塞巴斯蒂安-科互贈(zèng)禮物,男祭司繼北京奧運(yùn)會(huì)后叫乌,再度出現(xiàn)在采火儀式中柴罐。" 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未注冊(cè)" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIMESSAGECONTENTINVALID:
case EQQAPIMESSAGECONTENTNULL:
case EQQAPIMESSAGETYPEINVALID:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"發(fā)送參數(shù)錯(cuò)誤" 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:@"發(fā)送失敗" 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協(xié)議 添加屬性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"]
//授權(quán)登錄
[_tencentOAuth authorize:permissions inSafari:NO];
} 登錄成功之后調(diào)用代理方法(協(xié)議必須實(shí)現(xiàn)的三個(gè)方法) //qq登錄成功之后 在協(xié)議的方法中返回信息
- (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]; //調(diào)用此方法會(huì)在- (void)getUserInfoResponse:(APIResponse *)response中返回?cái)?shù)據(jù)
}
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個(gè)人信息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無(wú)網(wǎng)絡(luò)連接憨奸,請(qǐng)?jiān)O(shè)置網(wǎng)絡(luò)");
}