[VKSdk initialize: delegate andAppId: APP_ID];
[VKSdk authorize: scope];
- (void) vkSdkDidReceiveNewToken: (VKAccessToken *) newToken;
- (void) vkSdkUserDeniedAccess: (VKError *) authorizationError;
#import <UIKit/UIKit.h> @interface VKontakteActivity : UIActivity - (id)initWithParent:(UIViewController*)parent; @end
- (NSString *)activityType { return @"VKActivityTypeVKontakte"; } - (NSString *)activityTitle { return @""; } - (UIImage *)activityImage { return [UIImage imageNamed:@"vk_activity"]; }
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems { for (UIActivityItemProvider *item in activityItems) { if ([item isKindOfClass:[UIImage class]]) { return YES; } else if ([item isKindOfClass:[NSString class]]) { return YES; } } return NO; }
- (void)prepareWithActivityItems:(NSArray *)activityItems { for (id item in activityItems) { if ([item isKindOfClass:[NSString class]]) { self.string = item; } else if([item isKindOfClass:[UIImage class]]) { self.image = item; } else if([item isKindOfClass:[NSURL class]]) { self.URL = item; } } }
- (void)performActivity{ [VKSdk initializeWithDelegate:self andAppId:@"3974615"]; if ([VKSdk wakeUpSession]) { [self postToWall]; } else{ [VKSdk authorize:@[VK_PER_WALL, VK_PER_PHOTOS]]; } }
-(void)postToWall{ [self begin]; if (self.image) { [self uploadPhoto]; } else{ [self uploadText]; } } // -(void)uploadText{ [self postParameters:@{ VK_API_FRIENDS_ONLY : @(0), VK_API_OWNER_ID : [VKSdk getAccessToken].userId, VK_API_MESSAGE : self.string}]; } // -(void)postParameters:(NSDictionary *)params{ VKRequest *post = [[VKApi wall] post:params]; [post executeWithResultBlock: ^(VKResponse *response) { NSNumber * postId = response.json[@"post_id"]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://vk.com/wall%@_%@", [VKSdk getAccessToken].userId, postId]]]; [self end]; } errorBlock: ^(NSError *error) { NSLog(@"Error: %@", error); [self end]; }]; }
- (void)uploadPhoto { NSString *userId = [VKSdk getAccessToken].userId; // VKRequest *request = [VKApi uploadWallPhotoRequest:self.image parameters:[VKImageParameters jpegImageWithQuality:1.f] userId:[userId integerValue] groupId:0]; [request executeWithResultBlock: ^(VKResponse *response) { VKPhoto *photoInfo = [(VKPhotoArray*)response.parsedModel objectAtIndex:0]; NSString *photoAttachment = [NSString stringWithFormat:@"photo%@_%@", photoInfo.owner_id, photoInfo.id]; // [self postParameters:@{ VK_API_ATTACHMENTS : photoAttachment, VK_API_FRIENDS_ONLY : @(0), VK_API_OWNER_ID : userId, VK_API_MESSAGE : [NSString stringWithFormat:@"%@ %@",self.string, [self.URL absoluteString]]}]; } errorBlock: ^(NSError *error) { NSLog(@"Error: %@", error); [self end]; }]; }
NSArray *items = @[[UIImage imageNamed:@"example.jpg"], @"Example" , [NSURL URLWithString:@"https://www.youtube.com/watch?v=S59fDUZIuKY"]]; VKontakteActivity *vkontakteActivity = [[VKontakteActivity alloc] initWithParent:self]; UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:@[vkontakteActivity]]; [self presentViewController:activityViewController animated:YES completion:nil];
Source: https://habr.com/ru/post/214637/
All Articles