单军华
2018-07-11 7b02207537d35bfa1714bf8beafc921f717d100a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//
//  WeiboSDKHeaders.h
//  WeiboSDKDemo
//
//  Created by Wade Cheng on 4/3/13.
//  Copyright (c) 2013 SINA iOS Team. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
 
#import "WBHttpRequest.h"
#import "WBHttpRequest+WeiboToken.h"
 
typedef NS_ENUM(NSInteger, WeiboSDKResponseStatusCode)
{
    WeiboSDKResponseStatusCodeSuccess               = 0,//成功
    WeiboSDKResponseStatusCodeUserCancel            = -1,//用户取消发送
    WeiboSDKResponseStatusCodeSentFail              = -2,//发送失败
    WeiboSDKResponseStatusCodeAuthDeny              = -3,//授权失败
    WeiboSDKResponseStatusCodeUserCancelInstall     = -4,//用户取消安装微博客户端
    WeiboSDKResponseStatusCodePayFail               = -5,//支付失败
    WeiboSDKResponseStatusCodeShareInSDKFailed      = -8,//分享失败 详情见response UserInfo
    WeiboSDKResponseStatusCodeUnsupport             = -99,//不支持的请求
    WeiboSDKResponseStatusCodeUnknown               = -100,
};
 
@protocol WeiboSDKDelegate;
@protocol WBHttpRequestDelegate;
@class WBBaseRequest;
@class WBBaseResponse;
@class WBMessageObject;
@class WBImageObject;
@class WBBaseMediaObject;
@class WBHttpRequest;
 
/**
 微博SDK接口类
 */
@interface WeiboSDK : NSObject
 
/**
 检查用户是否安装了微博客户端程序
 @return 已安装返回YES,未安装返回NO
 */
+ (BOOL)isWeiboAppInstalled;
 
/**
 检查用户是否可以通过微博客户端进行分享
 @return 可以使用返回YES,不可以使用返回NO
 */
+ (BOOL)isCanShareInWeiboAPP;
 
/**
 检查用户是否可以使用微博客户端进行SSO授权
 @return 可以使用返回YES,不可以使用返回NO
 */
+ (BOOL)isCanSSOInWeiboApp;
 
/**
 打开微博客户端程序
 @return 成功打开返回YES,失败返回NO
 */
+ (BOOL)openWeiboApp;
 
 
/**
 获取微博客户端程序的itunes安装地址
 @return 微博客户端程序的itunes安装地址
 */
+ (NSString *)getWeiboAppInstallUrl;
 
/**
 获取当前微博SDK的版本号
 @return 当前微博SDK的版本号
 */
+ (NSString *)getSDKVersion;
 
 
extern NSString * const WeiboSDKGetAidSucessNotification;
extern NSString * const WeiboSDKGetAidFailNotification;
/**
 获取当前微博SDK的aid
 返回的aid值可能为 nil ,当值为 nil 时会尝试获取 aid 值。
 当获取成功( aid 值变为有效值)时,SDK会发出名为 WeiboSDKGetAidSucessNotification 的通知,通知中带有 aid 值。
 当获取失败时,SDK会发出名为 WeiboSDKGetAidFailNotification 的通知,通知中带有 NSError 对象。
 @return aid 用于广告的与设备信息相关的标识符
 */
+ (NSString *)getWeiboAid;
 
 
/**
 向微博客户端程序注册第三方应用
 @param appKey 微博开放平台第三方应用appKey
 @return 注册成功返回YES,失败返回NO
 */
+ (BOOL)registerApp:(NSString *)appKey;
 
/**
 处理微博客户端程序通过URL启动第三方应用时传递的数据
 
 需要在 application:openURL:sourceApplication:annotation:或者application:handleOpenURL中调用
 @param url 启动第三方应用的URL
 @param delegate WeiboSDKDelegate对象,用于接收微博触发的消息
 @see WeiboSDKDelegate
 */
+ (BOOL)handleOpenURL:(NSURL *)url delegate:(id<WeiboSDKDelegate>)delegate;
 
/**
 发送请求给微博客户端程序,并切换到微博
 
 请求发送给微博客户端程序之后,微博客户端程序会进行相关的处理,处理完成之后一定会调用 [WeiboSDKDelegate didReceiveWeiboResponse:] 方法将处理结果返回给第三方应用
 
 @param request 具体的发送请求
 
 @see [WeiboSDKDelegate didReceiveWeiboResponse:]
 @see WBBaseResponse
 */
+ (BOOL)sendRequest:(WBBaseRequest *)request;
 
