1.如何让官方demo跑起来?
#define kAppKey @"your app_key"
#define kAppSecret @"your app_secret"
#define kAppRedirectURI @"your app_rederict_uri"
上面这三个东西一定要有,否则不行。
2.对于sso登陆,如何回到demo?
- 打开demo的Info.plist
- 将Info.plist中URL Schemes的sinaweibosso.xxxxx,(xxx换成你自己的appkey)
进阶:根据官方demo提取需要的功能,以分享网络图片为例
DEMO地址:http://download.csdn.net/detail/take8619702/4797035
1.sso登陆之后不能回到自己的程序?
需要重写AppDelegate中的两个方法为:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [self.sinaWeiBo handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [self.sinaWeiBo handleOpenURL:url];
}
如果多个sdk都需要重写这两个方法,则根据url的类型调用不同的sdk中的handOpenURL
2.登陆成功后程序没有记录登陆信息,每次重新登陆?
初始化SinaWeiBo对象时需要赋值给对象,如下(官方demo):
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *sinaweiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"];
if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfo objectForKey:@"ExpirationDateKey"] &&
[sinaweiboInfo objectForKey:@"UserIDKey"]) {
_sinaWeiBo.accessToken = [sinaweiboInfo objectForKey:@"AccessTokenKey"];
_sinaWeiBo.expirationDate = [sinaweiboInfo objectForKey:@"ExpirationDateKey"];
_sinaWeiBo.userID = [sinaweiboInfo objectForKey:@"UserIDKey"];
}
登陆成功后,要存下登陆信息
SinaWeibo *sinaweibo = [appDelegate sinaWeiBo];(appDelegate = (AppDelegate *)[[UIApplication shareApplication] delegate])
NSDictionary *authData = [NSDictionary dictionaryWithObjectsAndKeys:
sinaweibo.accessToken, @"AccessTokenKey",
sinaweibo.expirationDate, @"ExpirationDateKey",
sinaweibo.userID, @"UserIDKey",
sinaweibo.refreshToken, @"refresh_token", nil];
[[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"SinaWeiboAuthData"];
[[NSUserDefaults standardUserDefaults] synchronize];
登出微博,需要将这些信息清除[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SinaWeiboAuthData"];
[[NSUserDefaults standardUserDefaults] synchronize];
3.分享一张网络图片,已知URL地址
SinaWeibo *sinaweibo = [appDelegate sinaWeiBo];
UIImage * tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://static.androidesk.com/download/5099c1050a2ae0706b7cbc38"]]];
[sinaweibo requestWithURL:@"statuses/upload.json"
params:[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"sina new interface success maybe", @"status",
tempImage, @"pic", nil]
httpMethod:@"POST"
delegate:self];//delegate sinaweiborequestdelegate
4.如何知道分享状态,成功或者失败?
实现SinaWeiboRequestDelegate代理,可以得到返回状态的request
官方方法:
- (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError : %@", [error localizedDescription]);
if ([request.url hasSuffix:@"users/show.json"])
{
[userInfo release], userInfo = nil;
}
else if ([request.url hasSuffix:@"statuses/user_timeline.json"])
{
[statuses release], statuses = nil;
}
else if ([request.url hasSuffix:@"statuses/update.json"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Post status failed!"]
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
[alertView release];
NSLog(@"Post status failed with error : %@", error);
}
else if ([request.url hasSuffix:@"statuses/upload.json"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Post image status failed! with error : %@", [error localizedDescription]]
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
[alertView release];
NSLog(@"Post image status failed with error : %@", error);
}
}
- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result
{
NSLog(@"didFinishLoadingWithResult :%@", request);
if ([request.url hasSuffix:@"users/show.json"])
{
[userInfo release];
userInfo = [result retain];
}
else if ([request.url hasSuffix:@"statuses/user_timeline.json"])
{
[statuses release];
statuses = [[result objectForKey:@"statuses"] retain];
}
else if ([request.url hasSuffix:@"statuses/update.json"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Post status \"%@\" succeed!", [result objectForKey:@"text"]]
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
[alertView release];
}
else if ([request.url hasSuffix:@"statuses/upload.json"])
{
NSLog(@"Request ---> : %@", result);
//发送微博成功
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
message:[NSString stringWithFormat:@"Post image status succeed!"]
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
[alertView release];
}
}