您的位置:威尼斯官方网站 > 威尼斯正规官网 > 里面有相比详细的撸代码在此之前的开始的一段

里面有相比详细的撸代码在此之前的开始的一段

发布时间:2019-10-02 02:51编辑:威尼斯正规官网浏览(160)

    前方一些大旨的配备作者这里就不赘述了,不太知道的能够去看作者的有关微信分享的那篇小说,里面有比较详细的撸代码在此以前的先前时代筹划干活。

    1.向微信注册

    // WXAPPID:在微信开放平台注册应用时分配的AppID[WXApi registerApp:WXAPPID];
    

    2.处理open url

    - application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options { // 设置了代理为Appdelegate return [WXApi handleOpenURL:url delegate:self];}
    

    3.微信登陆

    - weixinLoginButtonClick { // 先判断是否安装微信客户端 if ([WXApi isWXAppInstalled]) { SendAuthReq *req = [[SendAuthReq alloc] init]; req.scope = @"snsapi_userinfo"; req.state = @"App"; [WXApi sendReq:req]; } else { [self showAlertWithTitle:@"提示" message:@"请先下载微信客户端"]; }}
    

    图片 1微信登入.png图片 2微信授权.png

    4.微信相关的回调

    地方的措施试行过后就能举办如下的回调方法

    - onReq:(BaseReq *)req{ JRLog(@"调用sendResp方法后,收到来自微信的请求,发送相应的结果给微信");}- onResp:(BaseResp *)resp{ if([resp isKindOfClass:[SendMessageToWXResp class]]) { NSLog(@"微信分享相关的回调"); }else if ([resp isKindOfClass:[SendAuthResp class]]) { SendAuthResp *temp = (SendAuthResp*)resp; if (temp.errCode == WXSuccess) { // 微信授权成功 [self getAuthorityWithCode:temp.code]; } }}#pragma mark --/**WXAPPID:在微信平台注册时的AppIDAPPSECRET:在微信平台注册应用时获取*/- getAuthorityWithCode:(NSString *)code { NSString *URL = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WXAPPID,APPSECRET,code]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *baseURL = [NSURL URLWithString:URL]; NSString *rightURL = [NSString stringWithContentsOfURL:baseURL encoding:NSUTF8StringEncoding error:nil]; NSData *data = [rightURL dataUsingEncoding:NSUTF8StringEncoding]; if  { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSString *access_token = [dic objectForSafeKey:@"access_token"]; NSString *openid = [dic objectForSafeKey:@"openid"]; [self wechatLoginByRequestForUserInfoWithAccess:access_token openID:openid]; } });}/** 获取微信用户信息*/- wechatLoginByRequestForUserInfoWithAccess:(NSString *)access_token openID:(NSString *)openid { // 获取用户信息 NSString *URL = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",access_token,openid]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:URL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]; if (dict != nil) { NSLog(@"dict里面即是获取到得微信的相关信息; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];}
    
    - qqLoginButtonClick { if (![TencentOAuth iphoneQQInstalled]) { [self showAlertWithTitle:@"提示" message:@"请先下载QQ客户端"]; }else { self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQAPPID andDelegate:self]; NSArray *permissions= [NSArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo",@"add_t",nil]; [self.tencentOAuth authorize:permissions inSafari:NO]; }}
    

    图片 3QQ登入授权.png

    #pragma mark -- TencentLoginDelegate/** * 登录成功后的回调 */- tencentDidLogin { NSLog(@"QQ登录成功 - %@",self.tencentOAuth.openId);}/** * 登录失败后的回调 * param cancelled 代表用户是否主动退出登录 */- tencentDidNotLogin:cancelled { if (!cancelled) { [self showAlertWithTitle:@"提示" message:@"QQ登录失败"]; }}/** * 登录时网络有问题的回调 */- tencentDidNotNetWork { [self showAlertWithTitle:@"提示" message:@"登录时网络出现错误"];}/** * 退出登录的回调 */- tencentDidLogout { NSLog;}
    

    1.博客园和讯按键点击

    - weiboLoginButtonClick { if (![WeiboSDK isWeiboAppInstalled]) { [self showAlertWithTitle:@"提示" message:@"请先安装新浪微博客户端"]; }else { // 授权页面(授权页面是微博提供的 我们只需要用网页来加载它即可) JROAuthorViewController *oauthVc = [[JROAuthorViewController alloc] init]; [self.navigationController pushViewController:oauthVc animated:YES]; }}
    

    2.授权页面的贯彻

    图片 4搜狐今日头条1.png

    - viewDidLoad{ [super viewDidLoad]; UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, [UIDevice screenWidth], [UIDevice screenHeight])]; webView.delegate = self; [self.view addSubview:webView]; self.navigationTitleLabel.text = @"新浪微博授权"; // 2.用webView加载授权页面 NSString *urlStr = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",WBAPPID,@"http://sns.whalecloud.com/sina2/callback"]; NSURL *url = [NSURL URLWithString:urlStr]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request];}#pragma mark - UIWebViewDelegate- webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *url = request.URL.absoluteString; // 1.是否为回调地址 NSRange range = [url rangeOfString:@"code="]; if (range.length != 0) { // 2.截取code=后面的参数值 NSInteger fromIndex = range.location   range.length; NSString *code = [url substringFromIndex:fromIndex]; // 3.利用code换取一个accessToken [self accessTokenWithCode:code]; } return YES;}
    

    图片 5搜狐和讯2.png

    /** * 利用授权成功后的request token换取一个accessToken */- accessTokenWithCode:(NSString *)code { // 1.请求管理者 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // 2.拼接请求参数 NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"client_id"] = WBAPPID; params[@"client_secret"] = WBSECRT; params[@"grant_type"] = @"authorization_code"; params[@"redirect_uri"] = @"http://sns.whalecloud.com/sina2/callback"; params[@"code"] = code; // 3.发送请求 [manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]; if (dict != nil) { NSLog(@"获取access_token成功"); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];}
    

    图片 6天涯论坛知乎3.png

    总结

    如上只是三方登陆的大概利用,具体的行使依然各自平台的文书档案万分完美。

    本文由威尼斯官方网站发布于威尼斯正规官网,转载请注明出处:里面有相比详细的撸代码在此之前的开始的一段

    关键词:

上一篇:威尼斯官方网站:即为str指向的地址

下一篇:没有了