First, download the SDK from wechat Developer Platform at:
Add LSApplicationQueriesSchemes array in plist file, add two strings for calling wechat
Import dependency library SystemConfiguration.framework, libz.dylib, libsqlite3.0.dylib, libc++.dylib, Security.framework, CoreTelephony.framework, CFNetwork.framework
Add white list
Select Build Setting in your project file, and add "- ObjC and - all_load" in "Other Linker Flags"
Follow protocol and add header file in Appdelegate.h
#import "AppDelegate.h" #import "WXApi.h" @interface AppDelegate ()<WXApiDelegate> @end
Register wechat Wx? appId in the didFinishLaunchingWithOptions method in Appdelegate.m is the appId on your wechat Developer Platform
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Wechat registration [WXApi registerApp:WX_AppId]; return YES; }
Add proxy method in Appdelegate.m
// This method is used to return the third-party App from wechat - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*,id> *)options NS_AVAILABLE_IOS(9_0){ [WXApi handleOpenURL:url delegate:self]; return YES; }
Callback method in Appdelegate.m
[self.window showmbhudallertwithmessage: @ "wechat authorization failed" hide:1.0]; it is a prompt box
- (void)onResp:(BaseResp *)resp{ // ===============Wechat login authorization callback obtained============ if ([resp isMemberOfClass:[SendAuthResp class]]) { NSLog(@"******************Wechat login authorization obtained******************"); SendAuthResp *aresp = (SendAuthResp *)resp; if (aresp.errCode != 0 ) { dispatch_async(dispatch_get_main_queue(), ^{ [self.window showMBHUDAlertWithMessage:@"Wechat authorization failed" hide:1.0]; }); return; } NSString *code = aresp.code; [self getWeiXinOpenId:code]; } }
Get token and openid in AppDelegate.m
//access_token, openid, unionid through code - (void)getWeiXinOpenId:(NSString *)code{ NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WX_AppId,WX_AppSerect,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 *data1 = [zoneStr dataUsingEncoding:NSUTF8StringEncoding]; if (!data1) { [self.window showMBHUDAlertWithMessage:@"Wechat authorization failed" hide:1.0]; return ; } // Authorization succeeded, get token and openID dictionary NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil]; NSLog(@"token,openID Dictionaries===%@",dic); self.access_token = dic[@"access_token"]; self.openid= dic[@"openid"]; // Get wechat user information [self getUserInfo]; }); }
Appdelete. M user obtains wechat user information
-(void)getUserInfo { NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",self.access_token,self.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(), ^{ // Failed to get user information if (!data) { [self.window showMBHUDAlertWithMessage:@"Wechat authorization failed" hide:1.0]; return ; } // Get user information dictionary NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"User information dictionary:===%@",dic); }); }); }
Write in wechat login event
if ([WXApi isWXAppInstalled]) { //Construct SendAuthReq structure SendAuthReq *req = [[SendAuthReq alloc] init]; req.scope = @"snsapi_userinfo"; req.state = @"App"; //Send a SendAuthReq message structure to the wechat terminal [WXApi sendReq:req]; }else{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"reminder" message:@"Please install wechat client first" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"determine" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil]; }
End!!!