一、前篇:去各个官网找相应sdk和教程 Weixin官方iOS接入指南和代码实例下载 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=&lang=zh_CN qq文档 http://wiki.open.qq.com/wiki/API3.0%E6%96%87%E6%A1%A3#.E5.85.AC.E5.85.B1.E5.8F.82.E6.95.B0.E8.AF.B4.E6.98.8E http://wiki.open.qq.com/wiki/%E8%85%BE%E8%AE%AF%E5%BC%80%E6%94%BE%E5%B9%B3%E5%8F%B0%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E7%AD%BE%E5%90%8D%E5%8F%82%E6%95%B0sig%E7%9A%84%E8%AF%B4%E6%98%8E
2.分析
大概都分3步,发送请求并回调,回调中处理返回结果,根据返回结果再去请求下一步。
3.info.plist里配置对应URL types
4.代码
AppDelegate.h#import "WXApi.h"#import "WeiboSDK.h"@interface AppDelegate : UIResponder@property (strong, nonatomic) UIWindow *window;@endAppDelegate.m#pragma mark - Init PlatValue- (void)initializeSharePlat{ //注册初始化参数 //weibo [WeiboSDK enableDebugMode:YES]; [WeiboSDK registerApp:kWeiboAppKey]; //weixin [WXApi registerApp:kWeiXinAppKey]; //qq也可以不在这里初始化,直接在需要的页面中初始化,本项目暂只用于登陆,在这可以不初始化// TencentOAuth * _oauth = [[TencentOAuth alloc] initWithAppId:kTencentAppKey andDelegate:self];}#pragma mark -#pragma mark openURL- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ return [WeiboSDK handleOpenURL:url delegate:self] || [WXApi handleOpenURL:url delegate:self] || [TencentOAuth HandleOpenURL:url];}- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [WeiboSDK handleOpenURL:url delegate:self] || [WXApi handleOpenURL:url delegate:self] || [TencentOAuth HandleOpenURL:url];}#pragma mark - Weixin- (void)onReq:(BaseReq *)req{ NSLog(@"onReq: %@", req); //onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。}- (void)onResp:(BaseResp *)resp{ //如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。 NSLog(@"onResp: %@", resp); if ([resp isKindOfClass:[SendAuthResp class]]){//用户授权后回调 SendAuthResp * result = (SendAuthResp*)resp; // ERR_OK = 0(用户同意) // ERR_AUTH_DENIED = -4(用户拒绝授权) // ERR_USER_CANCEL = -2(用户取消) if (resp.errCode == 0) { NSLog(@"SendAuthResp2 %@, %@", result.code, result.state); //1.result.code 获取code //2.getToken //3.getuserinfo [self getAccess_token:result.code];//2 }else{ //提示授权失败 } }}-(void)getUserInfo:(NSString*)accessToken withOpenID:(NSString*)openID{ // https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",accessToken,openID]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *zoneUrl = [NSURL URLWithString:url]; NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil]; NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding]; dispatch_async(dispatch_get_main_queue(), ^{ if (data) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; /* { city = Haidian; country = CN; headimgurl = "http://wx.qlogo.cn/mmopen/FrdAUicrPIibcpGzxuD0kjfnvc2klwzQ62a1brlWq1sjNfWREia6W8Cf8kNCbErowsSUcGSIltXTqrhQgPEibYakpl5EokGMibMPU/0"; language = "zh_CN"; nickname = "xxx"; openid = oyAaTjsDx7pl4xxxxxxx; privilege = ( ); province = Beijing; sex = 1; unionid = oyAaTjsxxxxxxQ42O3xxxxxxs; } */ NSLog(@"getUserInfo%@", dic); } }); });}- (void)refreshAccess_token:(NSString*)refresh_token{ //refresh_token拥有较长的有效期(30天),当refresh_token失效的后,需要用户重新授权。 //https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%@&grant_type=%@&refresh_token=REFRESH_TOKEN", kWeiXinAppKey, refresh_token]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *zoneUrl = [NSURL URLWithString:url]; NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil]; NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding]; dispatch_async(dispatch_get_main_queue(), ^{ if (data) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; /* { "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" } */ NSLog(@"getAccess_token%@", dic); [self getUserInfo:dic[@"access_token"] withOpenID:dic[@"openid"]]; } }); });}-(void)getAccess_token:(NSString*)wxCode{ //获取access_token// https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", kWeiXinAppKey, kWeiXinAppSecret, wxCode]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *zoneUrl = [NSURL URLWithString:url]; NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil]; NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding]; dispatch_async(dispatch_get_main_queue(), ^{ if (data) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; /* "access_token" = "OezXcEiiBSKSxW0eoylIeDkHcwRLAAAf2y5ZOMFiIgZgqh8HV4X4b2OYXOM0m7M7k5eEc3U_muSNezPcRkEG_K9w9JFCgyBiFAqoX1r2bNOUGm6eouinITnE0_Y8KN-elHDlCXQIaCKJOvMGk23mcQ"; "expires_in" = 7200; openid = "okUT8s_748U2PTAva7AePzZ98G5c"; "refresh_token" = "OezXcEiiBSKSxW0eoylIeDkHcwRLAAAf2y5ZOMFiIgZgqh8HV4X4b2OYXOM0m7M7T4jiwpU3RMFneEFq5-0ZM39dDw7XGhWpX6Bi1d8uir12NG1ryKpa0XuxJE7hrtT8B2KKqph-Vux_R4Ty9HAMYA"; scope = "snsapi_userinfo,snsapi_base"; */ NSLog(@"getAccess_token%@", dic); [self getUserInfo:dic[@"access_token"] withOpenID:dic[@"openid"]]; } }); });}#pragma mark - Weibo- (void)didReceiveWeiboRequest:(WBBaseRequest *)request{ NSLog(@"didReceiveWeiboRequest %@", request);}- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{ NSLog(@"didReceiveWeiboResponse %@", response); NSString * wbtoken = nil; NSString * wbCurrentUserID = nil; NSString * wbRefreshToken = nil; if ([response isKindOfClass:WBAuthorizeResponse.class]) { NSString *title = NSLocalizedString(@"认证结果", nil); NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.userId: %@\nresponse.accessToken: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken], NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo]; if ((int)response.statusCode == 0) { //getuserinfo wbtoken = [(WBAuthorizeResponse *)response accessToken]; wbCurrentUserID = [(WBAuthorizeResponse *)response userID]; wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken]; [self getWbUserInfo:wbtoken withUid:wbCurrentUserID];//请求用户信息 } }}- (void)getWbUserInfo:(NSString*)access_token withUid:(NSString*)uid{ NSString *url =[NSString stringWithFormat:@"https://api.weibo.com/2/users/show.json?access_token=%@&uid=%@", access_token, uid]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *zoneUrl = [NSURL URLWithString:url]; NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil]; NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding]; dispatch_async(dispatch_get_main_queue(), ^{ if (data) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"getWbUserInfo%@", dic); } }); });}#pragma mark - Tencent- (void)tencentDidLogin{ NSLog(@"tencentDidLogin"); [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccessed object:self];}- (void)tencentDidNotLogin:(BOOL)cancelled{ //提示授权失败}- (void)tencentDidNotNetWork{ //提示网络中断}6.最后,调用发送授权请求,在所需要的viewController- (IBAction)loginQq:(id)sender{ _oauthQQ = [[TencentOAuth alloc] initWithAppId:kTencentAppKey andDelegate:self]; NSArray * permissions = @[@"get_user_info", @"get_simple_userinfo", @"add_t"]; [_oauthQQ authorize:permissions];}- (IBAction)loginWeixin:(id)sender{ SendAuthReq* req =[[SendAuthReq alloc ] init]; req.scope = @"snsapi_userinfo,snsapi_base"; req.state = @"fandian0722" ; [WXApi sendReq:req];}- (IBAction)loginWeibo:(id)sender;{ WBAuthorizeRequest *request = [WBAuthorizeRequest request]; request.redirectURI = @"http://"; request.scope = @"all"; [WeiboSDK sendRequest:request];}7.其它 还在补充中,后续接着更新完