iOS-Snapchat登錄集成及注意事項 1. Snapchat控制檯的配置 2. SDK的集成與配置 3.代碼配置

Snapchat是海外最火的社交App之一,它擁有着大量的年輕用戶羣體,因此,靈活運用它的SDK,對於出海類社交App來說至關重要。它提供的SDK包括登錄(Login Kit),表情貼紙(Bitmoji Kit),創意分享(Creative Kit),本文重點講解最基礎的一環-Snapchat登錄

Snapchat官方集成文檔
實現整個Snapchat登錄功能,主要分以下三個步驟

1. Snapchat控制檯的配置

Snapchat控制檯地址


控制檯的配置按照相應描述填寫即可。
Redirect URL這裏建議格式填寫爲myapp://bundle id/oauth2

備註:測試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你需要申請用戶的什麼數據。

\color{red}{display name} 是用戶的暱稱

Snapchat裏的display name和user name是兩回事,display name是我們理解的暱稱,user name是我們理解的唯一ID。我們添加Snapchat好友是用的user name。

\color{red}{external id}是我們拿到的授權ID,後臺拿來做唯一標識的。

Snapchat並未提供後臺SDK,所以後臺是無法判斷前端給的external id是否真實存在。因此,如果App有相應業務場景的話,一定要做好接口加密,防止批量被刷。

\color{red}{avatar}很好理解,即是用戶頭像。
另外,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

    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:@""];
    }];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章