Library for working with VK API, authorizing through VK app, using VK API methods. Supported iOS from 8.0
Prepare for Using VK SDK
To use VK SDK primarily you need to create a new Standalone VK application here. Choose a title and confirm the action via SMS and you will be redirected to the application settings page.
You will need your APP_ID to use the library. Fill in the App Bundle for iOS field.
Setup URL schema of Your Application
To authorize via VK App you need to setup a url-schema for your application, which looks like vk+APP_ID (e.g. vk1234567).
iOS 9 changes the way of applications security and way of using unsecured connections. Basically, you don't have to change anything in transport security settings. But, if you're planing to use VK API with nohttps scope, you have to change security settings that way (in your Info.plist file):
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, '8.0'
target 'YourProjectName' do
pod 'VK-ios-sdk'
end
Then import the project as module if your podfile contains use_frameworks! directive:
@import VK_ios_sdk;
Or import the main project header, if you're installing pods without use_frameworks! directive:
If you're targeting iOS 8 and upper, you can use the SDK framework target. Add VK-ios-sdk.xcodeproj as sub-project to your project. Open your project in Xcode -> Go to General tab -> Find the Embedded Binaries section -> Click Add items (plus sign) -> And select VKSdkFramework.framework from the VK-ios-sdk project lastly import the main header:
Starting from version 1.3 there are two types of delegates available: common delegate and UI delegate. You can register as much common delegates, as you need, but an UI delegate may be only one. After the SDK initialization you should register delegates separately:
You will find full description of VKSdkDelegate and VKSdkUIDelegate protocols here or here
You need to check, if there is previous session available, so call asynchronous method wakeUpSession:completeBlock:
NSArray *SCOPE = @[@"friends", @"email"];
[VKSdk wakeUpSession:SCOPE completeBlock:^(VKAuthorizationState state, NSError *error) {
if (state == VKAuthorizationAuthorized) {
// Authorized and ready to go
} else if (error) {
// Some error happened, but you may try later
}
}];
You will find full list of available SCOPE permission here
Check out the VKAuthorizationState parameter. You can get several states:
VKAuthorizationInitialized – means the SDK is ready to work, and you can authorize user with +authorize: method. Probably, an old session has expired, and we wiped it out. This is not an error.
VKAuthorizationAuthorized - means a previous session is okay, and you can continue working with user data.
VKAuthorizationError - means some error happened when we tried to check the authorization. Probably, the internet connection has a bad quality. You have to try again later.
VKAuthorizationResult contains some initial information: new access token object, basic user information, and error (if authorization failed).
Complete documentation here
API Requests
VK API Request syntax
Below we have listed some examples for several request types.
Every request can return NSError with domain equal to VKSdkErrorDomain. SDK can return networking error or internal SDK error (e.g. request was canceled). Category NSError+VKError provides vkError property that describes error event. Compare error code with the global constant VK_API_ERROR. If they are equal that means you process vkError property as API error. Otherwise you should handle an http error.
SDK can handle some errors (e.g., captcha error, validation error). Appropriate ui delegate method will be called for this purpose.
Below is an example of captcha error processing:
The result of each method returns to a corresponding completeBlock. Response array contains result of the requests in order they have been passed.
Working with Share dialog
Share dialog allows you to create a user friendly dialog for sharing text and photos from your application directly to VK. See the Share dialog usage example:
Create an instance of the dialog controller as usual
Attach some text information to a dialog. Notice that users can change this information
Attach images uploaded to VK earlier. If you want user to upload a new image use uploadImages property
Attach link at your pages
Set the dialog completion handler
Present the dialog view controller to your view controller
Working with share activity
VK SDK provides a special class to work with UIActivityViewController - VKActivity.
Pay attention to the fact, that a VK App has it own Share extension since version 2.4. Since version 2.5 it will support special URL scheme to check if Share extension is available. You should call [VKActivity vkShareExtensionEnabled] method to remove VKActivity from activities list, if a VK share extension is available.
Check the example below to understand how it works:
Prepare your share information - UIImage, NSString and NSURL. That kind of information may be shared through VK
Prepare UIActivityViewController with a new application VKActivity
Set additional properties for activityViewController
Set completion handler for activityViewController
Check if you're running iOS 8 or upper. If user is using iPad, you have to present the activity controller in a popover otherwise you'll get system error
请发表评论