sina demo 说明(官方文档demo)

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提取需要的功能,以分享网络图片为例

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];

        

    }

}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值