commit | author | age
|
3eceb5
|
1 |
// |
W |
2 |
// VKSdk.h |
60b963
|
3 |
// |
3eceb5
|
4 |
// Copyright (c) 2014 VK.com |
W |
5 |
// |
|
6 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
7 |
// this software and associated documentation files (the "Software"), to deal in |
|
8 |
// the Software without restriction, including without limitation the rights to |
|
9 |
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
10 |
// the Software, and to permit persons to whom the Software is furnished to do so, |
|
11 |
// subject to the following conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be included in all |
|
14 |
// copies or substantial portions of the Software. |
|
15 |
// |
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
18 |
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
19 |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
20 |
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
21 |
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
22 |
// |
|
23 |
// -------------------------------------------------------------------------------- |
|
24 |
// |
|
25 |
|
|
26 |
#import <Foundation/Foundation.h> |
|
27 |
#import "VKAccessToken.h" |
|
28 |
#import "VKPermissions.h" |
|
29 |
#import "VKUtil.h" |
|
30 |
#import "VKApi.h" |
|
31 |
#import "VKApiConst.h" |
|
32 |
#import "VKSdkVersion.h" |
|
33 |
#import "VKCaptchaViewController.h" |
|
34 |
#import "VKRequest.h" |
|
35 |
#import "VKBatchRequest.h" |
|
36 |
#import "NSError+VKError.h" |
|
37 |
#import "VKApiModels.h" |
|
38 |
#import "VKUploadImage.h" |
|
39 |
#import "VKShareDialogController.h" |
|
40 |
#import "VKActivity.h" |
|
41 |
#import "VKAuthorizationResult.h" |
|
42 |
|
|
43 |
/** |
|
44 |
Options used for authorization. |
|
45 |
*/ |
|
46 |
typedef NS_OPTIONS(NSUInteger, VKAuthorizationOptions) { |
|
47 |
///This option is passed by default. You will have unlimited in time access to user data |
|
48 |
VKAuthorizationOptionsUnlimitedToken = 1 << 0, |
|
49 |
///Pass this option to disable usage of SFSafariViewController |
|
50 |
VKAuthorizationOptionsDisableSafariController = 1 << 1, |
|
51 |
}; |
|
52 |
|
|
53 |
/** |
|
54 |
SDK events delegate protocol. |
|
55 |
|
|
56 |
This protocol may be implemented by any count of objects, but don't forget unregistering deallocated delegates. |
|
57 |
*/ |
|
58 |
@protocol VKSdkDelegate <NSObject> |
|
59 |
@required |
|
60 |
|
|
61 |
/** |
|
62 |
Notifies about authorization was completed, and returns authorization result with new token or error. |
|
63 |
|
|
64 |
@param result contains new token or error, retrieved after VK authorization. |
|
65 |
*/ |
|
66 |
- (void)vkSdkAccessAuthorizationFinishedWithResult:(VKAuthorizationResult *)result; |
|
67 |
|
|
68 |
/** |
|
69 |
Notifies about access error. For example, this may occurs when user rejected app permissions through VK.com |
|
70 |
*/ |
|
71 |
- (void)vkSdkUserAuthorizationFailed; |
|
72 |
|
|
73 |
@optional |
|
74 |
|
|
75 |
/** |
|
76 |
Notifies about authorization state was changed, and returns authorization result with new token or error. |
|
77 |
|
|
78 |
If authorization was successfull, also contains user info. |
|
79 |
|
|
80 |
@param result contains new token or error, retrieved after VK authorization |
|
81 |
*/ |
|
82 |
- (void)vkSdkAuthorizationStateUpdatedWithResult:(VKAuthorizationResult *)result; |
|
83 |
|
|
84 |
/** |
|
85 |
Notifies about access token has been changed |
|
86 |
|
|
87 |
@param newToken new token for API requests |
|
88 |
@param oldToken previous used token |
|
89 |
*/ |
|
90 |
- (void)vkSdkAccessTokenUpdated:(VKAccessToken *)newToken oldToken:(VKAccessToken *)oldToken; |
|
91 |
|
|
92 |
/** |
|
93 |
Notifies about existing token has expired (by timeout). This may occurs if you requested token without no_https scope. |
|
94 |
|
|
95 |
@param expiredToken old token that has expired. |
|
96 |
*/ |
|
97 |
- (void)vkSdkTokenHasExpired:(VKAccessToken *)expiredToken; |
|
98 |
|
|
99 |
@end |
|
100 |
|
|
101 |
/** |
|
102 |
SDK UI delegate protocol. |
|
103 |
|
|
104 |
This delegate used for managing UI events, when SDK required user action. |
|
105 |
*/ |
|
106 |
@protocol VKSdkUIDelegate <NSObject> |
|
107 |
/** |
|
108 |
Pass view controller that should be presented to user. Usually, it's an authorization window. |
|
109 |
|
|
110 |
@param controller view controller that must be shown to user |
|
111 |
*/ |
|
112 |
- (void)vkSdkShouldPresentViewController:(UIViewController *)controller; |
|
113 |
|
|
114 |
/** |
|
115 |
Calls when user must perform captcha-check. |
|
116 |
If you implementing this method by yourself, call -[VKError answerCaptcha:] method for captchaError with user entered answer. |
|
117 |
|
|
118 |
@param captchaError error returned from API. You can load captcha image from <b>captchaImg</b> property. |
|
119 |
*/ |
|
120 |
- (void)vkSdkNeedCaptchaEnter:(VKError *)captchaError; |
|
121 |
|
|
122 |
@optional |
|
123 |
/** |
|
124 |
* Called when a controller presented by SDK will be dismissed. |
|
125 |
*/ |
|
126 |
- (void)vkSdkWillDismissViewController:(UIViewController *)controller; |
|
127 |
|
|
128 |
/** |
|
129 |
* Called when a controller presented by SDK did dismiss. |
|
130 |
*/ |
|
131 |
- (void)vkSdkDidDismissViewController:(UIViewController *)controller; |
|
132 |
|
|
133 |
@end |
|
134 |
|
|
135 |
|
|
136 |
/** |
|
137 |
Entry point for using VK sdk. Should be initialized at application start. |
|
138 |
|
|
139 |
Typical scenario of using SDK is next: |
|
140 |
|
|
141 |
1) Register new standalone application on https://vk.com/editapp?act=create |
|
142 |
|
|
143 |
2) Setup your application delegate and Info.plist as described on project page: https://github.com/VKCOM/vk-ios-sdk#how-to-set-up-vk-ios-sdk |
|
144 |
|
|
145 |
3) Initialize SDK with your VK application ID. |
|
146 |
|
|
147 |
VKSdk *sdkInstance = [VKSdk initializeWithAppId:VK_APP_ID]; |
|
148 |
|
|
149 |
4) Register required SDK-delegates (VKSdkDelegate) and single UI-delegate (VKSdkUIDelegate). |
|
150 |
|
|
151 |
[sdkInstance registerDelegate:self]; |
|
152 |
[sdkInstance setUiDelegate:self]; |
|
153 |
|
|
154 |
5) Check if user already authorized. |
|
155 |
|
|
156 |
[VKSdk wakeUpSession:SCOPE completeBlock:^(VKAuthorizationState state, NSError *error) { |
|
157 |
switch (state) { |
|
158 |
case VKAuthorizationAuthorized: |
|
159 |
// User already autorized, and session is correct |
|
160 |
break; |
|
161 |
|
|
162 |
case VKAuthorizationInitialized: |
|
163 |
// User not yet authorized, proceed to next step |
|
164 |
break; |
|
165 |
|
|
166 |
default: |
|
167 |
// Probably, network error occured, try call +[VKSdk wakeUpSession:completeBlock:] later |
|
168 |
break; |
|
169 |
} |
|
170 |
}]; |
|
171 |
|
|
172 |
6) If user is not authorized, call +[VKSdk authorize:] method with required scope (permission for token you required). |
|
173 |
|
|
174 |
[VKSdk authorize:@[VK_PER_FRIENDS, VK_PER_WALL]]; |
|
175 |
|
|
176 |
7) You wait for -[VKSdkDelegate vkSdkAccessAuthorizationFinishedWithResult:] method called. |
|
177 |
|
|
178 |
- (void)vkSdkAccessAuthorizationFinishedWithResult:(VKAuthorizationResult *)result { |
|
179 |
if (result.token) { |
|
180 |
// User successfully authorized, you may start working with VK API |
|
181 |
} else if (result.error) { |
|
182 |
// User canceled authorization, or occured unresolving networking error. Reset your UI to initial state and try authorize user later |
|
183 |
} |
|
184 |
} |
|
185 |
|
|
186 |
*/ |
|
187 |
@interface VKSdk : NSObject |
|
188 |
|
|
189 |
///------------------------------- |
|
190 |
/// @name Delegate |
|
191 |
///------------------------------- |
|
192 |
|
|
193 |
/// Delegate for managing user interaction, when SDK required |
|
194 |
@property(nonatomic, readwrite, weak) id <VKSdkUIDelegate> uiDelegate; |
|
195 |
|
|
196 |
/// Returns a last app_id used for initializing the SDK |
|
197 |
@property(nonatomic, readonly, copy) NSString *currentAppId; |
|
198 |
|
|
199 |
/// API version for making requests |
|
200 |
@property(nonatomic, readonly, copy) NSString *apiVersion; |
|
201 |
|
|
202 |
///------------------------------- |
|
203 |
/// @name Initialization |
|
204 |
///------------------------------- |
|
205 |
|
|
206 |
/** |
|
207 |
Returns instance of VK sdk. You should never use that directly |
|
208 |
*/ |
|
209 |
+ (instancetype)instance; |
|
210 |
|
|
211 |
/** |
|
212 |
Returns YES if SDK was previously initialized with initializeWithAppId: method |
|
213 |
*/ |
|
214 |
+ (BOOL)initialized; |
|
215 |
|
|
216 |
/** |
|
217 |
Initialize SDK with responder for global SDK events with default api version from VK_SDK_API_VERSION |
|
218 |
|
|
219 |
@param appId your application id (if you haven't, you can create standalone application here https://vk.com/editapp?act=create ) |
|
220 |
*/ |
|
221 |
+ (instancetype)initializeWithAppId:(NSString *)appId; |
|
222 |
|
|
223 |
/** |
|
224 |
Initialize SDK with responder for global SDK events. |
|
225 |
|
|
226 |
@param appId your application id (if you haven't, you can create standalone application here https://vk.com/editapp?act=create ) |
|
227 |
@param version if you want to use latest API version, pass required version here |
|
228 |
*/ |
|
229 |
+ (instancetype)initializeWithAppId:(NSString *)appId |
|
230 |
apiVersion:(NSString *)version; |
|
231 |
|
|
232 |
/** |
|
233 |
Adds a weak object reference to an object implementing the VKSdkDelegate protocol. |
|
234 |
|
|
235 |
@param delegate your object implementing delegate protocol |
|
236 |
*/ |
|
237 |
- (void)registerDelegate:(id <VKSdkDelegate>)delegate; |
|
238 |
|
|
239 |
/** |
|
240 |
Removes an object reference SDK delegate. |
|
241 |
|
|
242 |
@param delegate your object implementing delegate protocol |
|
243 |
*/ |
|
244 |
- (void)unregisterDelegate:(id <VKSdkDelegate>)delegate; |
|
245 |
|
|
246 |
///------------------------------- |
|
247 |
/// @name Authentication in VK |
|
248 |
///------------------------------- |
|
249 |
|
|
250 |
/** |
|
251 |
Starts authorization process to retrieve unlimited token. If VKapp is available in system, it will opens and requests access from user. |
|
252 |
Otherwise SFSafariViewController or webview will be opened for access request. |
|
253 |
|
|
254 |
@param permissions array of permissions for your applications. All permissions you can |
|
255 |
*/ |
|
256 |
+ (void)authorize:(NSArray *)permissions; |
|
257 |
|
|
258 |
/** |
|
259 |
Starts authorization process. If VKapp is available in system, it will opens and requests access from user. |
|
260 |
Otherwise SFSafariViewController or webview will be opened for access request. |
|
261 |
|
|
262 |
@param permissions array of permissions for your applications. All permissions you can |
|
263 |
@param options special options |
|
264 |
*/ |
|
265 |
+ (void)authorize:(NSArray *)permissions withOptions:(VKAuthorizationOptions)options; |
|
266 |
|
|
267 |
///------------------------------- |
|
268 |
/// @name Access token methods |
|
269 |
///------------------------------- |
|
270 |
|
|
271 |
/** |
|
272 |
Returns token for API requests. |
|
273 |
|
|
274 |
@return Received access token or nil, if user not yet authorized |
|
275 |
*/ |
|
276 |
+ (VKAccessToken *)accessToken; |
|
277 |
|
|
278 |
///------------------------------- |
|
279 |
/// @name Other methods |
|
280 |
///------------------------------- |
|
281 |
|
|
282 |
/** |
|
283 |
Checks passed URL for access token. |
|
284 |
|
|
285 |
@param passedUrl url from external application |
60b963
|
286 |
@param sourceApplication source application (it is an optional param, because the check is performed not only by this parameter, but also by the URL scheme) |
3eceb5
|
287 |
@return YES if parsed successfully |
W |
288 |
*/ |
|
289 |
+ (BOOL)processOpenURL:(NSURL *)passedUrl fromApplication:(NSString *)sourceApplication; |
|
290 |
|
|
291 |
|
|
292 |
/** |
|
293 |
Checks if somebody logged in with SDK (call after wakeUpSession) |
|
294 |
*/ |
|
295 |
+ (BOOL)isLoggedIn; |
|
296 |
|
|
297 |
/** |
|
298 |
This method is trying to retrieve token from storage, and check application still permitted to use user access token |
|
299 |
*/ |
|
300 |
+ (void)wakeUpSession:(NSArray *)permissions completeBlock:(void (^)(VKAuthorizationState state, NSError *error))wakeUpBlock; |
|
301 |
|
|
302 |
/** |
|
303 |
Forces logout using OAuth (with VKAuthorizeController). Removes all cookies for *.vk.com. |
|
304 |
Has no effect for logout in VK app |
|
305 |
*/ |
|
306 |
+ (void)forceLogout; |
|
307 |
|
|
308 |
/** |
|
309 |
* Checks if there is some application, which may process authorize url |
|
310 |
*/ |
|
311 |
+ (BOOL)vkAppMayExists; |
|
312 |
|
|
313 |
/** |
|
314 |
Check existing permissions |
|
315 |
@param permissions array of permissions you want to check |
|
316 |
*/ |
|
317 |
- (BOOL)hasPermissions:(NSArray *)permissions; |
|
318 |
|
|
319 |
/** |
|
320 |
Enables or disables scheduling for requests |
|
321 |
*/ |
|
322 |
+ (void)setSchedulerEnabled:(BOOL)enabled; |
|
323 |
|
|
324 |
// Deny allocating more SDK |
|
325 |
+ (instancetype)alloc NS_UNAVAILABLE; |
|
326 |
|
|
327 |
- (instancetype)init NS_UNAVAILABLE; |
|
328 |
|
|
329 |
+ (instancetype)new NS_UNAVAILABLE; |
|
330 |
|
|
331 |
@end |
|
332 |
|
|
333 |
|
|
334 |
@interface UIViewController (VKController) |
|
335 |
|
|
336 |
- (void)vks_presentViewControllerThroughDelegate; |
|
337 |
|
|
338 |
- (void)vks_viewControllerWillDismiss; |
|
339 |
|
|
340 |
- (void)vks_viewControllerDidDismiss; |
|
341 |
|
|
342 |
@end |
|
343 |
|
|
344 |
@interface VKAccessToken (Private) |
|
345 |
|
|
346 |
- (void)notifyTokenExpired; |
|
347 |
|
b80c10
|
348 |
@end |