第一步:在微信开放平台注册应用,获取应用的appID以及AppSecret。
第二步:在微信开放平台下载sdk,并导入进项目。(详细步骤看官方文档https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=&lang=zh_CN);
第三步:配置 URL Scheme 在Xcode中,选择你的工程设置项,选中TARGETS
一栏,在info
标签栏的URL type
添加URL scheme
为你所注册的应用程序id.
第四步:开始编写代码
//在appdelegate.h中引用微信头文件,并遵循代理
#import
#import "WXApi.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//注册sdk- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:@"wx43b458fb2f33fc68"];
return YES;
}
//重写openURL方法
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
return [WXApi handleOpenURL:url delegate:self];
}
//在登录页方法中调用接口
-(void)WXbuttonClick{
SendAuthReq *req = [[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo";
req.state = @"App";
[WXApi sendReq:req];
}
//在Appdelegate中实现微信的代理,并获取微信返回的code,并在获取code之后,采用通知的原理,调用相对应的接口实现下面的方法
-(void) onResp:(BaseResp*)resp
{
if ([resp isKindOfClass:[SendAuthResp class]]) //判断是否为授权请求,否则与微信支付等功能发生冲突
{
SendAuthResp *aresp = (SendAuthResp *)resp;
if (aresp.errCode== 0)
{
NSLog(@"code %@",aresp.code);
[[NSNotificationCenter defaultCenter] postNotificationName:@"wechatDidLoginNotification" object:self userInfo:@{@"code":aresp.code}];
}
}
}
//在登录页注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wechatDidLoginNotification:) name:@"wechatDidLoginNotification" object:nil];
//根据code获取accesstoken和openid
-(void)wechatDidLoginNotification:(NSNotification*)notification{
NSString *code= notification.userInfo[@"code"];
[self getWechatAccessTokenWithCode:code];
}
- (void)getWechatAccessTokenWithCode:(NSString *)code
{
NSString *url =[NSString stringWithFormat:
@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",
WechatAppKey,WechatSecrectKey,code];
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(@"%@",dic);
NSString *accessToken = dic[@"access_token"];
NSString *openId = dic[@"openid"];
[self getWechatUserInfoWithAccessToken:accessToken openId:openId];
}
});
});
}
//根据accesstoken和openid获取用户信息
- (void)getWechatUserInfoWithAccessToken:(NSString *)accessToken openId:(NSString *)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];
NSLog(@"%@",dic);
NSString *openId = [dic objectForKey:@"openid"];
NSString *memNickName = [dic objectForKey:@"nickname"];
NSString *memSex = [dic objectForKey:@"sex"];
// [self loginWithOpenId:openId memNickName:memNickName memSex:memSex];
NSLog(@"--%@--%@--%@",openId,memNickName,memSex);
}
});
});
}