多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 第三方登陸:微信官方登陸

第三方登陸:微信官方登陸

來源:程序員人生   發布時間:2016-08-02 09:03:49 閱讀次數:2858次

微信官方登陸

1、首先進入微信授權登陸之前的1個驗證,在微信開放平臺注冊開發者賬號,并具有1個已審核通過的移動利用,取得相應的AppID和AppSecrect,申請微信通過審核后(以下如)可開始植入工程的相干流程。


2、下載最新的SDK,鏈接以下:iOS SDK下載

下載下來的SDK以下圖:
   
1、libWeChatSDK.a  : 靜態庫,直接拖入工程中使用的;
2、README.txt : 重要內容,1些最新SDK版本的說明和安裝配置
3、WechatAuthSDK.h :授權SDK
4、WXApi.h : 登陸方法所在類
5、WXApiObject.h : 所有接口和對象數據的定義類

iOS微信登陸注意事項:

1、目前移動利用上微信登錄只提供原生的登錄方式,需要用戶安裝微信客戶端才能配合使用。
2、對iOS利用,斟酌到iOS利用商店審核指南中的相干規定,建議開發者接入微信登錄時,先檢測用戶手機是不是已安裝微信客戶端(使用sdk中isWXAppInstalled函數 ),對未安裝的用戶隱藏微信登錄按鈕,只提供其他登錄方式(比如手機號注冊登錄、游客登錄等)?!緄OS9以上的系統需注意,要想使判斷有效,需要進行白名單的適配】

iOS微信登陸大致流程:
1. 第3方發起微信授權登錄要求,微信譽戶允許授權第3方利用后,微信會拉起利用或重定向到第3方網站,并且帶上授權臨時票據code參數;
2. 通過code參數加上AppID和AppSecret等,通過API換取access_token;
3. 通過access_token進行接口調用,獲得用戶基本數據資源或幫助用戶實現基本操作。


接下來我們結合工程來集成1下:

第1步:新建工程,并添加依賴庫




第2步:設置URL Schemes(iOS9以上適配),URL Types,HTTPS設置 (以下圖)

URL Schemes和HTTPS網絡協議的適配,僅需要在Info.plist中添加以下相干源碼:
網絡適配:

 <key>NSAppTransportSecurity</key>

     <dict>

        <key>NSAllowsArbitraryLoads</key>

             <true/>

    </dict>

白名單適配:

<key>LSApplicationQueriesSchemes</key>

<array>

<string>weixin</string>

<string>wechat</string>

</array>



第3步:工程代碼
// // AppDelegate.h // WeiXinLogin // // Created by 王園園 on 16/6/26. // Copyright © 2016年 Wangyuanyuan. All rights reserved. // #import <UIKit/UIKit.h> #import "WXApi.h" @protocol WXDelegate <NSObject> - (void)loginSuccessByCode:(NSString *)code; @end @interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate> @property (strong, nonatomic) UIWindow *window; ///聲明微信代理屬性 @property (nonatomic,assign)id<WXDelegate>wxDelegate; @end

