VK接入
2020-12-29 本文已影响0人
stYiwe
VK接入
1.vk后台创建应用 -- 地址
image-202012221659348422.获取SDK,在github上下载的工程打开进行编译获取对应的framework
image-202012221710491243.将SDK加入项目中
-
将下列代码加到
info.plist
中<key>LSApplicationQueriesSchemes</key> <array> <string>vk</string> <string>vk-share</string> <string>vkauthorize</string> </array>
添加成功后如下所示
image-20201222171425705 -
在
image-20201222171848263info
中,URL Types
添加如下内容,其中Identifier和URL Schemes为vk
+VK后台创建的应用ID
-
在需要调用的地方导入
#import <VKSdkFramework/VKSdkFramework.h>
#import <VKSdkFramework/VKSdkFramework.h>
-
SDK初始化
1.将代码放入应用程序委托方法里
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options { [VKSdk processOpenURL:url fromApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]]; return YES; }
注意:如果您已经添加了FaceBook SDK,并且返回了其中一种方法
[FBSDKDelegate ...]
,则可以对其进行处理-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]; [VKSdk processOpenURL:url fromApplication:sourceApplication]; return YES; }
3.在需要调用的页面
- (**void**)viewDidLoad { [super viewDidLoad]; [[VKSdk initializeWithAppId:@"3974615"] registerDelegate:self]; [[VKSdk instance] setUiDelegate:self]; SCOPE = @[VK_PER_FRIENDS, VK_PER_EMAIL]; [VKSdk wakeUpSession:SCOPE completeBlock:^(VKAuthorizationState state, NSError *error) { NSLog(@"state:%lu",(unsigned long)state); if (state == VKAuthorizationAuthorized) { NSLog(@"可以登录"); } else if (error) { NSLog(@"发生错误:%@",error.description); } }]; }
4.添加delegate
<VKSdkUIDelegate,VKSdkDelegate>
5.在点击VK登录的方法里调用如下方法
[VKSdk authorize:SCOPE];
6.实现代理方法
- (void)vkSdkAccessAuthorizationFinishedWithResult:(VKAuthorizationResult *)result { if (result.token) { NSLog(@"userid:%@",result.token.userId); NSLog(@"accessToken:%@",result.token.accessToken); //登录成功,获取到userId,执行后续操作(如登录等) } else if (result.error) {//失败 NSLog(@"error:%@",result.error.description); } } - (void)vkSdkNeedCaptchaEnter:(VKError *)captchaError { VKCaptchaViewController *vc = [VKCaptchaViewController captchaControllerWithError:captchaError]; [vc presentIn:self]; } - (void)vkSdkTokenHasExpired:(VKAccessToken *)expiredToken { [VKSdk authorize:SCOPE]; } - (void)vkSdkUserAuthorizationFailed { NSLog(@"failed"); } - (void)vkSdkShouldPresentViewController:(UIViewController *)controller { [self presentViewController:controller animated:YES completion:nil]; }
注:
如果没有装VK APP,点击VK登录没有提示,暂没做处理