-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZYWeiBoManager.m
205 lines (160 loc) · 5.89 KB
/
ZYWeiBoManager.m
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
//
// ZYWeiBoManager.m
// ABiteOfKunMing
//
// Created by zCloud on 14-7-20.
// Copyright (c) 2014年 Yun.Zou. All rights reserved.
//
#import "ZYWeiBoManager.h"
#define APPKEY @"801515859"
#define APPSECRECT @"34ec44459431ebe4020974cb655f87b3"
#define REDIRECTURI @"http://www.baidu.com"
@interface ZYWeiBoManager () <WeiboRequestDelegate,WeiboAuthDelegate>
@property (strong, nonatomic) WeiboApi *wbapi;
@end
@implementation ZYWeiBoManager
- (WeiboApi *)wbapi
{
if (!_wbapi) {
_wbapi = [[WeiboApi alloc]initWithAppKey:APPKEY
andSecret:APPSECRECT
andRedirectUri:REDIRECTURI
andAuthModeFlag:TCWBModelDefault
andCachePolicy:TCWBCachePolicyCacheFirst];
}
return _wbapi;
}
#pragma mark - Public methods
-(BOOL)handleOpenURL:(NSURL *)url
{
return [self.wbapi handleOpenURL:url];
}
-(void)checkAuthValid
{
[self.wbapi checkAuthValid:TCWBAuthCheckServer andDelegete:self];
}
-(void)loginWithRootViewController:(UIViewController *)rootCtrl
{
[self.wbapi loginWithDelegate:self andRootController:rootCtrl];
}
-(void)logout
{
[self.wbapi cancelAuth];
}
#pragma mark - WeiboAuthDelegate
/**
* @brief 重刷授权成功后的回调
* @param INPUT wbapi 成功后返回的WeiboApi对象,accesstoken,openid,refreshtoken,expires 等授权信息都在此处返回
* @return 无返回
*/
- (void)DidAuthRefreshed:(WeiboApiObject *)wbobj
{
//UISwitch
NSString *str = [[NSString alloc]initWithFormat:@"accesstoken = %@\r openid = %@\r appkey=%@ \r appsecret=%@\r",wbobj.accessToken, wbobj.openid, wbobj.appKey, wbobj.appSecret];
NSLog(@"result = %@",str);
//注意回到主线程,有些回调并不在主线程中,所以这里必须回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
}
/**
* @brief 重刷授权失败后的回调
* @param INPUT error 标准出错信息
* @return 无返回
*/
- (void)DidAuthRefreshFail:(NSError *)error
{
NSString *str = [[NSString alloc] initWithFormat:@"refresh token error, errcode = %@",error.userInfo];
NSLog(@"%@",str);
//注意回到主线程,有些回调并不在主线程中,所以这里必须回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
}
/**
* @brief 授权成功后的回调
* @param INPUT wbapi 成功后返回的WeiboApi对象,accesstoken,openid,refreshtoken,expires 等授权信息都在此处返回
* @return 无返回
*/
- (void)DidAuthFinished:(WeiboApiObject *)wbobj
{
//NSString *str = [[NSString alloc]initWithFormat:@"accesstoken = %@\r\n openid = %@\r\n appkey=%@ \r\n appsecret=%@ \r\n refreshtoken=%@ ", wbobj.accessToken, wbobj.openid, wbobj.appKey, wbobj.appSecret, wbobj.refreshToken];
//NSLog(@"result = %@",str);
//注意回到主线程,有些回调并不在主线程中,所以这里必须回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
_accessToken = wbobj.accessToken;
_openId = wbobj.openid;
_appKey = wbobj.appKey;
_appSecrect = wbobj.appSecret;
_isAccessTokenValid = YES;
[self.delegate isAuthValid];
});
}
/**
* @brief 授权成功后的回调
* @param INPUT wbapi weiboapi 对象,取消授权后,授权信息会被清空
* @return 无返回
*/
- (void)DidAuthCanceled:(WeiboApi *)wbapi_
{
}
/**
* @brief 授权成功后的回调
* @param INPUT error 标准出错信息
* @return 无返回
*/
- (void)DidAuthFailWithError:(NSError *)error
{
NSString *str = [[NSString alloc] initWithFormat:@"get token error, errcode = %@",error.userInfo];
NSLog(@"%@",str);
//注意回到主线程,有些回调并不在主线程中,所以这里必须回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
}
/**
* @brief 授权成功后的回调
* @param INPUT error 标准出错信息
* @return 无返回
*/
-(void)didCheckAuthValid:(BOOL)bResult suggest:(NSString *)strSuggestion
{
NSString *str = [[NSString alloc] initWithFormat:@"ret=%d, suggestion = %@", bResult, strSuggestion];
NSLog(@"%@",str);
//注意回到主线程,有些回调并不在主线程中,所以这里必须回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
_isAccessTokenValid = bResult;
_accessToken = [self.wbapi getToken].accessToken;
_openId = [self.wbapi getToken].openid;
_appKey = [self.wbapi getToken].appKey;
_appSecrect = [self.wbapi getToken].appSecret;
[self.delegate isAuthValid];
});
}
#pragma mark WeiboRequestDelegate
/**
* @brief 接口调用成功后的回调
* @param INPUT data 接口返回的数据
* @param INPUT request 发起请求时的请求对象,可以用来管理异步请求
* @return 无返回
*/
- (void)didReceiveRawData:(NSData *)data reqNo:(int)reqno
{
//NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
//NSLog(@"result = %@",strResult);
//注意回到主线程,有些回调并不在主线程中,所以这里必须回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
}
/**
* @brief 接口调用失败后的回调
* @param INPUT error 接口返回的错误信息
* @param INPUT request 发起请求时的请求对象,可以用来管理异步请求
* @return 无返回
*/
- (void)didFailWithError:(NSError *)error reqNo:(int)reqno
{
NSString *str = [[NSString alloc] initWithFormat:@"refresh token error, errcode = %@",error.userInfo];
NSLog(@"%@",str);
//注意回到主线程,有些回调并不在主线程中,所以这里必须回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
});
}
@end