原始地址:IOS 6社交应用开发-新浪微博
1.添加Framework.
2.导入头文件.
#import <Accounts/Accounts.h>
#import <Social/Social.h>
3.确保在“设置”里配置了社交应用的帐户(以新浪微博举例),如下图。
4.获取新浪微博用户.
//获取帐号存储
ACAccountStore *strore = [[ACAccountStore alloc] init];
//获取新浪微博的帐号类型
ACAccountType *type = [strore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierSinaWeibo];
//验证是否配置了匹配帐户
[strore requestAccessToAccountsWithType:type options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)//验证授权成功
{
//获取新浪微博用户列表
NSArray *counts = [strore accountsWithAccountType:type];
if (counts && [counts count] > 0)
{
//认证通过
//以第一个用户举例
[self requestWithAccount:[counts objectAtIndex:0]];
}
}
}];
5.发布微博.
- (void)requestWithAccount:(ACAccount *)account
{
/*
//类型
SLServiceTypeTwitter
SLServiceTypeFacebook
SLServiceTypeSinaWeibo
//方法
SLRequestMethodGET
SLRequestMethodPOST
SLRequestMethodDELETE
*/
//请求地址,参考上图
NSURL *url = [NSURL URLWithString:@"https://open.weibo.cn/2/statuses/update"];
//配置参数字典
NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:@"ssstext", @"status", nil];
//配置轻取
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeSinaWeibo requestMethod:SLRequestMethodGET URL:url parameters:para];
//装载微博用户
request.account = account;
//发送微博
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (!error)
{
//主线程中操作UI
dispatch_async(dispatch_get_main_queue(), ^{
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"请求结果:%@",response);
//操作UI
});
}
}];
}