微信登录 分享 原生文档接入
1,终端导入 pod 'WechatOpenSDK'
2,https://open.weixin.qq.com 获取应用的
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
<string>wechat</string>
</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) {
}];
}