sina demo 说明(官方文档demo)

来源:互联网 发布:北斗手机导航软件 编辑:程序博客网 时间:2024/06/06 17:47

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.sinaWeiBohandleOpenURL:url];

}


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

{

    return [self.sinaWeiBohandleOpenURL:url];

}

如果多个sdk都需要重写这两个方法,则根据url的类型调用不同的sdk中的handOpenURL

2.登陆成功后程序没有记录登陆信息,每次重新登陆?

初始化SinaWeiBo对象时需要赋值给对象,如下(官方demo):

NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];

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

    if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfoobjectForKey:@"ExpirationDateKey"] &&

        [sinaweiboInfo objectForKey:@"UserIDKey"]) {

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

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

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

    }


登陆成功后,要存下登陆信息

SinaWeibo *sinaweibo = [appDelegatesinaWeiBo];(appDelegate = (AppDelegate *)[[UIApplication shareApplication] delegate])

    NSDictionary *authData = [NSDictionarydictionaryWithObjectsAndKeys:

                              sinaweibo.accessToken,@"AccessTokenKey",

                              sinaweibo.expirationDate,@"ExpirationDateKey",

                              sinaweibo.userID,@"UserIDKey",

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

    [[NSUserDefaults standardUserDefaults] setObject:authDataforKey:@"SinaWeiboAuthData"];

    [[NSUserDefaults standardUserDefaults] synchronize];

登出微博,需要将这些信息清除

[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"SinaWeiboAuthData"];

[[NSUserDefaults standardUserDefaults]synchronize];


3.分享一张网络图片,已知URL地址

SinaWeibo *sinaweibo = [appDelegatesinaWeiBo];

    

    UIImage * tempImage = [UIImageimageWithData:[NSData dataWithContentsOfURL:[NSURLURLWithString:@"http://static.androidesk.com/download/5099c1050a2ae0706b7cbc38"]]];

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

                       params:[NSMutableDictionarydictionaryWithObjectsAndKeys:

                               @"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 : %@", [errorlocalizedDescription]);

    

    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!"]

                                                           delegate:nil cancelButtonTitle:nilotherButtonTitles:@"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! with error : %@", [errorlocalizedDescription]]

                                                           delegate:nil cancelButtonTitle:nilotherButtonTitles:@"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.urlhasSuffix:@"users/show.json"])

    {

        [userInfo release];

        userInfo = [result retain];

    }

    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:nilotherButtonTitles:@"OK", nil];

        [alertView show];

        [alertView release];

        

    }

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

    {

        NSLog(@"Request ---> : %@", result);

        //发送微博成功

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

                                                            message:[NSString stringWithFormat:@"Post image status  succeed!"]

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

        [alertView show];

        [alertView release];

        

    }

}




原创粉丝点击