iOS第三方登錄及分享入門指南 —— 微信(二)

導(dǎo)語:

完成原生QQ的SDK集成后,接下來就是微信的了,這兩家的SDK我懷疑是同個人寫的,如此相像,雖不難,但還是有一些坑需要注意。

微信 SDK官方文檔

實現(xiàn)微信登錄

一、準備工作

  1. 微信·開放平臺,注冊成為開發(fā)者。
  2. 創(chuàng)建應(yīng)用,申請你的AppIDAppSecret。

二、集成SDK

下載好完整包,如圖導(dǎo)入工程需要的文件。

微信SDK.png

三、配置工程

1. 添加SDK依賴的系統(tǒng)庫文件

SystemConfiguration.framework
Security.framework
CFNetwork.framework
CoreTelephony.framework
libsqlite3.0.tbd
libstdc++.tbd
libz.tbd
libc++.tbd

2. 修改必要的工程配置屬性

在工程配置中的Build Settings一欄中找到Linking配置區(qū),給Other Linker Flags配置項添加屬性值-Objc、-all_load

配置工程.png
3. 修改Info.plist文件

在XCode中,選中TARGETS一欄,在Info標簽欄中找到URL Types,添加一條新的URL scheme。(必須填寫

Identifier: wexin
URL Schemes: wx + "appid"

想要實現(xiàn)應(yīng)用間跳轉(zhuǎn),而不是打開一個登陸網(wǎng)頁,在Info.plist中添加LSApplicationQueriesSchemes

<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
</array>

四、代碼實現(xiàn)

1.AppDelegate

在AppDelegate中添加頭文件,并重寫AppDelegate的handleOpenURLopenURL方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [WXApi registerApp:@"appid"];
    return YES;
}
  • iOS9之前,分別重寫handleOpenURL && openURL方法

      //handleOpenURL(ios10已棄用)
      NS_DEPRECATED_IOS(2_0, 9_0, "Please use application:openURL:options:")
      - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
          return [WXApi handleOpenURL:url delegate:self];
    
      }
         
      //openURL(iOS10已棄用)
      NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:")
      - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
          return [WXApi handleOpenURL:url delegate:self];
      }
    
  • iOS9之后,handleOpenURL && openURL 合成為同一個方法

      //handleOpenURL && openURL
      - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
          return [WXApi handleOpenURL:url delegate:self];
      }
    
2.實現(xiàn)代理方法
- (void)onReq:(BaseReq *)req{
    NSLog(@"wx onReq");
}
  
- (void)onResp:(BaseResp *)resp{
    NSLog(@"wx onResp");
    //登錄 分享 都走同一個回調(diào)
    if ([resp isKindOfClass:[SendAuthResp class]]) {
         SendAuthResp* authResp = (SendAuthResp *)resp;
        [self loginResponse:authResp];
    }
}
  
- (void)loginResponse:(SendAuthResp*)response{
     switch (response.errCode) {
         case WXSuccess:{
            [self getUserToken:response.code];
       }
            break;
         case WXErrCodeCommon:
            break;
         case WXErrCodeUserCancel:
            break;
         case WXErrCodeSentFail:
            break;
         case WXErrCodeAuthDeny:
            break;
         case WXErrCodeUnsupport:
            break;
    }
}
3.請求code,通過login點擊事件
-(void)sendAuthRequest{ 
    //構(gòu)造SendAuthReq結(jié)構(gòu)體 
    SendAuthReq* req =[[[SendAuthReq alloc ] init ] autorelease ];
    req.scope = @"snsapi_userinfo" ;
    req.state = @"123" ;
    //第三方向微信終端發(fā)送一個SendAuthReq消息結(jié)構(gòu)
    [WXApi sendReq:req]; 
}
4.通過code獲取access_token
- (void)getUserToken:(NSString*)code{
     NSString* url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WXAppID,WXSecret,code];
     //獲取第一步的code后,請求以下鏈接獲取access_token:
     
        { 
            "access_token":"ACCESS_TOKEN", 
            "expires_in":7200, 
            "refresh_token":"REFRESH_TOKEN",
            "openid":"OPENID", 
            "scope":"SCOPE",
            "unionid":"o6_bmasdasdsad6_2sgVt7hMZOPfL"
        }
     
     //將獲取到的userToken && openID 繼續(xù)網(wǎng)絡(luò)請求獲取userInfo
     [self getUserInfo:response[@"access_token"] openID:response[@"openid"]];
 }
參數(shù) 說明
access_token 接口調(diào)用憑證(有效期30天)
expires_in access_token接口調(diào)用憑證超時時間,單位(秒)
refresh_token 用戶刷新access_token
openid 授權(quán)用戶唯一標識
scope 用戶授權(quán)的作用域,使用逗號(,)分隔
unionid 當且僅當該移動應(yīng)用已獲得該用戶的userinfo授權(quán)時,才會出現(xiàn)該字段
5.刷新access_token有效期

獲取第一步的code后,請求以下鏈接進行refresh_token:

https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
6.根據(jù)獲取到的userToken && openID 繼續(xù)網(wǎng)絡(luò)請求獲取userInfo
- (void)getUserInfo:(NSString*)token openID:(NSString*)openID{
    NSString* url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID];
    
    //網(wǎng)絡(luò)請求 返回response包含UserInfo
 }