// // AppDelegate.m // WeiXinLogin // // Created by 王園園 on 16/6/26. // Copyright © 2016年 Wangyuanyuan. All rights reserved. // #import "AppDelegate.h" #define weixinLoginAppId @"" #define weixinLoginAppSecret @"" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //向微信注冊APPID [WXApi registerApp:weixinLoginAppId withDescription:@"wechat"]; return YES; } #pragma mark - 微信登陸 /*! @brief 處理微信通過URL啟動App時傳遞的數據 * * 需要在 application:openURL:sourceApplication:annotation:或application:handleOpenURL中調用。 * @param url 微信啟動第3方利用時傳遞過來的URL * @param delegate WXApiDelegate對象,用來接收微信觸發的消息。 * @return 成功返回YES,失敗返回NO。 */ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [WXApi handleOpenURL:url delegate:self]; } /*! 微信回調,不論是登錄還是分享成功與否,都是走這個方法 @brief 發送1個sendReq后,收到微信的回應 * * 收到1個來自微信的處理結果。調用1次sendReq后會收到onResp。 * 可能收到的處理結果有SendMessageToWXResp、SendAuthResp等。 * @param resp具體的回應內容,是自動釋放的 */ -(void) onResp:(BaseResp*)resp { // [[NSNotificationCenter defaultCenter]postNotificationName:@"weixinNoticationAboutLogin" object:resp]; NSLog(@"resp %d",resp.errCode); /* enum WXErrCode { WXSuccess = 0, 成功 WXErrCodeCommon = ⑴, 普通毛病類型 WXErrCodeUserCancel = ⑵, 用戶點擊取消并返回 WXErrCodeSentFail = ⑶, 發送失敗 WXErrCodeAuthDeny = ⑷, 授權失敗 WXErrCodeUnsupport = ⑸, 微信不支持 }; */ if ([resp isKindOfClass:[SendAuthResp class]]) { //授權登錄的類。 if (resp.errCode == 0) { //成功。 //這里處理回調的方法 。 通過代理吧對應的登錄消息傳送過去。 if ([_wxDelegate respondsToSelector:@selector(loginSuccessByCode:)]) { SendAuthResp *resp2 = (SendAuthResp *)resp; [_wxDelegate loginSuccessByCode:resp2.code]; } }else{ //失敗 NSLog(@"error %@",resp.errStr); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登錄失敗" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil]; [alert show]; } } } @end

// // ViewController.m // WeiXinLogin // // Created by 王園園 on 16/6/26. // Copyright © 2016年 Wangyuanyuan. All rights reserved. // #import "ViewController.h" #import "WXApi.h" #import "AppDelegate.h" #define weixinLoginAppId @"" #define weixinLoginAppSecret @"" @interface ViewController ()<WXDelegate> @property (nonatomic,strong)AppDelegate *appDelegate; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //在微信開發者平臺:利用管理中心/移動利用 獲得AppID UIButton *weiXinLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [weiXinLoginBtn setTitle:@"微信登陸" forState:UIControlStateNormal]; weiXinLoginBtn.backgroundColor = [UIColor cyanColor]; [weiXinLoginBtn addTarget:self action:@selector(weiXinLoginButtonAction) forControlEvents:UIControlEventTouchUpInside]; weiXinLoginBtn.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:weiXinLoginBtn]; } #pragma mark - 登陸按鈕的響應方法 - (void)weiXinLoginButtonAction{ //解決isWXAppInstalled的值不準確,需要iOS9白名單適配:https://github.com/ChenYilong/iOS9AdaptationTips#常見-url-scheme if ([WXApi isWXAppInstalled]) { //構造SendAuthReq結構體 SendAuthReq* req =[[SendAuthReq alloc ] init] ; req.scope = @"snsapi_userinfo"; req.openID = weixinLoginAppId; req.state = @"1245"; _appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; _appDelegate.wxDelegate = self; //第3方向微信終端發送1個SendAuthReq消息結構 [WXApi sendReq:req]; }else{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"您沒有安裝微信,是不是去下載" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]; // Add the actions. [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil]; } } #pragma mark - 此方法是,當用戶點擊允許授權以后,注冊的通知的方法 //微信 成功回調 -(void)loginSuccessByCode:(NSString *)code{ //第1步 得到code NSLog(@"code %@",code); //第2步 通過code獲得access_token NSString * grantStr = @"grant_type=authorization_code"; //通過code獲得access_token https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code NSString * tokenUrl = @"https://api.weixin.qq.com/sns/oauth2/access_token?"; NSString * tokenUrl1 = [tokenUrl stringByAppendingString:[NSString stringWithFormat:@"appid=%@&",weixinLoginAppId]]; NSString * tokenUrl2 = [tokenUrl1 stringByAppendingString:[NSString stringWithFormat:@"secret=%@&",weixinLoginAppSecret]]; NSString * tokenUrl3 = [tokenUrl2 stringByAppendingString:[NSString stringWithFormat:@"code=%@&",code]]; NSString * tokenUrlend = [tokenUrl3 stringByAppendingString:grantStr]; NSLog(@"%@",tokenUrlend); //要求:獲得access_token 和 openid NSURL *url = [NSURL URLWithString:tokenUrlend]; NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url]; __weak typeof(self)weakSelf = self; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //轉換為字典 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSString * access_token = [dic objectForKey:@"access_token"]; //NSString * expires_in = [dic objectForKey:@"expires_in"]; //NSString * refresh_token = [dic objectForKey:@"refresh_token"]; NSString * openid = [dic objectForKey:@"openid"]; [weakSelf requestUserInfoByToken:access_token andOpenid:openid]; }]; } #pragma mark - 根據accessToken和openID獲得用戶信息 -(void)requestUserInfoByToken:(NSString *)token andOpenid:(NSString *)openID{ //第3步:通過access_token得到昵稱、unionid等信息 NSString * userfulStr = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID]; NSURL * userfulUrl = [NSURL URLWithString:userfulStr]; NSURLRequest * userfulRequest = [[NSURLRequest alloc]initWithURL:userfulUrl]; [NSURLConnection sendAsynchronousRequest:userfulRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //轉換為字典 NSDictionary *userfulResultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSString * weixinOpenid = [userfulResultDic objectForKey:@"openid"]; NSString * unionidStr = [userfulResultDic objectForKey:@"unionid"];//每一個用戶所對應的每一個開放平臺下的每一個利用是同1個唯1標識 NSString * langue = [userfulResultDic objectForKey:@"language"]; NSString * nickStr = [userfulResultDic objectForKey:@"nickname"]; NSString * filePath = [userfulResultDic objectForKey:@"headimgurl"]; NSString * gender = [userfulResultDic objectForKey:@"sex"]; NSString *sex; if ([gender intValue] == 1) { sex = @"1"; }else if ([gender intValue] == 2){ sex = @"0"; } NSString * province = [userfulResultDic objectForKey:@"province"]; NSString * city = [userfulResultDic objectForKey:@"city"]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end


