📜 ⬆️ ⬇️

VKontakte iOS SDK

Good evening!

It all started with the fact that a more or less convenient tool was needed to work with the VKontakte social networking API for iOS. However, Google quickly upset me with search results:

It seems to be all right, the most important thing is, but the use does not cause pleasant sensations.

Under the cut, I will tell you how the new version of the VKontakte iOS SDK works, how it all began and what came to the end.

')

Pre-project


It all started with the fact that at work we received the task to connect social networks to the application. We wanted the user to not notice any difference when interacting with different social networks (posting photos on the wall, sending a message, uploading photos, etc.).
It was decided to look for ready-made solutions that would contain several social networks like VKontakte, Odnoklassniki, Twitter and Facebook, but nothing was found. The use of ready-made solutions separately did not give the desired results, so we decided to write our bikes, having previously studied the Facebook iOS SDK, MGTwitterEngine and several other prominent libraries.

As a result, we received ASASocialServices (GitHub).
The project was easy to use and install, more attention was paid to working with Twitter and Vkontakte, it was decided not to concentrate on Facebook.

In ASASocialServices, work with three social networks (hereinafter, we will talk only about two) was carried out according to a single principle: the programmer creates a UIWebView, positions it and displays it, then starts the authorization process by the user of the application and, depending on the user’s decision, one of the three blocks is called handlers (success, error, cancel).

When viewed in context, ViewController.h looks like this:
#import <UIKit/UIKit.h> #import "ASASocialServices.h" @interface ViewController : UIViewController @property UIWebView *webView; @property ASATwitterCommunicator *tw; @end 


ViewController.m
 #import "ViewController.h" NSString *const kTWITTER_CONSUMER_KEY = @"v8146mdwpo05uEroMnhozg"; NSString *const kTWITTER_CONSUMER_SECRET = @"5AFkvjCKxqGBRId2fpSQFLClJLKtGcPGS1DzK7o"; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //  WebView        CGRect frame = [[UIScreen mainScreen] bounds]; _webView = [[UIWebView alloc] initWithFrame:frame]; [self.view addSubview:_webView]; //  TwitterCommunicator    _tw = [[ASATwitterCommunicator alloc] initWithWebView:_webView]; //         [_tw startOnCancelBlock:^{ NSLog(@"User canceled app authorization..."); } onErrorBlock:^(NSError *error) { NSLog(@"error during app authorization...%@", error); } onSuccessBlock:^(ASATwitterUserAccount *account) { NSLog(@"Account: %@", account); }]; } @end 


It was enough to replace ASATwitterCommunicator with ASAVkontakteCommunicator or ASAFacebookCommunicator in order to connect and start working with another social network.
In the last block, success, the user account of the corresponding network was transferred (access token, user ID, time at which the access token expires, etc.)

Subsequent requests on behalf of the current user could be made as follows:
 [account performTwitterMethod:kTWITTER_FOLLOWERS_LIST_URL HTTPMethod:@"GET" options:@{@"user_id" : account.twitterUserID, @"include_entities": @"true"} success:^(id response) { NSLog(@"response: %@", response); } failure:^(NSError *error) { NSLog(@"error: %@", error); }]; 


Here's what the Twitter status update looks like:
 [account performTwitterMethod:kTWITTER_STATUSES_UPDATE_URL HTTPMethod:@"POST" options:@{@"status": @"Hello from ASASocialServices Framework!"} success:^(id response) { NSLog(@"response: %@", response); } failure:^(NSError *error) { NSLog(@"error: %@", error); }]; 


Everything would be fine, but in the course of use, we understood that leaving the programmer to save the token, the subsequent download was not entirely correct because our main goal is to simplify the work with the library so much that we don’t have to think about trifles like this (the library on this stage died and its development / support had to deal with one, as well as all subsequent developments).

Minuses:


VKontakte iOS SDK v1.0


I did not want to bother with ASASocialServices anymore, so I decided that I would start writing SDK for VKontakte in my free time. Sketched on a sheet of diagram of the interaction of classes, “hung” for two days above it, eventually decided that it seemed to be the first version - it started to implement.

