Library for working with VK API, authorization through VK app, using VK functions. Supported iOS from 5.0
To use VK SDK primarily you need to create a new VK application here by choosing the Standalone application type. Choose a title and confirm the action via SMS and you will be redirected to the application settings page. You will require your Application ID (referenced as API_ID in the documentation). Fill in the App Bundle for iOS field.
To use authorization via VK App you need to setup a url-schema of your application.
Xcode 5: Open your application settings then select the Info tab. In the URL Types section click the plus sign. Enter vk+APP_ID (e.g. vk1234567) to the Identifier and URL Schemes fields.
Xcode 4: Open your Info.plist then add a new row URL Types. Set the URL identifier to vk+APP_ID
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like VK SDK in your projects. See the "Getting Started" guide for more information.
Podfile
platform :ios, '5.0'
pod "VK-ios-sdk"
Then import the main header.
#import <VKSdk.h>
Add the sdk/sdk.xcodeproj file to your project. In the Application settings open Build phases, then the Link Binary with Libraries section. Add libVKSdk.a there, then import the main header.
#import "VKSdk.h"
- Put this code to the application delegate method application:openURL:sourceApplication:annotation:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[VKSdk processOpenURL:url fromApplication:sourceApplication];
return YES;
}
- Initialize SDK with your APP_ID for any delegate. [VKSdk initialize:delegate andAppId:YOUR_APP_ID];
See full description of VKSdkDelegate protocol here: http://vkcom.github.io/vk-ios-sdk/Protocols/VKSdkDelegate.html
There are several methods for authorization:
[VKSdk authorize:scope];
[VKSdk authorize:scope revokeAccess:YES];
[VKSdk authorize:scope revokeAccess:YES forceOAuth:YES];
Generally, [VKSdk authorize:scope]; is enough for your needs.
When succeeded, the following method of delegate will be called:
-(void) vkSdkDidReceiveNewToken:(VKAccessToken*) newToken;
In case of error (e.g., user canceled authorization):
-(void) vkSdkUserDeniedAccess:(VKError*) authorizationError;
Below we have listed the examples for several request types.
-
Plain request.
VKRequest * audioReq = [[VKApi users] get];
-
Request with parameters.
VKRequest * audioReq = [[VKApi audio] get:@{VK_API_OWNER_ID : @"896232"}];
-
Http (not https) request (only if scope VK_PER_NOHTTPS has been passed)
VKRequest * audioReq = [[VKApi audio] get:@{VK_API_OWNER_ID : @"896232"}]; audioReq.secure = NO;
-
Request with predetermined maximum number of attempts.
VKRequest * postReq = [[VKApi wall] post:@{VK_API_MESSAGE : @"Test"}]; postReq.attempts = 10; //or infinite //postReq.attempts = 0;
It will take 10 attempts until succeeds or an API error occurs.
-
Request that calls a method of VK API (keep in mind scope value).
VKRequest * getWall = [VKRequest requestWithMethod:@"wall.get" andParameters:@{VK_API_OWNER_ID : @"-1"} andHttpMethod:@"GET"];
-
Request for uploading photos on user wall.
VKRequest * request = [VKApi uploadWallPhotoRequest:[UIImage imageNamed:@"my_photo"] parameters:[VKImageParameters pngImage] userId:0 groupId:0 ];
[audioReq executeWithResultBlock:^(VKResponse * response) {
NSLog(@"Json result: %@", response.json);
} errorBlock:^(VKError * error) {
if (error.code != VK_API_ERROR) {
[error.request repeat];
} else {
NSLog(@"VK error: %@", error.apiError);
}
}];
The VKError class contains the errorCode property. Compare its value with the global constant VK_API_ERROR. If it equals, process the vkError field that contains a description of a VK API error. Otherwise you should handle an http error in the httpError property.
Some errors (e.g., captcha error, validation error) can be proccessed by the SDK. Appropriate delegate methods will be called for this purpose. Below is an example of captcha error processing:
-(void) vkSdkNeedCaptchaEnter:(VKError*) captchaError
{
VKCaptchaViewController * vc = [VKCaptchaViewController captchaControllerWithError:captchaError];
[vc presentIn:self];
}
SDK gives a feature to execute several unrelated requests at the one call.
- Prepare requests
VKRequest * request1 = [[VKApi audio] get];
request1.completeBlock = ^(VKResponse*) { ... };
VKRequest * request2 = [[VKApi users] get:@{VK_USER_IDS : @[@(1), @(6492), @(1708231)]}];
request2.completeBlock = ^(VKResponse*) { ... };
-
Combine created requests into one.
VKBatchRequest * batch = [[VKBatchRequest alloc] initWithRequests:request1, request2, nil];
-
Load the obtained request.
[batch executeWithResultBlock:^(NSArray *responses) { NSLog(@"Responses: %@", responses); } errorBlock:^(VKError *error) { NSLog(@"Error: %@", error); }];
-
The result of each method returns to a corresponding completeBlock. The responses array contains responses of the requests in order they have been passed.