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

2017-05-09  本文已影响1139人  面向copy编程的白丁

导语:

完成原生QQ的SDK集成后,接下来就是微信的了,这两家的SDK我怀疑是同个人写的,如此相像,虽不难,但还是有一些坑需要注意。

微信 SDK官方文档

实现微信登录

一、准备工作

  1. 微信·开放平台,注册成为开发者。
  2. 创建应用,申请你的AppIDAppSecret

二、集成SDK

下载好完整包,如图导入工程需要的文件。

微信SDK.png

三、配置工程

1. 添加SDK依赖的系统库文件

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

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

在工程配置中的Build Settings一栏中找到Linking配置区,给Other Linker Flags配置项添加属性值-Objc-all_load

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

在XCode中,选中TARGETS一栏,在Info标签栏中找到URL Types,添加一条新的URL scheme。(必须填写

Identifier: wexin
URL Schemes: wx + "appid"

想要实现应用间跳转,而不是打开一个登陆网页,在Info.plist中添加LSApplicationQueriesSchemes

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

四、代码实现

1.AppDelegate

在AppDelegate中添加头文件,并重写AppDelegate的handleOpenURLopenURL方法

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

    [WXApi registerApp:@"appid"];
    return YES;
}
2.实现代理方法
- (void)onReq:(BaseReq *)req{
    NSLog(@"wx onReq");
}
  
- (void)onResp:(BaseResp *)resp{
    NSLog(@"wx onResp");
    //登录 分享 都走同一个回调
    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{ 
    //构造SendAuthReq结构体 
    SendAuthReq* req =[[[SendAuthReq alloc ] init ] autorelease ];
    req.scope = @"snsapi_userinfo" ;
    req.state = @"123" ;
    //第三方向微信终端发送一个SendAuthReq消息结构
    [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 继续网络请求获取userInfo
     [self getUserInfo:response[@"access_token"] openID:response[@"openid"]];
 }
参数 说明
access_token 接口调用凭证(有效期30天)
expires_in access_token接口调用凭证超时时间,单位(秒)
refresh_token 用户刷新access_token
openid 授权用户唯一标识
scope 用户授权的作用域,使用逗号(,)分隔
unionid 当且仅当该移动应用已获得该用户的userinfo授权时,才会出现该字段
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.根据获取到的userToken && openID 继续网络请求获取userInfo
- (void)getUserInfo:(NSString*)token openID:(NSString*)openID{
    NSString* url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID];
    
    //网络请求 返回response包含UserInfo
 }

实现微信分享

步骤同上一、二、三

微信 SDK官方文档

代码实现

1.AppDelegate

设置分享支持类型

- (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.实现代理方法
- (void)onReq:(BaseReq *)req{
    NSLog(@"wx onReq");
}
  
- (void)onResp:(BaseResp *)resp{
    NSLog(@"wx onResp");
    //登录 分享 都走同一个回调
    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 发送到聊天界面
WXSceneTimeline 发送到朋友圈
WXSceneFavorite 添加到微信收藏

结束语

上一篇 下一篇

猜你喜欢

热点阅读