I love Ruby and I like Rails and, for some reason, it always and still seems that they influenced to some extent the appearance of Vkontakte iOS SDK.

The user is associated with objects such as:


Each object has a list of actions that the user can perform on his own behalf:


Here's how the above steps will look like on VKontakte iOS SDK v1.0:

Creating a photo album:
  VKUser *me = [VKUser currentUser]; [[me photoAlbums] createTitle:@", !" description:@"    "]; 

image

Join the group:
  VKUser *me = [VKUser currentUser]; [[me groups] joinGroupID:100500]; 


Set status:
  VKUser *me = [VKUser currentUser]; [[me status] setStatus:@", !"]; 


Get a list of friends who are online:
 id result = [[[VKUser currentUser] friends] online]; 


Where to begin?

Suppose that you have already added Vkontakte IOS SDK v1.0 to your project and do not know what to do next with this.

We will work with the VKConnector class, which will allow us to receive + save + use the received access token once, and at the right time he will notify us that it is necessary to update the token and call the appropriate delegate method, which may follow (or maybe not) VKConnectorProtocol.

Here's what the easiest way to connect to ASAAppDelegate.m will look like:
 // // ASAAppDelegate.m // Vkontakte iOS SDK_Project // // Created by AndrewShmig on 05/27/13. // Copyright (c) 2013 AndrewShmig. All rights reserved. // #import "ASAAppDelegate.h" #import "ASAViewController.h" #import "VKUser.h" static NSString *const kVKAppID = @"3541027"; static NSString *const kVKPermissionsArray = @"photos,friends,wall,audio,video,docs,notes,pages,status,groups,messages"; @implementation ASAAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [[VKConnector sharedInstance] setDelegate:self]; [[VKConnector sharedInstance] startWithAppID:kVKAppID permissons:[kVKPermissionsArray componentsSeparatedByString:@","]]; // Override point for customization after application launch. self.viewController = [[ASAViewController alloc] initWithNibName:@"ASAViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } - (void)VKConnector:(VKConnector *)connector willShowModalView:(KGModal *)view { NSLog(@"%s", __FUNCTION__); } - (void)VKConnector:(VKConnector *)connector willHideModalView:(KGModal *)view { NSLog(@"%s", __FUNCTION__); } - (void)VKConnector:(VKConnector *)connector accessTokenInvalidated:(VKAccessToken *)accessToken { NSLog(@"%s", __FUNCTION__); } - (void)VKConnector:(VKConnector *)connector accessTokenRenewalFailed:(VKAccessToken *)accessToken { NSLog(@"%s", __FUNCTION__); } - (void)VKConnector:(VKConnector *)connector accessTokenRenewalSucceeded:(VKAccessToken *)accessToken { NSLog(@"%s", __FUNCTION__); } - (void)VKConnector:(VKConnector *)connector connectionErrorOccured:(NSError *)error { NSLog(@"%s", __FUNCTION__); } - (void)VKConnector:(VKConnector *)connector parsingErrorOccured:(NSError *)error { NSLog(@"%s", __FUNCTION__); } @end 


After the launch, a modal window appears in front of the user (KGModal was used) to authorize the application:
image

If you have questions or you do not know why this or that method is needed (for what is responsible), then feel free to contact the documentation. The documentation is generated using AppleDoc and looks generally like this:
image

VKConnectorProtocol:
image

Xcode helps with this:
image

In conclusion


The article, it seems to me, turned out to be quite long, so I’ll stop on this, although unfortunately I didn’t mention much of what I planned (file upload, token update, error handling, etc.)

I want to note that the project is actively developing and supported. The current status of the project is “Ready”, therefore in v1.0 errors will only be corrected and minor corrections made. All global changes are transferred to v2.0.

Find the most current version at this link: GitHub ( https://github.com/AndrewShmig/Vkontakte-iOS-SDK )

Some information on Vkontakte iOS SDK v2.0 can be found here: GitHub ( https://github.com/AndrewShmig/Vkontakte-iOS-SDK-v2.0/issues?labels=Future+features&state=open )

Thank you for attention.

Source: https://habr.com/ru/post/184560/


All Articles