国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS微信登錄步驟

2019-11-09 18:44:19
字體:
來源:轉載
供稿:網友

1.在進行微信OAuth2.0授權登錄接入之前,在微信開放平臺注冊開發者帳號,并擁有一個已審核通過的移動應用,并獲得相應的AppID和AppSecret,申請微信登錄且通過審核后,可開始接入流程。 地址: 點擊打開鏈接

2. 下載最新的SDK   地址: 點擊打開鏈接

SDK內容如下:

結構解析:

     從上到下依次說明:

     1. 靜態庫,直接拖入工程。

     2. ready.text自己看

    3. 授權SDK。

    4. 登錄方法所在類。

    5.  一些常用的對象類。

iOS微信登錄注意事項:

[objc] view plain copy 在CODE上查看代碼片1、目前移動應用上微信登錄只提供原生的登錄方式,需要用戶安裝微信客戶端才能配合使用。  2、對于Android應用,建議總是顯示微信登錄按鈕,當用戶手機沒有安裝微信客戶端時,請引導用戶下載安裝微信客戶端。  3、對于iOS應用,考慮到iOS應用商店審核指南中的相關規定,建議開發者接入微信登錄時,先檢測用戶手機是否已安裝微信客戶端(使用sdk中isWXAppInstalled函數 ),對未安裝的用戶隱藏微信登錄按鈕,只提供其他登錄方式(比如手機號注冊登錄、游客登錄等)。  

iOS微信登錄大致流程:

[objc] view%20plain copy 1. 第三方發起微信授權登錄請求,微信用戶允許授權第三方應用后,微信會拉起應用或重定向到第三方網站,并且帶上授權臨時票據code參數;  2. 通過code參數加上AppID和AppSecret等,通過API換取access_token;  3. 通過access_token進行接口調用,獲取用戶基本數據資源或幫助用戶實現基本操作。  示意圖:

接下來就進入正題:

     1.配置工程

 1. 新建一個工程。         2. 把下載下來的sdk中的.h文件與靜態庫全部拖入工程。 

         3.  加入依賴庫         4.  URL - Types  (加入 appid)        target  -  Info - URL Types                 5. 白名單當程序出現此錯誤[objc] view plain copy 在CODE上查看代碼片-canOpenURL: failed for URL: "weixin://app/wx5efead4057f98bc0/" - error: "This app is not allowed to query for scheme weixin"  就說明沒有針對iOS9%20增加白名單。在info.plist文件中加入%20LSapplicationQueriesSchemes App Transport Security 這個是讓程序還是用http進行請求。LSApplicationQueriesSchemes 這個是增加微信的白名單。         6.  現在編譯應該是沒有問題了。2. 終于到令人興奮的代碼部分了。 直接上代碼。[objc] view plain copy 在CODE上查看代碼片//  //  AppDelegate.m  //  weixinLoginDemo  //  //  Created by 張國榮 on 16/6/20.  //  Copyright ? 2016年 BateOrganization. All rights reserved.  //    #import "AppDelegate.h"  #import "WXApi.h"    //微信開發者ID  #define URL_APPID @"app id"    @end        @implementation AppDelegate      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {            //向微信注冊應用。      [WXApi registerApp:URL_APPID withDescr派生到我的代碼片//  //  ViewController.m  //  weixinLoginDemo  //  //  Created by 張國榮 on 16/6/20.  //  Copyright ? 2016年 BateOrganization. All rights reserved.  //    #import "ViewController.h"  #import "WXApi.h"  #import "AppDelegate.h"  //微信開發者ID  #define URL_APPID @"appid"  #define URL_SECRET @"app secret"  #import "AFNetworking.h"  @interface ViewController ()<WXDelegate>  {      AppDelegate *appdelegate;  }  @end    @implementation ViewController    - (void)viewDidLoad {      [super viewDidLoad];      // Do any additional setup after loading the view, typically from a nib.  }  #PRagma mark 微信登錄  - (IBAction)weixinLoginAction:(id)sender {            if ([WXApi isWXAppInstalled]) {          SendAuthReq *req = [[SendAuthReq alloc]init];          req.scope = @"snsapi_userinfo";          req.openID = URL_APPID;          req.state = @"1245";          appdelegate = [UIApplication sharedApplication].delegate;          appdelegate.wxDelegate = self;            [WXApi sendReq:req];      }else{          //把微信登錄的按鈕隱藏掉。      }  }  #pragma mark 微信登錄回調。  -(void)loginSuccessByCode:(NSString *)code{      NSLog(@"code %@",code);      __weak typeof(*&self) weakSelf = self;            AFHTTPsessionManager *manager = [AFHTTPSessionManager manager];      manager.requestSerializer = [AFJSONRequestSerializer serializer];//請求      manager.responseSerializer = [AFHTTPResponseSerializer serializer];//響應      manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json",@"text/plain", nil nil];      //通過 appid  secret 認證code . 來發送獲取 access_token的請求      [manager GET:[NSString stringWithFormat:@"https://api.weixin.QQ.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",URL_APPID,URL_SECRET,code] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {               } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responSEObject) {  //獲得access_token,然后根據access_token獲取用戶信息請求。            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];          NSLog(@"dic %@",dic);                    /*          access_token   接口調用憑證          expires_in access_token接口調用憑證超時時間,單位(秒)          refresh_token  用戶刷新access_token          openid 授權用戶唯一標識          scope  用戶授權的作用域,使用逗號(,)分隔          unionid     當且僅當該移動應用已獲得該用戶的userinfo授權時,才會出現該字段          */          NSString* accessToken=[dic valueForKey:@"access_token"];          NSString* openID=[dic valueForKey:@"openid"];          [weakSelf requestUserInfoByToken:accessToken andOpenid:openID];      } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {       NSLog(@"error %@",error.localizedFailureReason);      }];        }    -(void)requestUserInfoByToken:(NSString *)token andOpenid:(NSString *)openID{            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];      manager.requestSerializer = [AFJSONRequestSerializer serializer];      manager.responseSerializer = [AFHTTPResponseSerializer serializer];      [manager GET:[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {          NSDictionary *dic = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];          NSLog(@"dic  ==== %@",dic);                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {          NSLog(@"error %ld",(long)error.code);      }];  }    - (void)didReceiveMemoryWarning {      [super didReceiveMemoryWarning];      // Dispose of any resources that can be recreated.  }    @end  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梁河县| 荣昌县| 栖霞市| 晋州市| 白玉县| 双柏县| 新绛县| 南丹县| 赞皇县| 南和县| 石楼县| 射阳县| 龙泉市| 宝应县| 克拉玛依市| 嘉义县| 正安县| 修水县| 麟游县| 安福县| 商水县| 新巴尔虎右旗| 越西县| 沅陵县| 正蓝旗| 佛学| 安阳市| 伊川县| 应城市| 南漳县| 阿拉善右旗| 赤峰市| 新兴县| 宣城市| 招远市| 嘉禾县| 刚察县| 准格尔旗| 张北县| 玛纳斯县| 房产|