1,終端導入 pod 'WechatOpenSDK'web
2,https://open.weixin.qq.com 獲取應用的api
<key>LSApplicationQueriesSchemes</key>url
<array>code
<string>weixin</string>orm
<string>wechat</string>server
</array>
4,
#import "WXApi.h"
<WXApiDelegate>
在didFinishLaunchingWithOptions 註冊微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[WXApi registerApp:Wx_Appid];
return YES;
}
9.0系統後
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
[WXApi handleOpenURL:url delegate:self];
return YES;
}
- (void)onResp:(BaseResp *)resp{
if([resp isKindOfClass:[SendAuthResp class]]){
SendAuthResp *rep = (SendAuthResp *)resp;
if(rep.errCode == 0){
[[NSNotificationCenter defaultCenter]postNotificationName:@"wxchaen" object:@{@"code":rep.code}];
}
}else if([resp isKindOfClass:[SendMessageToWXResp class]]){
if (resp.errCode == 0) {
NSLog(@"分享成功");
}else{
NSLog(@"分享失敗");
}
}
}
在第三方登陸界面
if([WXApi isWXAppInstalled]){
SendAuthReq *reques = [[SendAuthReq alloc]init];
reques.state = @"wx.oauth2_authorization_state";
reques.scope = @"snsapi_userinfo";
[WXApi sendReq:reques];
}
分享
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"分享標題";
message.description = @"分享描述";
[message setThumbImage:[UIImage imageNamed:@"wx"]];
WXWebpageObject *webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = @「」;
message.mediaObject = webpageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
// 微信好友
req.scene = WXSceneTimeline;//朋友圈
req.scene =WXSceneSession;//好友
[WXApi sendReq:req];
在第三方登陸界面
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginsecess:) name:@"wxchaen" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(payresult:) name:@"payresult" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(payfail:) name:@"payfail" object:nil];
}
-(void)loginsecess:(NSNotification *)notification{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableDictionary *param = [NSMutableDictionary dictionary];
[param setObject:Wx_Appid forKey:@"appid"];
[param setObject:Wx_Srecret forKey:@"secret"];
[param setObject:notification.object[@"code"] forKey:@"code"];
[param setObject:@"authorization_code" forKey:@"grant_type"];
[manager GET:@"https://api.weixin.qq.com/sns/oauth2/access_token" parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dict == %@",dict);
[self getuserinfo:dict];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
-(void)getuserinfo:(NSDictionary *)dic{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableDictionary *param = [NSMutableDictionary dictionary];
[param setObject:[NSString stringWithFormat:@"%@",dic[@"openid"]] forKey:@"openid"];
[param setObject:[NSString stringWithFormat:@"%@",dic[@"access_token"]] forKey:@"access_token"];
[manager GET:@"https://api.weixin.qq.com/sns/userinfo" parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dict == %@",dict);
[self PostUrl:dict];//吧獲取的信息傳給後臺
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}