最近要写个微博关注的功能,看了很多资料,好多人说推荐友盟,说那个很好用,后来对比了还是选了ShareSDK,我只是需要一个关注功能。
按照他的sdk来,注册过新浪的开发者之后,得到了AppKey和AppSecret,添加到项目里面之后,方法如下:
[ShareSDK connectSinaWeiboWithAppKey:@"24372639"
appSecret:@"2be71dd05bc50e24342ad46acd5a"
redirectUri:@"https://api.weibo.com/oauth2/default.html"];
然后就是编写关注的代码
//在授权页面中添加关注官方微博
[authOptions setFollowAccounts:[NSDictionary dictionaryWithObjectsAndKeys:
[ShareSDK userFieldWithType:SSUserFieldTypeName value:@"苏州大学信息中心"],
SHARE_TYPE_NUMBER(ShareTypeSinaWeibo),
nil]];
[ShareSDK followUserWithType:ShareTypeSinaWeibo
field:@"苏州大学信息中心"
fieldType:SSUserFieldTypeName
authOptions:authOptions
viewDelegate:nil
result:^(SSResponseState state, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
NSString *msg = nil;
if (state == SSResponseStateSuccess)
{
msg = @"关注成功";
}
else if (state == SSResponseStateFail)
{
msg = [NSString stringWithFormat:@"关注失败:%@", error.errorDescription];
}
if (msg)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"知道了"
otherButtonTitles:nil];
[alertView show];
}
}];
调试了之后发现,控制台报错说没有通过SSO认证,通过web方式打开,很纳闷。
查了资料发现,原来需要加个url,在plist文件里面
大功告成!