IOS,第三方登錄和分享

#使用ShareSDK完成第三方()登錄和分享git

###這幾天遇到一個需求:作第三方登錄和分享,在這把整個過程記錄下來。方便他人,本身也捋一下思路。api

第一次作的時候是直接下載的sdk在配置拖到項目裏面,而後發現會報錯,後來仍是決定用cocoapod,我的以爲cocoapod比較簡單。過程以下:

一、去ShareSDK官網註冊一個帳號。微信

輸入圖片說明

二、添加一個應用,這裏我建立了一個tenst應用。網絡

輸入圖片說明

建立後進去的頁面以下:測試

輸入圖片說明

這裏會有App Key 和App Specret。ui

三、而後就是cocoapod的安裝了,這個在ShareSDK官網有說明按照步驟來就能夠了。url

四、到相應開發者平臺註冊開發者帳號,並添加你要用的第三方登錄和分享的信息。code

添加新浪微博應用 註冊網址 http://open.weibo.comorm

添加QQ應用 註冊網址 http://mobile.qq.com/api/token

添加微信應用 http://open.weixin.qq.com

註冊完就能夠拿到相應的App Key 和App Specret。

輸入圖片說明

五、添加URL Schemes。 點擊:項目名 > TARGETS > info > URL Types 點擊添加URL Types。 如圖:

輸入圖片說明

在URL Schemes後面的框裏添上對應信息。

新浪微博的URL Schemes是:wb 加上在微博開發平臺上申請的AppKey

到此第三方的一些工做差很少就是完了

六、寫分享代碼

(1)、在界面上添加按鈕,並綁定響應事件

(2)、寫事件處理代碼以下:

- (IBAction)SinaweiboShare:(UIButton *)sender {
    
    //建立分享參數
    //    NSString *imageUrl = [_responseDataDic objectForKey:@"picUrl"];
    //    (注意:圖片必需要在Xcode左邊目錄裏面,名稱必需要傳正確,若是要分享網絡圖片,能夠這樣傳iamge參數 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
    //    if (imageUrl) {
    NSString *title = @"title";
    NSString *content = [NSString stringWithFormat:@"項目測試實驗%@",title];
    
    NSString *urlString = @"www.sina.com";
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:content
                                     images:nil
                                        url:url
                                      title:title
                                       type:SSDKContentTypeAuto];
    
    //新浪微博
    [shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"%@ %@",content,urlString] title:title image:nil url:nil latitude:0 longitude:0 objectID:nil type:SSDKContentTypeText];
    
    //分享(能夠彈出咱們的分享菜單和編輯界面)
    [ShareSDK showShareActionSheet:nil
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                       case SSDKResponseStateSuccess:
                       {
                           
                           
                           
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           
                           break;
                       }
                       default:
                           break;
                   }
               }];

七、登錄代碼

(1)、在界面添加按鈕,並綁定響應事件。

(2)、寫響應代碼(以微博位例,其它都差很少,不一樣點在Type這個參數上)

- (IBAction)SinaweiboLogin:(UIButton *)sender {

    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
             
             self.name.text = user.nickname;
             [self.image sd_setImageWithURL:[NSURL URLWithString:user.icon]];
         }
         
         else
         {
             NSLog(@"%@",error);
         }
         
     }];

到這裏因此就算是作完了。

相關文章
相關標籤/搜索