大功告成,這就是簡單的微信登陸!?。?!







生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美日韩一区二区三区视频在线观看 | 亚洲欧洲高清有无 | 一级aa免费视频毛片 | 欧美日韩在线播放 | 印度free xxxx性农村 | 精品视频在线看 | 亚洲综合日韩中文字幕v在线 | 国产高清一区二区 | 亚洲精品综合一区二区三区在线 | 国内精品一区视频在线播放 | 欧美日韩在线精品成人综合网 | 久久精品国产一区二区三区 | 成人精品一区二区三区校园激情 | 亚洲在线不卡 | 另类小说校园春色 | 欧美日韩性视频一区二区三区 | 久久久久国产一级毛片高清片 | 最新亚洲一区二区三区四区 | 日韩一区二区精品久久高清 | 图片区小说区校园小说 | 亚洲a视频在线观看 | 自拍偷自第11页 | 日本一区二区三区精品国产 | 可以免费看黄色的网站 | 国产精品视频一区二区三区不卡 | 国内自拍视频在线看免费观看 | 欧美第一页在线观看 | 国产精品96久久久久久久 | 亚洲最大视频网站 | 欧美人成片免费看视频不卡 | 另类小说图片 | 亚洲成在线观看 | 亚洲国产精品一区二区久 | 99精品国产高清一区二区 | 中文国产成人精品久久一 | 老师邪恶影院a啦啦啦影院 老司机av | 国产毛片a精品毛 | 男女那个视频免费 | 日本免费人成在线网站 | 一级女性全黄久久生活片免费 | 国产三级日产三级 |