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);
         }
         
     }];

到这里因此就算是作完了。

相关文章
相关标签/搜索