IOS SDK

跳转到: 导航, 搜索

目录

概述

微博官方SDK提供给开发者OAuth认证,发送微博和其他Open API URL请求,默认的发送界面,数据解析等功能。 目前支持iOS 4.0以上系统,发送界面暂不支持IPAD设备。

名词解释

参数名称 作用
Consumer_key 分配给每个第三方应用的App key。用于鉴权身份,显示来源等功能。
Consumer_secret 生成请求Request Token的Secret,与Consumer key一起分配。
OAuth_token 服务器根据App key和时间,Callback_url等哈希出的Token值,用于获取OAuth verifier。
OAuth_token_secret 与OAuth Token一起使用,用于获取OAuth verifier。
OAuth_verifier 通过OAuth页面返回的verifier,用于最终获取Access Token。
Access_token 表示用户身份的Token,用于微博API的调用。

授权模式

OAuth2.0,支持web授权和客户端授权两种模式。

整体架构

  • 整体架构图:[1]
  • 主要结构说明:
  • 1、Weibo: 微博API 接口类,对外提供Weibo API的调用,包括登录,API调用,微博分享等功能。
  • 2、AsyncWeiboRunner:微博API异步执行类,封装了回调接口,通过创建线程来调用Weibo中的接口方法。
  • 3、Utility:互联网工具类,包括接口请求GET/POST封装,BASE64等encode,decode方法。
  • 4、WeiboException:微博异常封装类,封装了微博的各个异常。

SDK使用说明

  • 1、安装Xcode。
  • 2、从open.weibo.com下载官方SDK包。
  • 3、SDK包里面包含有示例程序和SDK的源代码。打开SinaWeiBoSDK/src下面的SDK工程,将其中的src文件夹拖拽到自己的工程里面。
  • 4、在自己的工程里面增加Security.framework。SDK需要使用Security.framework将OAuth认证以后的Token放到KeyChain里面从而增加整个工程的安全性。
  • 5、到 http://open.weibo.com/ 创建新的应用,获取相应的App Key 和 App Secrect。
  • 6、SDK使用及修改必须遵守微博开发者协议,以及开源软件协议。

接口说明

Class: WBEngine


1、获取WBEngine实例 初始化WBEngine类

接口名称: (id)initWithAppKey:(NSString*)app_key withAppSecret:(NSString*)app_secret

参数名称 作用
App_key 申请应用时分配给第三方应用程序的App key。
App_secret 申请应用时分配给第三方应用程序的App secrect。

返回结果:返回WBEngine类的实例。


2、Web方式认证

  • 接口名称:(void)login
  • 返回结果:无


3、客户端方式认证

  • 使用SDK提供的默认界面发送微博
  • 接口名称: (void)logInUsingUserID:(NSString *)theUserID password:(NSString *)thePassword
参数名称 作用
TheUserID 用户登陆名称。
ThePassword 用户密码。

返回结果:无。


4、通过接口发送微博

  • 接口名称:(void)sendWeiBoWithText:(NSString *)text image:(UIImage *)image
参数名称 作用
Text 发送的微博内容。
Image 发送的微博图片。

返回结果:无。


5、自定义请求

  • 接口名称:
 (void)loadRequestWithMethodName:(NSString *)methodName
 httpMethod:(NSString *)httpMethod
 params:(NSDictionary *)params
 postDataType:(WBRequestPostDataType)postDataType
 httpHeaderFields:(NSDictionary *)httpHeaderFields  


参数名称 作用
MethodName url中除去http://api.t.sina.com.cn/ 的部分。
HttpMethod GET or POST。
Params 发送的参数集合。
PostDataType Post里面的数据类型,图片,文字。
Delegate 发送请求的代理对象,用来接收发送请求的各种回调方法。

返回结果:无。

错误类型

在使用微博SDK中发生的错误都会封装成NSError对象通过回调方法返回给使用者。错误分为两大类,一种是kWBErrorCodeInterface 它代表着Open API平台返回的错误;另一种是kWBErrorCodeInterface它代表着在使用SDK过程中出现的错误。其中第二种错误又分为4种。


1、kWBSDKErrorCodeParseError 在解析数据过程中发生的错误。

2、kWBSDKErrorCodeRequestError 在获取request token时候发生的错误。

3、kWBSDKErrorCodeAccessError在获取access token时候发生的错误。

4、kWBSDKErrorCodeAuthorizeError 在没有授权成功就发送请求时提示的错误。

