1.向微信注册你的应用程序id,并记录在你的工程中
#define kWxAppID @"appid"
#define kWxAppSecret @"appSecret"
2.下载微信终端SDK文件
3.搭建开发环境
[1] 在XCode中建立你的工程
[2] 将SDK文件添加到你所建的工程中
[3]微信开放平台新增了微信模块用户统计功能,便于开发者统计微信功能模块的用户使用和活跃情况。需要添加库文件
[4] 在你的工程文件中选择Build Setting,在Search Paths中添加 libWeChatSDK.a ,WXApi.h,WXApiObject.h 三个文件所在位置:
building setting->search paths ->library search path
[5] 在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你所注册的应用程序id
[6] 在你需要使 用微信终端API的文件中import WXApi.h 头文件,并增加 WXApiDelegate 协议
4.在代码中使用开发工具包
[1] 要使你的程序启动后微信终端能响应你的程序,必须在代码中向微信终端注册你的id
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[WXApi registerApp:kWxAppID];
return YES;
}
[2] 重写AppDelegate的handleOpenURL和openURL方法
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [WXApi handleOpenURL:url delegate:self];
}
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [WXApi handleOpenURL:url delegate:self];
}
[3] 现在,你的程序要实现和微信终端交互的具体请求与回应,因此需要实现WXApiDelegate协议的两个方法
-(void) onReq:(BaseReq*)reqonReq
这是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面
-(void) onResp:(BaseResp*)resp
如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面
5.请求微信登录
- (IBAction)weChatLogin:(id)sender {
//构造SendAuthReq结构体
SendAuthReq* req =[[SendAuthReq alloc ] init ];
req.scope = @"snsapi_userinfo" ;
req.state = @"123" ;
//第三方向微信终端发送一个SendAuthReq消息结构
[WXApi sendReq:req];
}
6.在wechat回调方法中
-(void) onResp:(BaseResp*)resp{
switch (resp.errCode) {
case 0://用户同意
{
SendAuthResp *aresp = (SendAuthResp *)resp;
[self weChatCallBackWithCode:aresp.code];
}
break;
case -4://用户拒绝授权
//do ...
break;
case -2://用户取消
//do ...
break;
default:
break;
}
}
7.请求accessToken & openId
- (void)weChatCallBackWithCode:(NSString *)code{
NSString *urlString =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WECHAT_APPID,WECHAT_APPSECRET,code];
__block LoginViewController *loginVC = self;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
[loginVC getUserInfoWithAccessToken:[dic objectForKey:@"access_token"] andOpenId:[dic objectForKey:@"openid"]];
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
}];
}
8.获取用户信息
//wx获取用户信息
- (void)getUserInfoWithAccessToken:(NSString *)accessToken andOpenId:(NSString *)openId{
NSString *urlString =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",accessToken,openId];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//保存数据
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
}];
}