實現(xiàn)微信分享

步驟同上一、二、三

微信 SDK官方文檔

代碼實現(xiàn)

1.AppDelegate

設(shè)置分享支持類型

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [WXApi registerApp:@"appid"];
    
    UInt64 typeFlag = MMAPP_SUPPORT_TEXT | MMAPP_SUPPORT_PICTURE |
                      MMAPP_SUPPORT_LOCATION | MMAPP_SUPPORT_VIDEO | 
                      MMAPP_SUPPORT_AUDIO | MMAPP_SUPPORT_WEBPAGE |
                      MMAPP_SUPPORT_DOC | MMAPP_SUPPORT_DOCX | 
                      MMAPP_SUPPORT_PPT | MMAPP_SUPPORT_PPTX | 
                      MMAPP_SUPPORT_XLS | MMAPP_SUPPORT_XLSX |
                      MMAPP_SUPPORT_PDF;
    
    [WXApi registerAppSupportContentFlag:typeFlag];
    
    return YES;
}
2.實現(xiàn)代理方法
- (void)onReq:(BaseReq *)req{
    NSLog(@"wx onReq");
}
  
- (void)onResp:(BaseResp *)resp{
    NSLog(@"wx onResp");
    //登錄 分享 都走同一個回調(diào)
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
         SendMessageToWXResp* messageResp = (SendMessageToWXResp *)resp;
        [self shareResponse:messageResp];
    }
}
  
- (void)shareResponse:(SendAuthResp*)response{
     switch (response.errCode) {
         case WXSuccess:{
            //分享成功
       }
            break;
         case WXErrCodeCommon:
            break;
         case WXErrCodeUserCancel:
            break;
         case WXErrCodeSentFail:
            break;
         case WXErrCodeAuthDeny:
            break;
         case WXErrCodeUnsupport:
            break;
    }
}
3.分享示例

WXSceneSession 發(fā)送到聊天界面
WXSceneTimeline 發(fā)送到朋友圈
WXSceneFavorite 添加到微信收藏

  • 文字

      // 第三方程序發(fā)送消息至微信終端程序的消息結(jié)構(gòu)體
      SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
      req.text = @"分享的內(nèi)容";
      // 發(fā)送消息的類型,包括文本消息和多媒體消息兩種,兩者只能選擇其一,不能同時發(fā)送文本和多媒體消息
      req.bText = YES;
      req.scene = WXSceneTimeline;
      [WXApi sendReq:req];
    
  • 圖片

      WXMediaMessage *message = [WXMediaMessage message];
      [message setThumbImage:[UIImage imageNamed:@"圖片.png"]];
    
      WXImageObject *imageObject = [WXImageObject object];
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res1" ofType:@"jpg"];
      // 圖片真實數(shù)據(jù)內(nèi)容
      imageObject.imageData = [NSData dataWithContentsOfFile:filePath];
      message.mediaObject = imageObject;
      
      SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
      req.bText = NO;
      req.message = message;
      req.scene = WXSceneTimeline;// 分享到朋友圈
      [WXApi sendReq:req];
    
  • 音樂

      WXMediaMessage *message = [WXMediaMessage message];
      message.title = @"音樂標題";
      message.description = @"音樂描述";
      [message setThumbImage:[UIImage imageNamed:@"縮略圖.jpg"]];
      
      WXMusicObject *musicObject = [WXMusicObject object];
      musicObject.musicUrl = @"音樂url";
      musicObject.musicLowBandDataUrl = @"音樂lowband數(shù)據(jù)url地址";
      musicObject.musicDataUrl = @"音樂數(shù)據(jù)url";
      musicObject.musicLowBandDataUrl = @"音樂lowband數(shù)據(jù)url地址";
      message.mediaObject = musicObject;
      
      SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
      req.bText = NO;
      req.message = message;
      req.scene = WXSceneTimeline;
      [WXApi sendReq:req];
    
  • 網(wǎng)頁

      WXMediaMessage *message = [WXMediaMessage message];
      message.title = @"標題";
      message.description = @"描述";
      [message setThumbImage:[UIImage imageNamed:@"res2.png"]];
      
      WXWebpageObject *webpageObject = [WXWebpageObject object];
      webpageObject.webpageUrl = @"https://opne.weixin.qq.com";
      message.mediaObject = webpageObject;
      
      SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
      req.bText = NO;
      req.message = message;
      req.scene = WXSceneTimeline;
      [WXApi sendReq:req];
    

結(jié)束語

  • 如果同時集成QQ SDK和微信 SDK,需要注意兩者的回調(diào)方法會沖突。解決辦法可以分別創(chuàng)建對應(yīng)的管理類來管理代理方法。

     -(void)onReq:(BaseReq *)req;
     -(void)onResp:(BaseResp *)resp;
    
  • 微信登錄和分享的回調(diào)方法為同一個

  • 純文字分享可以跳轉(zhuǎn),其他類型無法跳轉(zhuǎn)時,請注意縮略圖(thumbImage)大小是否超過32kb (重要

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容