/**
 收到微博客户端程序的请求后,发送对应的应答给微博客户端端程序,并切换到微博
 
 第三方应用收到微博的请求后,异步处理该请求,完成后必须调用该函数将应答返回给微博
 
 @param response 具体的应答内容
 @see WBBaseRequest
 */
+ (BOOL)sendResponse:(WBBaseResponse *)response;
 
/**
 设置WeiboSDK的调试模式
 
 当开启调试模式时,WeiboSDK会在控制台输出详细的日志信息,开发者可以据此调试自己的程序。默认为 NO
 @param enabled 开启或关闭WeiboSDK的调试模式
 */
+ (void)enableDebugMode:(BOOL)enabled;
 
/**
 取消授权,登出接口
 调用此接口后,token将失效
 @param token 第三方应用之前申请的Token
 @param delegate WBHttpRequestDelegate对象,用于接收微博SDK对于发起的接口请求的请求的响应
 @param tag 用户自定义TAG,将通过回调WBHttpRequest实例的tag属性返回
 
 */
+ (void)logOutWithToken:(NSString *)token delegate:(id<WBHttpRequestDelegate>)delegate withTag:(NSString*)tag;
 
/**
 呼起微博客户端或打开微博H5页面,SDK自动检测是否安装微博客户端,当调用SDK相关方法时:
 
 有的话呼起微博客户端定位到对应界面;
 没有的话打开 webView 加载相应的微博H5页面;
 @param uid 用户id
 @param mid 微博id
 @param aid 文章id
*/
 
//连接到指定用户的微博个人主页,连接后可进行加关注等互动
+ (void)linkToUser:(NSString *)uid;
 
//连接到指定的单条微博详情页,连接后可对这条微博进行转、评、赞等互动
+ (void)linkToSingleBlog:(NSString *)uid blogID:(NSString *)mid;
 
//连接到指定的微博头条文章页
+ (void)linkToArticle:(NSString *)aid;
 
//分享到微博
+ (void)shareToWeibo:(NSString *)content;
 
//评论指定的微博
+ (void)commentToWeibo:(NSString *)mid;
 
//连接到微博搜索内容流
+ (void)linkToSearch:(NSString *)keyword;
 
//连接到我的微博消息流
+ (void)linkToTimeLine;
 
//连接到我的微博个人主页
+ (void)linkToProfile;
 
@end
 
/**
 接收并处理来至微博客户端程序的事件消息
 */
@protocol WeiboSDKDelegate <NSObject>
 
/**
 收到一个来自微博客户端程序的请求
 
 收到微博的请求后,第三方应用应该按照请求类型进行处理,处理完后必须通过 [WeiboSDK sendResponse:] 将结果回传给微博
 @param request 具体的请求对象
 */
- (void)didReceiveWeiboRequest:(WBBaseRequest *)request;
 
/**
 收到一个来自微博客户端程序的响应
 
 收到微博的响应后,第三方应用可以通过响应类型、响应的数据和 WBBaseResponse.userInfo 中的数据完成自己的功能
 @param response 具体的响应对象
 */
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response;
 
@end
 
 
#pragma mark - DataTransferObject and Base Request/Response
 
/**
 微博客户端程序和第三方应用之间传输数据信息的基类
 */
@interface WBDataTransferObject : NSObject
 
/**
 自定义信息字典,用于数据传输过程中存储相关的上下文环境数据
 
 第三方应用给微博客户端程序发送 request 时,可以在 userInfo 中存储请求相关的信息。
 
 @warning userInfo中的数据必须是实现了 `NSCoding` 协议的对象,必须保证能序列化和反序列化
 @warning 序列化后的数据不能大于10M
 */
@property (nonatomic, strong) NSDictionary *userInfo;
 
 
/**
 发送该数据对象的SDK版本号
 
 如果数据对象是自己生成的,则sdkVersion为当前SDK的版本号;如果是接收到的数据对象,则sdkVersion为数据发送方SDK版本号
 */
@property (strong, nonatomic, readonly) NSString *sdkVersion;
 
 
/**
 当用户没有安装微博客户端程序时是否提示用户打开微博安装页面
 
 如果设置为YES,当用户未安装微博时会弹出Alert询问用户是否要打开微博App的安装页面。默认为YES
 */
@property (nonatomic, assign) BOOL shouldOpenWeiboAppInstallPageIfNotInstalled;
 
 
@end
 
 
/**
 微博SDK所有请求类的基类
 */
@interface WBBaseRequest : WBDataTransferObject
 
/**
 返回一个 WBBaseRequest 对象
 
 @return 返回一个*自动释放的*WBBaseRequest对象
 */
+ (id)request;
 
@end
 
 
/**
 微博SDK所有响应类的基类
 */
@interface WBBaseResponse : WBDataTransferObject
 
