iOS5.0以上使用新浪微博开放平台OAuth 续(及解决登录无效问题)

新浪微博开放平台为第三方应用提供了简便的合作模式,满足了手机用户和平板电脑用户随时随地分享信息的需求。通过调用平台的api即可实现很多微博上的功能。

本篇主要目的是记录新浪微博移动SDK iOS版本的在iOS5下的嵌入和使用。

1、申请一个新浪微博的移动应用 。

申请地址:http://open.weibo.com/development,申请后得到App key 和 App Secret

2、下载iOS_sdk

下载地址:http://open.weibo.com/wiki/SDK#iOS_SDK  ,下载第一个就ok了。

3、新建一个项目Sina_weibo,选择single View app。而且使用5.0后的ARC特性 。 导入解压后的sdk

导入SDK

4、适配SDK在arc环境下运行

这时候运行程序,你会发现很多关于ARC的错误,因为sdk里是没有使用arc的。这时候如果想sdk的文件不参与arc方式的编译,那就需要做下设置,在Build Phases里添加“-fno-objc-arc”标示

双击需要标识的文件,输入-fno-objc-arc。

这样weibo SDK的文件就不会以arc的方式编译了。

5、 在自己的工程里面增加Security.framework。SDK需要使用Security.framework将OAuth认证以后的token放到keyChain里面从而增加整个工程的安全性。

这时候运行,程序就编译运行正常了

6、其他的和SDK里的Demo一样了

登录调用

    [weiBoEnginelogIn]; 

注销调用

    [weiBoEnginelogOut];

发微博:

可以调用SDK默认的界面发送:

 

    WBSendView *sendView = [[WBSendViewalloc] initWithAppKey:appKeyappSecret:appSecrettext:@"test"image:[UIImageimageNamed:@"bg.png"]];

    [sendView setDelegate:self];

    [sendView show:YES];

对应的发送微博的api是:statuses/upload 发送微博并上传图片。如果在微博上显示地图,那就发送经纬度参数,多加上

lat false float 纬度,有效范围:-90.0到+90.0,+表示北纬,默认为0.0。
long false float 经度,有效范围:-180.0到+180.0,+表示东经,默认为0.0。

7、调用自定义api

6步骤里调用的是sdk里封装好的,那微博这么api和功能,怎么调用呢?

我们试着获取个人信息

  1. NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];  
  2.     [params setObject:[engine accessToken]forKey:@"access_token"];  
  3.     [params setObject:[engine userID]forKey:@"uid"];  
  4.     NSLog(@"params:%@", params);  
  5.       
  6.     [engine loadRequestWithMethodName:@"users/show.json"  
  7.                            httpMethod:@"GET"  
  8.                                params:params  
  9.                          postDataType:kWBRequestPostDataTypeNone  
  10.                      httpHeaderFields:nil];  

 

params的参数是必须的。

返回的数据参考接口http://open.weibo.com/wiki/2/users/show

这样可以获取微博自己的昵称等信息。

 

微博所有api文档都在这个页面http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2,使用的方法和例子都有。

需要什么用什么接口,把loadRequestWithMethodName 改变成自己需要的接口,params参数改成需要的参数,就可以了。

 

有的接口是不需要params的,比如

statuses/friends_timeline.json获取关注人的微博,这里params可以是nil.

PS:本篇记录用的是Oauth认证,xauth认证需要审核资格才能使用的。 

 

8、项目源码下载地址:http://download.csdn.net/detail/totogo2010/4633077

继上篇 iOS学习之iOS5.0以上 使用新浪微博开放平台OAuth

过后,新浪微博授权弹出的网页又有调整,中间还有过瘫痪情况。如果按上篇做出来的授权页面就成这样了:

第一:网页页面变大了,

第二:没有了取消按钮。

 

根据这个情况在sina weibo SDK里做了写调整

调整:增加一个关闭按钮,弹出窗口大小。

在WBAuthorizeWebView.m文件的方法:bounceOutAnimationStopped里添加按钮:

  1. UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  2.     [closeButton setFrame:CGRectMake(280, 430, 60, 60)];  
  3.     [closeButton setImageEdgeInsets:UIEdgeInsetsMake(3, 0, 0, 0)];  
  4.     [closeButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];  
  5.       
  6.     [closeButton addTarget:self action:@selector(hideAndCleanUp) forControlEvents:UIControlEventTouchUpInside];  
  7.     [self addSubview:closeButton];  


close.png图片sdk里自带就有。hideAndCleanUp方法就是把窗口移除。hideAndCleanUp方法原来就有。运行效果:

看右下角有个关闭按钮,为什么放在右下角呢,因为右上角有个注册按钮,容易被点到。一会把网页窗口最大化了就能看到了。

扩大窗口

在WBAuthorizeWebView.m文件的方法- (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation 修改如下:

上面的尺寸是横屏的时候的,我修改了竖屏时的窗口的大小。

  1. - (void)sizeToFitOrientation:(UIInterfaceOrientation)orientation  
  2. {  
  3.     [self setTransform:CGAffineTransformIdentity];  
  4.       
  5.     if (UIInterfaceOrientationIsLandscape(orientation))  
  6.     {  
  7.         [self setFrame:CGRectMake(0, 0, 480, 320)];  
  8.         [panelView setFrame:CGRectMake(10, 30, 460, 280)];  
  9.         [containerView setFrame:CGRectMake(10, 10, 440, 260)];  
  10.         [webView setFrame:CGRectMake(0, 0, 440, 260)];  
  11.         [indicatorView setCenter:CGPointMake(240, 160)];  
  12.     }  
  13.     else  
  14.     {  
  15.         [self setFrame:CGRectMake(0, 5, 320, 470)];  
  16.         [panelView setFrame:CGRectMake(0, 5, 320, 470)];  
  17.         [containerView setFrame:CGRectMake(0, 5, 320, 460)];  
  18.         [webView setFrame:CGRectMake(0, 0, 320, 460)];  
  19.         [indicatorView setCenter:CGPointMake(160, 240)];  
  20.     }  
  21.       
  22.     [self setCenter:CGPointMake(160, 240)];  
  23.       
  24.     [self setTransform:[self transformForOrientation:orientation]];  
  25.       
  26.     previousOrientation = orientation;  
  27. }  

运行效果:

这个状态差不多就可以了。

 

还有在调用WeiBoEngine 的Logout 登出无效的情况。修改如下:

在WBAuthorize.m文件,把startAuthorize函数修改如下:

  1. NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:appKey, @"client_id",  
  2.                                                                       @"code", @"response_type",  
  3.                                                                       redirectURI, @"redirect_uri",   
  4.                                                                       @"mobile", @"display",  
  5.                                                                       @"true",@"forcelogin", nil];  


就是在 params里添加@”true”,@”forcelogin”。

以上是使用新浪微博sdk开发遇到的问题和解决的一些方法。

修改过的项目代码:http://download.csdn.net/detail/totogo2010/4928029

 

来源:http://blog.csdn.net/totogo2010/article/details/8435174

原文地址:https://www.cnblogs.com/heyonggang/p/3709791.html