关于ShareSDK的使用

由于公司极度恶心的要使用网易的授权登录,找了好久,最终选择了ShareSDK,发现还是很好用的。

直接代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //注册shareSDk
    [ShareSDK registerApp:@"xxxxx"];
    //sina
    [ShareSDK connectSinaWeiboWithAppKey:@"38xxxx979"
                               appSecret:@"7df5xxxxbf7c"
                             redirectUri:@"http://www.baidu.com"];
    //网易
    [ShareSDK connect163WeiboWithAppKey:@"T5EI7BXe13vfyDuy"
                              appSecret:@"gZxwyNOvjFYpxwwlnuizHRRtBRZ2lV1j"
                            redirectUri:@"http://www.shareSDK.cn"];
    
    //人人
    [ShareSDK connectRenRenWithAppId:@"226427"
                              appKey:@"fc5b8aed373c4c27a05b712acba0f8c3"
                           appSecret:@"f29df781abdd4f49beca5a2194676ca4"
                   renrenClientClass:[RennClient class]];
    
    //QQ空间
    [ShareSDK connectQZoneWithAppKey:@"100371282"
                           appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
                   qqApiInterfaceCls:[QQApiInterface class]
                     tencentOAuthCls:[TencentOAuth class]];
    
    
    //这是模拟服务器的具体在www.parse.com上找到
    [Parse setApplicationId:@"Arqhxxxxx26seU9Nb"
                  clientKey:@"ixKhwYxxxxp9FHdf"];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.rootViewController = [[FirViewController alloc] init];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

2.在viewController中

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 50, 100, 100);
    [button setTitle:@"Sina授权" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)buttonAction:(UIButton *)button
{
    if ([ShareSDK hasAuthorizedWithType:ShareTypeSinaWeibo]) {
        MainViewController *mainVC = [[MainViewController alloc] init];
        [self presentViewController:mainVC animated:YES completion:NULL];
        
    }else{
        [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo
                          authOptions:nil
                               result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
                                   
                                   if (result)
                                   {
                                       PFQuery *query = [PFQuery queryWithClassName:@"UserInfo"];
                                       [query whereKey:@"uid" equalTo:[userInfo uid]];
                                       [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                                           
                                           if ([objects count] == 0)
                                           {
                                               PFObject *newUser = [PFObject objectWithClassName:@"UserInfo"];
                                               [newUser setObject:[userInfo uid] forKey:@"uid"];
                                               [newUser setObject:[userInfo nickname] forKey:@"name"];
                                               [newUser setObject:[userInfo profileImage] forKey:@"profileImage"];
                                               [newUser saveInBackground];
                                               
                                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello"
                                                                                                   message:@"欢迎注册"
                                                                                                  delegate:nil
                                                                                         cancelButtonTitle:@"知道了"
                                                                                         otherButtonTitles:nil];
                                               [alertView show];
                                           }else
                                           {
                                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello"
                                                                                                   message:@"欢迎回来"
                                                                                                  delegate:nil
                                                                                         cancelButtonTitle:@"知道了"
                                                                                         otherButtonTitles:nil];
                                               [alertView show];
                                           }
                                           
                                       }];
                                       
                                       //                                   MainViewController *mainVC  = [[MainViewController alloc] init];
                                       //                                   [self presentViewController:mainVC animated:YES completion:NULL];
                                   }
                               }];

    }
    
}

详情可在ShareSDK的官网上视频中有讲解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,872评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 15,136评论 4 61
  • 其实学美甲的方式应该是有三种的,一种是自学,一种是去美甲店做学徒,一种是考虑去专业的北京美甲学校学习,那么这几种方...
    _____fe63阅读 940评论 0 0
  • 光亮的那方,每个人都很向往。裹足不前的思想,也许只要我们敢,排除万难,每个人都遇见阳光,那只是我们的梦想,却被你们...
    东去烟雨阅读 341评论 0 0
  • 今天我做直播,一个听的人也没有。老实说我很失望。从第一次做直播开始已经十天过去了,但粉丝却越来越少,今天干脆一个人...
    兰奕阅读 146评论 0 0

友情链接更多精彩内容