美文网首页程序员
关于ShareSDK的使用

关于ShareSDK的使用

作者: Ghstart | 来源:发表于2014-03-13 16:15 被阅读3061次

    由于公司极度恶心的要使用网易的授权登录,找了好久,最终选择了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的官网上视频中有讲解。

    相关文章

      网友评论

        本文标题:关于ShareSDK的使用

        本文链接:https://www.haomeiwen.com/subject/kejktttx.html