MyEyes项目开发日记之三(添加新浪微博功能)

来源:互联网 发布:mac imovie使用教程 编辑:程序博客网 时间:2024/06/06 02:49

  为了增加MyEyes软件的互动性和推广,我们希望加入在测得视力后把结果分享到新浪微博的功能。不过由于新浪微博的审批程序的繁琐。在首次发布时,我们打算先屏蔽微博功能。但实际微博分享代码已经加入到程序里。下面就简单介绍下如何使用微博sdk。

1.首先从以下新浪开发者平台下载新浪微博SDK包。(当然,如果觉的对方挺供的sdk包封装的方法不好,也可以自己直接调用新浪提供的微博API,自己封装接口)

新浪微博iOS SDK下载

2.导入sdk到项目里。把sdk下的src文件里的内容,拖拽到自己的项目里。并添加Security.framework到项目里。

3.最关键的,就是要到open.weibo.com里去用自己新浪微博账号登陆,完成开发者信息。然后添加应用,就可以得到相应的App Key 和 App Secrect。用这个key和secrect就可以测试开发了。当然,这个测试发微博时,最多只能添加15个微博账号测试。如果需要更多的使用,就要去申请审批。审批流程就不在这里说了,大家可以去open.weibo.com上详细查阅。

下面简单说下sdk的使用方法。

最开始,要把申请到的AppKey和AppSecrect定义自己的项目里。

#define kWBSDKDemoAppKey @"53******24"

#define kWBSDKDemoAppSecret @"11d453a7***************3f1d9a62"

然后就可以使用sdk包里封装好的方法了。

1.微博登陆

WBEngine *engine = [[WBEngine alloc] initWithAppKey:kWBSDKDemoAppKey appSecret:kWBSDKDemoAppSecret];[engine setRootViewController:self];[engine setDelegate:self];[engine setRedirectURI:@"http://"];[engine setIsUserExclusive:NO];self.weiBoEngine = engine; [self.weiBoEngine logIn];

这段代码只是简单的调用微博登陆验证的webview页面,目前sdk里的做法,在程序末关闭时,登陆后,会记录登陆信息,也就是说,只需登陆一次。

2.登陆后的回调

- (void)engine:(WBEngine *)engine didFailToLogInWithError:(NSError *)error{    NSLog(@"didFailToLogInWithError: %@", error);    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil    message:@"登录失败!"   delegate:nil cancelButtonTitle:@"确定"  otherButtonTitles:nil];[alertView show];}- (void)engineDidLogOut:(WBEngine *)engine{    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil    message:@"登出成功!"   delegate:self cancelButtonTitle:@"确定"  otherButtonTitles:nil];    [alertView setTag:kWBAlertViewLogOutTag];[alertView show];}- (void)engineNotAuthorized:(WBEngine *)engine{    }- (void)engineAuthorizeExpired:(WBEngine *)engine{    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil    message:@"请重新登录!"   delegate:nil cancelButtonTitle:@"确定"  otherButtonTitles:nil];[alertView show];}

登陆成功与否的回调方法,我们在收到回调,弹出相应的对话框。


3.微博发送

    [weiBoEngine sendWeiBoWithText:[NSString stringWithFormat:@"我用视力宝测得视力 左眼:%@ 右眼:%@",resultLblLeft.text,resultLblRight.text] image:screenshotImage];

这里只是传入发送微博的文字和图标。并不调用发送界面。因为我们并不想让用户自定义发送的内容。


3. 发送后的回调

- (void)engine:(WBEngine *)engine requestDidSucceedWithResult:(id)result{    NSLog(@"requestDidSucceedWithResult: %@", result);    UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil    message:@"成功发送到微博!"   delegate:self cancelButtonTitle:@"确定"  otherButtonTitles:nil];[alertView show];}- (void)engine:(WBEngine *)engine requestDidFailWithError:(NSError *)error{    NSLog(@"requestDidFailWithError: %@", error);    NSNumber *errorCode = [error.userInfo objectForKey:@"error_code"];    NSLog(@"errorCode = %d", errorCode.intValue);    switch (errorCode.intValue) {        case 20019:        {            UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil                                                                message:@"不要太贪心哦,发一次就够啦!"                                                               delegate:self                                                     cancelButtonTitle:@"确定"                                                      otherButtonTitles:nil];            [alertView show];            break;        }        default:            break;    }}

前一个为发送成功回调。后者为失败的。如果我们需要查询失败的原因,我们可以从error中读出error_code,然后在官方文档中查询错误代码。对比出具体的原因。

错误代码查询

以上为sdk中提供的最简单的发送微博的方法。对于我们的MyEyes来说,这些功能也就够用了。仅供大家参考下。


原创粉丝点击