/**
 对应的 request 中的自定义信息字典
 
 如果当前 response 是由微博客户端响应给第三方应用的,则 requestUserInfo 中会包含原 request.userInfo 中的所有数据
 
 @see WBBaseRequest.userInfo
 */
@property (strong, nonatomic, readonly) NSDictionary *requestUserInfo;
 
/**
 响应状态码
 
 第三方应用可以通过statusCode判断请求的处理结果
 */
@property (nonatomic, assign) WeiboSDKResponseStatusCode statusCode;
 
/**
 返回一个 WBBaseResponse 对象
 
 @return 返回一个*自动释放的*WBBaseResponse对象
 */
+ (id)response;
 
@end
 
#pragma mark - Authorize Request/Response
 
/**
 第三方应用向微博客户端请求认证的消息结构
 
 第三方应用向微博客户端申请认证时,需要调用 [WeiboSDK sendRequest:] 函数, 向微博客户端发送一个 WBAuthorizeRequest 的消息结构。
 微博客户端处理完后会向第三方应用发送一个结构为 WBAuthorizeResponse 的处理结果。
 */
@interface WBAuthorizeRequest : WBBaseRequest
 
/**
 微博开放平台第三方应用授权回调页地址,默认为`http://`
 
 参考 http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E#.E5.AE.A2.E6.88.B7.E7.AB.AF.E9.BB.98.E8.AE.A4.E5.9B.9E.E8.B0.83.E9.A1.B5
 
 @warning 必须保证和在微博开放平台应用管理界面配置的“授权回调页”地址一致,如未进行配置则默认为`http://`
 @warning 不能为空,长度小于1K
 */
@property (nonatomic, strong) NSString *redirectURI;
 
/**
 微博开放平台第三方应用scope,多个scrope用逗号分隔
 
 参考 http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E#scope
 
 @warning 长度小于1K
 */
@property (nonatomic, strong) NSString *scope;
 
/**
 当用户没有安装微博客户端或微博客户端过低无法支持SSO的时候是否弹出SDK自带的Webview进行授权
 
 如果设置为YES,当用户没有安装微博客户端或微博客户端过低无法支持SSO的时候会自动弹出SDK自带的Webview进行授权。
 
 如果设置为NO,会根据 shouldOpenWeiboAppInstallPageIfNotInstalled 属性判断是否弹出安装/更新微博的对话框
 
 默认为YES
 */
@property (nonatomic, assign) BOOL shouldShowWebViewForAuthIfCannotSSO;
 
@end
 
 
/**
 微博客户端处理完第三方应用的认证申请后向第三方应用回送的处理结果
 
 WBAuthorizeResponse 结构中仅包含常用的 userID 、accessToken 和 expirationDate 信息,其他的认证信息(比如部分应用可以获取的 refresh_token 信息)会统一存放到 userInfo 中
 */
@interface WBAuthorizeResponse : WBBaseResponse
 
/**
 用户ID
 */
@property (nonatomic, strong) NSString *userID;
 
/**
 认证口令
 */
@property (nonatomic, strong) NSString *accessToken;
 
/**
 认证过期时间
 */
@property (nonatomic, strong) NSDate *expirationDate;
 
/**
 当认证口令过期时用于换取认证口令的更新口令
 */
@property (nonatomic, strong) NSString *refreshToken;
 
@end
 
#pragma mark - ProvideMessageForWeibo Request/Response
 
/**
 微博客户端向第三方程序请求提供内容的消息结构
 */
@interface WBProvideMessageForWeiboRequest : WBBaseRequest
 
@end
 
/**
 微博客户端向第三方应用请求提供内容,第三方应用向微博客户端返回的消息结构
 */
@interface WBProvideMessageForWeiboResponse : WBBaseResponse
 
/**
 提供给微博客户端的消息
 */
@property (nonatomic, strong) WBMessageObject *message;
 
/**
 返回一个 WBProvideMessageForWeiboResponse 对象
 @param message 需要回送给微博客户端程序的消息对象
 @return 返回一个*自动释放的*WBProvideMessageForWeiboResponse对象
 */
+ (id)responseWithMessage:(WBMessageObject *)message;
 
@end
 
#pragma mark - SendMessageToWeibo Request/Response
 
/**
 第三方应用发送消息至微博客户端程序的消息结构体
 */
@interface WBSendMessageToWeiboRequest : WBBaseRequest
 
/**
 发送给微博客户端的消息
 */
@property (nonatomic, strong) WBMessageObject *message;
 
