From 7eace6fe5d76f2df51031c78c7c1b632c7380d36 Mon Sep 17 00:00:00 2001 From: Andrey Toropchin Date: Thu, 14 Jul 2016 09:32:52 +0500 Subject: [PATCH] added support for VKontakte (aka VK.com) --- Simplicity.xcodeproj/project.pbxproj | 4 +++ Simplicity/VKontakte.swift | 42 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Simplicity/VKontakte.swift diff --git a/Simplicity.xcodeproj/project.pbxproj b/Simplicity.xcodeproj/project.pbxproj index a5cb028..f53a018 100644 --- a/Simplicity.xcodeproj/project.pbxproj +++ b/Simplicity.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + A132748C1D374C81002ED68B /* VKontakte.swift in Sources */ = {isa = PBXBuildFile; fileRef = A132748B1D374C81002ED68B /* VKontakte.swift */; }; DF74EC341CE2A8BB008F16BF /* Simplicity.h in Headers */ = {isa = PBXBuildFile; fileRef = DF74EC331CE2A8BB008F16BF /* Simplicity.h */; settings = {ATTRIBUTES = (Public, ); }; }; DF74EC3F1CE2A943008F16BF /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = DF74EC3E1CE2A943008F16BF /* LICENSE */; }; DF74EC411CE2AC2F008F16BF /* LoginProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF74EC401CE2AC2F008F16BF /* LoginProvider.swift */; }; @@ -22,6 +23,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + A132748B1D374C81002ED68B /* VKontakte.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VKontakte.swift; sourceTree = ""; }; DF74EC301CE2A8BB008F16BF /* Simplicity.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Simplicity.framework; sourceTree = BUILT_PRODUCTS_DIR; }; DF74EC331CE2A8BB008F16BF /* Simplicity.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Simplicity.h; sourceTree = ""; }; DF74EC351CE2A8BB008F16BF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -86,6 +88,7 @@ DF74EC491CE2ACF0008F16BF /* Facebook.swift */, DFB389851CECFBFC001DDCCE /* Google.swift */, DF8674F81D1A0351005C636A /* Instagram.swift */, + A132748B1D374C81002ED68B /* VKontakte.swift */, ); name = LoginProviders; sourceTree = ""; @@ -194,6 +197,7 @@ buildActionMask = 2147483647; files = ( DF74EC411CE2AC2F008F16BF /* LoginProvider.swift in Sources */, + A132748C1D374C81002ED68B /* VKontakte.swift in Sources */, DFB389861CECFBFC001DDCCE /* Google.swift in Sources */, DFB389881CED3667001DDCCE /* OAuth2.swift in Sources */, DF74EC451CE2AC54008F16BF /* Helpers.swift in Sources */, diff --git a/Simplicity/VKontakte.swift b/Simplicity/VKontakte.swift new file mode 100644 index 0000000..f0d393c --- /dev/null +++ b/Simplicity/VKontakte.swift @@ -0,0 +1,42 @@ +// +// VKontakte.swift +// Simplicity +// +// Created by Andrey Toropchin on 14.07.16. +// Copyright © 2016 Stormpath. All rights reserved. +// + +/** + Class implementing VKontakte (VK.com) implicit grant flow. + + ## Using Instagram in your app. + + To get started, you first need to [create an application](https://vk.com/dev/) with VKontakte. + After registering your app, go into your client settings page. + Set App Bundle ID for iOS to your App Bundle in Xcode -> Target -> Bundle Identifier (e.g. com.developer.applicationName) + + Finally, open up your App's Xcode project and go to the project's + info tab. Under "URL Types", add a new entry, and in the URL schemes form + field, type in `vk[CLIENT_ID_HERE]`. Then, you can initiate the login + screen by calling: + ``` + Simplicity.login(VKontakte()) { (accessToken, error) in + // Insert code here + } + ``` + */ + +public class VKontakte: OAuth2 { + + public init() { + guard let urlScheme = Helpers.registeredURLSchemes(filter: {$0.hasPrefix("vk")}).first, + range = urlScheme.rangeOfString("\\d+", options: .RegularExpressionSearch) else { + preconditionFailure("You must configure your VK URL Scheme to use VK login.") + } + let clientId = urlScheme.substringWithRange(range) + let authorizationEndpoint = NSURL(string: "https://oauth.vk.com/authorize")! + let redirectEndpoint = NSURL(string: urlScheme + "://authorize")! + + super.init(clientId: clientId, authorizationEndpoint: authorizationEndpoint, redirectEndpoint: redirectEndpoint, grantType: .Implicit) + } +}