vk-ios-sdk/example/VKSDKTestApplication/VKStartScreen.m

100 lines
4.1 KiB
Objective-C
Executable File

//
// VKStartScreen.m
//
// Copyright (c) 2014 VK.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "VKStartScreen.h"
static NSString *const TOKEN_KEY = @"my_application_access_token";
static NSString *const NEXT_CONTROLLER_SEGUE_ID = @"START_WORK";
static NSArray *SCOPE = nil;
@interface VKStartScreen () <UIAlertViewDelegate, VKSdkUIDelegate>
@end
@implementation VKStartScreen
- (void)viewDidLoad {
SCOPE = @[VK_PER_FRIENDS, VK_PER_WALL, VK_PER_AUDIO, VK_PER_PHOTOS, VK_PER_NOHTTPS, VK_PER_EMAIL, VK_PER_MESSAGES];
[super viewDidLoad];
[[VKSdk initializeWithAppId:@"3974615"] registerDelegate:self];
[[VKSdk instance] setUiDelegate:self];
[VKSdk wakeUpSession:SCOPE completeBlock:^(VKAuthorizationState state, NSError *error) {
if (state == VKAuthorizationAuthorized) {
[self startWorking];
} else if (error) {
[[[UIAlertView alloc] initWithTitle:nil message:[error description] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
}
}];
}
- (void)startWorking {
[self performSegueWithIdentifier:NEXT_CONTROLLER_SEGUE_ID sender:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)authorize:(id)sender {
[VKSdk authorize:SCOPE];
}
- (IBAction)openShareDialog:(id)sender {
VKShareDialogController *shareDialog = [VKShareDialogController new];
shareDialog.text = @"This post created created created created and made and post and delivered using #vksdk #ios";
shareDialog.uploadImages = @[ [VKUploadImage uploadImageWithImage:[UIImage imageNamed:@"apple"] andParams:[VKImageParameters jpegImageWithQuality:1.0] ] ];
[shareDialog setCompletionHandler:^(VKShareDialogController *dialog, VKShareDialogControllerResult result) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[self presentViewController:shareDialog animated:YES completion:nil];
}
- (void)vkSdkNeedCaptchaEnter:(VKError *)captchaError {
VKCaptchaViewController *vc = [VKCaptchaViewController captchaControllerWithError:captchaError];
[vc presentIn:self.navigationController.topViewController];
}
- (void)vkSdkTokenHasExpired:(VKAccessToken *)expiredToken {
[self authorize:nil];
}
- (void)vkSdkAccessAuthorizationFinishedWithResult:(VKAuthorizationResult *)result {
if (result.token) {
[self startWorking];
} else if (result.error) {
[[[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"Access denied\n%@", result.error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
}
}
- (void)vkSdkUserAuthorizationFailed {
[[[UIAlertView alloc] initWithTitle:nil message:@"Access denied" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)vkSdkShouldPresentViewController:(UIViewController *)controller {
[self.navigationController.topViewController presentViewController:controller animated:YES completion:nil];
}
@end