/**
 返回一个 WBSendMessageToWeiboRequest 对象
 此方法生成对象被[WeiboSDK sendRequest:]会唤起微博客户端的发布器进行分享,如果未安装微博客户端或客户端版本太低
 会根据 shouldOpenWeiboAppInstallPageIfNotInstalled 属性判断是否弹出安装/更新微博的对话框
 @param message 需要发送给微博客户端的消息对象
 @return 返回一个*自动释放的*WBSendMessageToWeiboRequest对象
 */
+ (id)requestWithMessage:(WBMessageObject *)message;
 
/**
 返回一个 WBSendMessageToWeiboRequest 对象
 
 当用户安装了可以支持微博客户端內分享的微博客户端时,会自动唤起微博并分享
 当用户没有安装微博客户端或微博客户端过低无法支持通过客户端內分享的时候会自动唤起SDK內微博发布器
 
 @param message 需要发送给微博的消息对象
 @param authRequest 授权相关信息,与access_token二者至少有一个不为空,当access_token为空并且需要弹出SDK內发布器时会通过此信息先进行授权后再分享
 @param access_token 第三方应用之前申请的Token,当此值不为空并且无法通过客户端分享的时候,会使用此token进行分享。
 @return 返回一个*自动释放的*WBSendMessageToWeiboRequest对象
 */
+ (id)requestWithMessage:(WBMessageObject *)message
                authInfo:(WBAuthorizeRequest *)authRequest
            access_token:(NSString *)access_token;
 
@end
 
/**
 WBSendMessageToWeiboResponse
 */
@interface WBSendMessageToWeiboResponse : WBBaseResponse
 
/**
 可能在分享过程中用户进行了授权操作,当此值不为空时,为用户相应授权信息
 */
@property (nonatomic,strong) WBAuthorizeResponse *authResponse;
@end
 
 
 
#pragma mark - MessageObject / ImageObject
 
/**
 微博客户端程序和第三方应用之间传递的消息结构
 
 一个消息结构由三部分组成:文字、图片和多媒体数据。三部分内容中至少有一项不为空,图片和多媒体数据不能共存。
 */
@interface WBMessageObject : NSObject
 
/**
 消息的文本内容
 
 @warning 长度小于2000个汉字
 */
@property (nonatomic, strong) NSString *text;
 
/**
 消息的图片内容
 
 @see WBImageObject
 */
@property (nonatomic, strong) WBImageObject *imageObject;
 
/**
 消息的多媒体内容
 
 @see WBBaseMediaObject
 */
@property (nonatomic, strong) WBBaseMediaObject *mediaObject;
 
/**
 返回一个 WBMessageObject 对象
 
 @return 返回一个*自动释放的*WBMessageObject对象
 */
+ (id)message;
 
@end
 
/**
 消息中包含的图片数据对象
 */
@interface WBImageObject : NSObject
 
/**
 图片真实数据内容
 
 @warning 大小不能超过10M
 */
@property (nonatomic, strong) NSData *imageData;
 
/**
 返回一个 WBImageObject 对象
 
 @return 返回一个*自动释放的*WBImageObject对象
 */
+ (id)object;
 
/**
 返回一个 UIImage 对象
 
 @return 返回一个*自动释放的*UIImage对象
 */
- (UIImage *)image;
 
@end
 
#pragma mark - Message Media Objects
 
/**
 消息中包含的多媒体数据对象基类,该类后期会被废弃,在发布器不再显示为linkcard样式,只显示为普通网络连接
 */
@interface WBBaseMediaObject : NSObject
 
/**
 对象唯一ID,用于唯一标识一个多媒体内容
 
 当第三方应用分享多媒体内容到微博时,应该将此参数设置为被分享的内容在自己的系统中的唯一标识
 @warning 不能为空,长度小于255
 */
@property (nonatomic, strong) NSString *objectID;
 
/**
 多媒体内容标题
 @warning 不能为空且长度小于1k
 */
@property (nonatomic, strong) NSString *title;
 
/**
 多媒体内容描述
 @warning 长度小于1k
 */
@property (nonatomic, strong) NSString *description;
 
/**
 多媒体内容缩略图
 @warning 大小小于32k
 */
@property (nonatomic, strong) NSData *thumbnailData;
 
/**
 点击多媒体内容之后呼起第三方应用特定页面的scheme
 @warning 长度小于255
 */
@property (nonatomic, strong) NSString *scheme;
 
/**
 返回一个 WBBaseMediaObject 对象
 
 @return 返回一个*自动释放的*WBBaseMediaObject对象
 */
+ (id)object;
 
@end
 
#pragma mark - Message WebPage Objects
 
/**
 消息中包含的网页数据对象
 */
@interface WBWebpageObject : WBBaseMediaObject
 
/**
 网页的url地址
 
 @warning 不能为空且长度不能超过255
 */
@property (nonatomic, strong) NSString *webpageUrl;
 
@end