iOS 圖文教程完成三方登錄

前言: 首先你要明白, 這種集成三方的東西, 都很是簡單,可能會有坑, 填上就好, 這篇文章以微博爲例, 其實都是同樣的. 步驟或繁或簡單, 習慣就好.ios

1. 首先在微博開放平臺註冊成爲開發者http://open.weibo.comgit

2. 建立應用github

iOS 圖文教程完成三方登錄

3. 完善應用信息, 這裏須要注意的是Bundle ID須要與應用一致, 同時記錄appKey 項目中會用到app

4. 在應用信息-高級信息中url

iOS 圖文教程完成三方登錄

iOS 圖文教程完成三方登錄

與上方的保持一致---這段代碼後面後使用到調試

5. 填寫URL Typescode

iOS 圖文教程完成三方登錄

6. 配置info.plist同時加入白名單orm

iOS 圖文教程完成三方登錄

<array>
  <string>sinaweibohd</string>
  <string>sinaweibo</string>
  <string>weibosdk</string>
  <string>weibosdk2.5</string>
 </array>

7. 導入SDK.教程

7.1 使用cocoapods你能夠這樣token

pod "WeiboSDK", :git => "https://github.com/sinaweibosdk/weibo_ios_sdk.git"

7.2 或者直接下載拖入工程

8. 代碼

// AppDelegate中
#import <WeiboSDK.h>
#define WB_iOS_Key @""
@interface AppDelegate ()<WeiboSDKDelegate>
/**
     *  1. 設置WeiboSDK的調試模式
     *  2. 註冊
     */
    [WeiboSDK enableDebugMode:YES];
    [WeiboSDK registerApp:WB_iOS_Key];
#pragma mark - WeiboSDKDelegate
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request
{

}
// 根據本身的需求寫回調
// 這是微博Demo提供的代碼
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
    {
        NSString *title = NSLocalizedString(@"發送結果", nil);
        NSString *message = [NSString stringWithFormat:@"%@: %d\\n%@: %@\\n%@: %@", NSLocalizedString(@"響應狀態", nil), (int)response.statusCode, NSLocalizedString(@"響應UserInfo數據", nil), response.userInfo, NSLocalizedString(@"原請求UserInfo數據", nil),response.requestUserInfo];

        WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
        NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
        if (accessToken)
        {
            self.wbtoken = accessToken;
        }
        NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
        if (userID) {
            self.wbCurrentUserID = userID;
        }
    }
    else if ([response isKindOfClass:WBAuthorizeResponse.class])
    {
        NSString *title = NSLocalizedString(@"認證結果", nil);
        NSString *message = [NSString stringWithFormat:@"%@: %d\\nresponse.userId: %@\\nresponse.accessToken: %@\\n%@: %@\\n%@: %@", NSLocalizedString(@"響應狀態", nil), (int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken],  NSLocalizedString(@"響應UserInfo數據", nil), response.userInfo, NSLocalizedString(@"原請求UserInfo數據", nil), response.requestUserInfo];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"肯定", nil)
                                              otherButtonTitles:nil];

        self.wbtoken = [(WBAuthorizeResponse *)response accessToken];
        self.wbCurrentUserID = [(WBAuthorizeResponse *)response userID];
        self.wbRefreshToken = [(WBAuthorizeResponse *)response refreshToken];
        [alert show];
    }
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WeiboSDK handleOpenURL:url delegate:self ];
}

9. 寫個登錄按鈕登錄

- (void)loginClick
{
    [[[[UIApplication sharedApplication] delegate] window] makeKeyWindow];

    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    // 前面提到的
    request.redirectURI = kRedirectURI;
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];
}

10. 完成圖

iOS 圖文教程完成三方登錄

iOS 圖文教程完成三方登錄

iOS 圖文教程完成三方登錄

狀態0 表明成功. 若是回憶沒有錯基本就成功了. O.-

相關文章
相關標籤/搜索