iOS-Snapchat登录集成及注意事项

857 阅读2分钟

Snapchat是海外最火的社交App之一,它拥有着大量的年轻用户群体,因此,灵活运用它的SDK,对于出海类社交App来说至关重要。它提供的SDK包括登录(Login Kit),表情贴纸(Bitmoji Kit),创意分享(Creative Kit),本文重点讲解最基础的一环-Snapchat登录

Snapchat 官方集成文档
实现整个 Snapchat 登录功能,主要分以下三个步骤

1. Snapchat 控制台的配置

Snapchat 控制台地址

Snapchat.png

Snapchat.png

控制台的配置按照相应描述填写即可。

Redirect URL这里建议格式填写为myapp://bundle id/oauth2

Snapchat.png

备注:测试 Snapchat 的时候,要在 Demo Users 里添加 Snapchat 的测试 ID,不然会报错。

2. SDK 的集成与配置

  • Podfile 里文件添加如下
    pod 'SnapSDK'
  • Plist 里配置如下:
<key>SCSDKScopes</key>
    <array>
        <string>https://auth.snapchat.com/oauth2/api/user.display_name</string>
        <string>https://auth.snapchat.com/oauth2/api/user.external_id</string>
        <string>https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar</string>
    </array>

SCSDKScopes是用来告诉Snapchat你需要申请用户的什么数据。

是用户的昵称

Snapchat里的display name和user name是两回事,display name是我们理解的昵称,user name是我们理解的唯一ID。我们添加Snapchat好友是用的user name。

是我们拿到的授权 ID,后台拿来做唯一标识的。

Snapchat并未提供后台SDK,所以后台是无法判断前端给的external id是否真实存在。因此,如果App有相应业务场景的话,一定要做好接口加密,防止批量被刷。

很好理解,即是用户头像。
另外,Plist 里还需加入以下内容

<key>SCSDKClientId</key>
    <string>控制台申请的客户端ID</string>
    <key>SCSDKRedirectUrl</key>
    <string>控制台填写的回调URL</string>
<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>snapchat</string>
        <string>bitmoji-sdk</string>
        <string>itms-apps</string>
    </array>
  • Info 里配置如下:
    TARGETS-Info-URL Types
    Info.png Identifier 填写 App 名字即可
    URL Schemes 填写 bundle ID 即可

Identifier这里要和RedirectUrl进行对应,如果你的RedirectUrl格式是myapp://bundle id/oauth2 那么Identifier 就填写myapp。URL Schemes那里要填写bundle id。

3. 代码配置

AppDelegate 中

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    
    BOOL handled = [SCSDKLoginClient application:app openURL:url options:options];
    if (handled) {
        NSLog(@"是Snapchat登录");
        //展示HUD
        [PCHUD showHudWith:@""];
    }else{
        NSLog(@"不是Snapchat登录");
    }
    
    return handled;
}

LoginViewController

- (void)snapChatLogin {
    [SCSDKLoginClient loginFromViewController:self completion:^(BOOL success, NSError * _Nullable error) {
        [PCHUD removeHudWith:@""];
        if (error) {
            NSLog(@"授权失败error=%@",error);
        }
        if (success) {
            NSLog(@"授权成功");
        ];
            [self fetchUserData];
        }
    }];
}
- (void)fetchUserData {
    [PCHUD showHudWith:@""];
    //这里是GraphQL 获取需要的用户信息,可以根据需要获取
    NSString *queryString = @"{me{bitmoji{avatar,selfie},displayName,externalId}}";
    [SCSDKLoginClient fetchUserDataWithQuery:queryString variables:nil success:^(NSDictionary * _Nullable resources) {
        NSDictionary *paramDict = resources[@"data"][@"me"];
        NSLog(@"用户信息resources=%@",paramDict);
        //把字段信息传给后台
        [self doLoginLogical:paramDict];
        
    } failure:^(NSError * _Nullable error, BOOL isUserLoggedOut) {
        NSLog(@"用户获取信息授权失败error=%@",error);
        [PCHUD removeHudWith:@""];
    }];
}