实例分析

登陆

微博SDK提供了方便的接口和丰富的回调来实现OAuth认证功能。在你需要使用认证功能的类的头文件中引入微博SDK的资源头文件,加入一个WBEngine类的成员变量并将之设置为类的一个属性。


1、在你的类的实现文件中,使用App Key 和 App Secret创建WBEngine类的实例

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


2、这里有两种方式,为Web验证和客户端验证。客户端验证方式类似于OAuth 1.0时的xAuth,需要满足一些条件才可以申请。

Web验证:

  weiBoEngine logIn

客户端验证:

  weiBoEngine logInUsingUserID:userID password:password


以下的协议方法可能会被回调:

@protocol WBEngineDelegate <NSObject>

@optional

// If you try to log in with logIn or logInUsingUserID method, and
// there is already some authorization info in the Keychain,
// this method will be invoked.
// You may or may not be allowed to continue your authorization,
// which depends on the value of isUserExclusive.
- (void)engineAlreadyLoggedIn:(WBEngine *)engine;

// Log in successfully.
- (void)engineDidLogIn:(WBEngine *)engine;

// Failed to log in.
// Possible reasons are:
// 1) Either username or password is wrong;
// 2) Your app has not been authorized by Sina yet.
- (void)engine:(WBEngine *)engine didFailToLogInWithError:(NSError *)error;

// Log out successfully.
- (void)engineDidLogOut:(WBEngine *)engine;

// When you use the WBEngine's request methods,
// you may receive the following four callbacks.
- (void)engineNotAuthorized:(WBEngine *)engine;
- (void)engineAuthorizeExpired:(WBEngine *)engine;

- (void)engine:(WBEngine *)engine requestDidFailWithError:(NSError *)error;
- (void)engine:(WBEngine *)engine requestDidSucceedWithResult:(id)result;

@end


发微博

微博SDK提供了发送微博的方法和默认发送的界面,既可以自己编写发送界面调用SDK的发送方法,也可以直接调用SDK提供的发送界面进行发送。


1、你可以通过-(id)initWithAppKey:(NSString *)appKey appSecret:(NSString *)appSecret text:(NSString *)text image:(UIImage *)image方法传入发送内容和发送图片,使用微博SDK提供的默认界面进行发送。 iOS21.png


微博SDK默认的发送界面同时也提供了丰富的回调方法来告知用户目前处于的阶段。

@protocol WBSendViewDelegate <NSObject>

@optional

- (void)sendViewWillAppear:(WBSendView *)view;
- (void)sendViewDidAppear:(WBSendView *)view;
- (void)sendViewWillDisappear:(WBSendView *)view;
- (void)sendViewDidDisappear:(WBSendView *)view;

- (void)sendViewDidFinishSending:(WBSendView *)view;
- (void)sendView:(WBSendView *)view didFailWithError:(NSError *)error;

- (void)sendViewNotAuthorized:(WBSendView *)view;
- (void)sendViewAuthorizeExpired:(WBSendView *)view;

@end


2、直接调用发送微博接口进行发送。 - (void)sendWeiBoWithText:(NSString *)text image:(UIImage *)image


发送一般请求

微博SDK同时支持发送一般的open api URL请求,但具体参数需要自己传入。

- (void)loadRequestWithMethodName:(NSString *)methodName
                       httpMethod:(NSString *)httpMethod
                           params:(NSDictionary *)params
                     postDataType:(WBRequestPostDataType)postDataType
                 httpHeaderFields:(NSDictionary *)httpHeaderFields

同样微博SDK提供了多个回调方法来告知使用者当前请求的状态。

@protocol WBRequestDelegate <NSObject>

@optional

- (void)request:(WBRequest *)request didReceiveResponse:(NSURLResponse *)response;

- (void)request:(WBRequest *)request didReceiveRawData:(NSData *)data;

- (void)request:(WBRequest *)request didFailWithError:(NSError *)error;

- (void)request:(WBRequest *)request didFinishLoadingWithResult:(id)result;

@end


数据解析

微博SDK目前只支持JSON数据格式的解析。它使用开源的第三方解析库来进行相应数据的解析,最后结果是NSDictionary类型的数据。

	NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	SBJSON *jsonParser = [[SBJSON alloc]init];
	NSError *parseError = nil;
	id result = [jsonParser objectWithString:dataString error:&parseError];