1.导入第三方库和系统框架
2.product设置,注意objc的大小写
3.appDelegate.m
导入头文件
#import "AppDelegate.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import "WeiboSDK.h"
#import "ViewController.h"
4.在appDelegate.m的didFinishLaunchingWithOptions方法中添加如下代码
// 设置根视图
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
//微博授权登陆
[ShareSDK registerApp:@"iosv1101"
activePlatforms:@[@(SSDKPlatformTypeSinaWeibo)]
onImport:^(SSDKPlatformType platformType)
{
switch (platformType)
{
case SSDKPlatformTypeSinaWeibo:
[ShareSDKConnector connectWeibo:[WeiboSDK class]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
{
switch (platformType)
{
case SSDKPlatformTypeSinaWeibo:
//设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权
[appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
redirectUri:@"http://www.sharesdk.cn"
authType:SSDKAuthTypeBoth];
break;
default:
break;
}
}];
5.在viewController.m中添加如下代码
#import "ViewController.h"
#import "WeiboSDK.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
@interface ViewController ()
{
UILabel *lab;
UILabel *lab1;
UILabel *lab2;
UILabel *labname;
UILabel *labfan;
UILabel *labfriend;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"新浪微博登陆";
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"新浪微博登陆" forState:UIControlStateNormal];
btn.frame = CGRectMake(100, 200, 100, 30);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setTitle:@"退出" forState:UIControlStateNormal];
btn1.frame = CGRectMake(230, 200, 60, 30);
[self.view addSubview:btn1];
[btn1 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click
{
[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);
NSLog(@"followerCount=%ld",user.followerCount);
NSLog(@"friendCount=%lu",user.friendCount);
lab = [[UILabel alloc]initWithFrame:CGRectMake(150, 300, 150, 30)];
lab.text = user.nickname;
[self.view addSubview:lab];
lab1 = [[UILabel alloc]initWithFrame:CGRectMake(150, 400, 150, 30)];
lab1.text =[NSString stringWithFormat:@"%ld",user.followerCount];
[self.view addSubview:lab1];
lab2 = [[UILabel alloc]initWithFrame:CGRectMake(150, 500, 150, 30)];
lab2.text =[NSString stringWithFormat:@"%ld",user.friendCount];
[self.view addSubview:lab2];
labname = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 50, 30)];
labname.text = @"昵称:";
[self.view addSubview:labname];
labfan = [[UILabel alloc]initWithFrame:CGRectMake(50, 400, 100, 30)];
labfan.text = @"粉丝数:";
[self.view addSubview:labfan];
labfriend = [[UILabel alloc]initWithFrame:CGRectMake(50, 500, 100, 30)];
labfriend.text =@"好友数:";
[self.view addSubview:labfriend];
}
else
{
NSLog(@"%@",error);
}
}];
}
-(void)click2
{
[ShareSDK cancelAuthorize:SSDKPlatformTypeSinaWeibo];
[lab removeFromSuperview];
[lab1 removeFromSuperview];
[lab2 removeFromSuperview];
[labfan removeFromSuperview];
[labfriend removeFromSuperview];
[labname removeFromSuperview];
}
@end
效果图:点击登陆 点击授权 显示个人信息 点击退出
原文:http://www.cnblogs.com/Bo-tree/p/5095302.html