VK接入

作者: stYiwe | 来源:发表于2020-12-29 11:43 被阅读0次

    VK接入

    官方SDK下载和接入说明

    官方文档

    后台

    1.vk后台创建应用 -- 地址

    image-20201222165934842

    2.获取SDK,在github上下载的工程打开进行编译获取对应的framework

    image-20201222171049124

    3.将SDK加入项目中

    • 将下列代码加到info.plist

      <key>LSApplicationQueriesSchemes</key>
      <array>
          <string>vk</string>
          <string>vk-share</string>
          <string>vkauthorize</string>
      </array>
      

      添加成功后如下所示

      image-20201222171425705
    • info中,URL Types添加如下内容,其中Identifier和URL Schemes为vk+VK后台创建的应用ID

      image-20201222171848263
    • 在需要调用的地方导入#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登录没有提示,暂没做处理

    相关文章

      网友评论

          本文标题:VK接入

          本文链接:https://www.haomeiwen.com/subject/casfoktx.html