新浪微博ios

申请了新浪开发者帐号后,下载了sdk。

首先要为 AppDelegate添加一个对象:

@interface AppDelegate :UIResponder <UIApplicationDelegate>

{

   SinaWeibo *_sinaweibo;

}

@property (readonly,nonatomic)SinaWeibo *sinaweibo;

在.m中初始化

@synthesize sinaweibo=_sinaweibo;


 //微博sdk初始化

    _sinaweibo = [[SinaWeiboalloc]initWithAppKey:kAppKeyappSecret:kAppSecretappRedirectURI:kAppRedirectURIandDelegate:_viewController];

    NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];

   NSDictionary *sinaweiboInfo = [defaultsobjectForKey:@"SinaWeiboAuthData"];

   if ([sinaweiboInfoobjectForKey:@"AccessTokenKey"] && [sinaweiboInfoobjectForKey:@"ExpirationDateKey"] && [sinaweiboInfoobjectForKey:@"UserIDKey"])

    {

       _sinaweibo.accessToken = [sinaweiboInfoobjectForKey:@"AccessTokenKey"];

       _sinaweibo.expirationDate = [sinaweiboInfoobjectForKey:@"ExpirationDateKey"];

       _sinaweibo.userID = [sinaweiboInfoobjectForKey:@"UserIDKey"];

    }


- (void)applicationDidBecomeActive:(UIApplication *)application

{

    [self.sinaweiboapplicationDidBecomeActive];

}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

   return [self.sinaweibohandleOpenURL:url];

}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

   return [self.sinaweibohandleOpenURL:url];

}


然后在viewController中实现如下

SinaWeiboDelegate, SinaWeiboRequestDelegate

#pragma mark - SinaWeibo Delegate

//登陆成功

- (void)sinaweiboDidLogIn:(SinaWeibo *)sinaweibo

{

    NSLog(@"sinaweiboDidLogIn userID = %@ accesstoken = %@ expirationDate = %@ refresh_token = %@", sinaweibo.userID, sinaweibo.accessToken, sinaweibo.expirationDate,sinaweibo.refreshToken);

    

    [selfresetButtons];

    [selfstoreAuthData];

}

//注销成功

- (void)sinaweiboDidLogOut:(SinaWeibo *)sinaweibo

{

    NSLog(@"sinaweiboDidLogOut");

    [selfremoveAuthData];

    [selfresetButtons];

}

//登陆取消

- (void)sinaweiboLogInDidCancel:(SinaWeibo *)sinaweibo

{

    NSLog(@"sinaweiboLogInDidCancel");

}

//登陆出错

- (void)sinaweibo:(SinaWeibo *)sinaweibo logInDidFailWithError:(NSError *)error

{

    NSLog(@"sinaweibo logInDidFailWithError %@", error);

}

//开发者帐号权限出错

- (void)sinaweibo:(SinaWeibo *)sinaweibo accessTokenInvalidOrExpired:(NSError *)error

{

    NSLog(@"sinaweiboAccessTokenInvalidOrExpired %@", error);

    [selfremoveAuthData];

    [selfresetButtons];

}


#pragma mark - SinaWeiboRequest Delegate 


- (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error

{

    if ([request.urlhasSuffix:@"users/show.json"])

    {

        [userInfo release], userInfo = nil;

    }

    elseif ([request.urlhasSuffix:@"statuses/user_timeline.json"])

    {

        [statuses release], statuses = nil;

    }

    elseif ([request.urlhasSuffix:@"statuses/update.json"])

    {

        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"Alert"

                                                            message:[NSString stringWithFormat:@"Post status \"%@\" failed!",postStatusText]

                                                           delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

        [alertView show];

        [alertView release];

        

        NSLog(@"Post status failed with error : %@", error);

    }

    elseif ([request.urlhasSuffix:@"statuses/upload.json"])

    {

        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"Alert"

                                                            message:[NSString stringWithFormat:@"Post image status \"%@\" failed!",postImageStatusText]

                                                           delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

        [alertView show];

        [alertView release];

        

        NSLog(@"Post image status failed with error : %@", error);

    }

    

    

    [selfresetButtons];

}


- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result

{

    if ([request.urlhasSuffix:@"users/show.json"])

    {

        [userInfo release];

        userInfo = [result retain];

        NSLog(@"userInfo---%@",userInfo);

    }

    elseif ([request.urlhasSuffix:@"statuses/user_timeline.json"])

    {

        [statuses release];

        statuses = [[result objectForKey:@"statuses"] retain];

    }

    elseif ([request.urlhasSuffix:@"statuses/update.json"])

    {

        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"Alert"

                                                            message:[NSString stringWithFormat:@"Post status \"%@\" succeed!", [resultobjectForKey:@"text"]]

                                                           delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

        [alertView show];

        [alertView release];


        [postStatusTextrelease],postStatusText = nil;

    }

    elseif ([request.urlhasSuffix:@"statuses/upload.json"])

    {

        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"Alert"

                                                            message:[NSString stringWithFormat:@"Post image status \"%@\" succeed!", [resultobjectForKey:@"text"]]

                                                           delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

        [alertView show];

        [alertView release];

        

        [postImageStatusTextrelease],postImageStatusText = nil;

    }

    

    [selfresetButtons];

}

//初始化设置

- (SinaWeibo *)sinaweibo

{

    SNAppDelegate *delegate = (SNAppDelegate *)[UIApplicationsharedApplication].delegate;

    return delegate.sinaweibo;

}


- (void)removeAuthData

{

    [[NSUserDefaultsstandardUserDefaults]removeObjectForKey:@"SinaWeiboAuthData"];

}


- (void)storeAuthData

{

    SinaWeibo *sinaweibo = [selfsinaweibo];

    

    NSDictionary *authData = [NSDictionarydictionaryWithObjectsAndKeys:

                              sinaweibo.accessToken,@"AccessTokenKey",

                              sinaweibo.expirationDate,@"ExpirationDateKey",

                              sinaweibo.userID,@"UserIDKey",

                              sinaweibo.refreshToken,@"refresh_token",nil];

    [[NSUserDefaultsstandardUserDefaults]setObject:authDataforKey:@"SinaWeiboAuthData"];

    [[NSUserDefaultsstandardUserDefaults]synchronize];

}


发送操作请求如下所示

 SinaWeibo *sinaweibo = [selfsinaweibo];

    [sinaweibo requestWithURL:@"statuses/user_timeline.json"

                      params:[NSMutableDictionarydictionaryWithObject:sinaweibo.userIDforKey:@"uid"]

                  httpMethod:@"GET"

                    delegate:self];

结果在中处理。

- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result

注意:一、通过对sinaweibo.delegate 修改,我们可以修改接收消息的viewController。

     二、

[sinaweibo requestWithURL:@"statuses/user_timeline.json"

                      params:[NSMutableDictionarydictionaryWithObject:sinaweibo.userIDforKey:@"uid"]

                  httpMethod:@"GET"

                    delegate:self];

最终是拼凑一个网页URL,形如:https://api.weibo.com/2/statuses/show.json?id=3481475946781445&access_token=2.....

params 的值如果为nil,则默认为access_token = 2.......形式。

httpMethod:为@"GET"或者@"POST"

我写了个demo源码地址:http://download.csdn.net/detail/cloud95/5225422


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值