on master help sign in
Added vk support
commita0a6486ce1fac6acebeb2e3f12302884bdf73316 authorilyakooo0 <ilyakooo0@gmail.com> dateSun 9 Jul 2017, 18:16 (9 years ago) parentda6d4a5
5 files changed+125−19
Melk/AppDelegate.swift+9 Melk/Info.plist+18 Melk/MainCoordinator.swift+70−1 Melk/PublicCrap.swift+10 Melk/VKPublicCrap.swift+18−18
Melk/AppDelegate.swift+9
@@ -7,6 +7,7 @@
77//
88
99import UIKit
10+import SwiftyVK
1011
1112@UIApplicationMain
1213class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -22,6 +23,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2223 mainWindow.screen = UIScreen.main
2324 mainWindow.makeKeyAndVisible()
2425 coordinator = MainCoordinator(window: mainWindow)
26+ coordinator.start()
2527
2628 NotificationCenter.default.addObserver(forName: NSNotification.Name.UIScreenDidConnect, object: nil, queue: OperationQueue.main) { (_) in
2729 self.updateScreens()
@@ -34,6 +36,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3436 // Override point for customization after application launch.
3537 return true
3638 }
39+
40+ func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
41+ let app = options[.sourceApplication] as? String
42+ VK.process(url: url, sourceApplication: app)
43+ return true
44+ }
45+
3746
3847 func applicationWillResignActive(_ application: UIApplication) {
3948 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Melk/Info.plist+18
@@ -16,8 +16,26 @@
1616 <string>APPL</string>
1717 <key>CFBundleShortVersionString</key>
1818 <string>1.0</string>
19+ <key>CFBundleURLTypes</key>
20+ <array>
21+ <dict>
22+ <key>CFBundleTypeRole</key>
23+ <string>Editor</string>
24+ <key>CFBundleURLName</key>
25+ <string>com.ilyakooo0.Melk</string>
26+ <key>CFBundleURLSchemes</key>
27+ <array>
28+ <string>vk6097091</string>
29+ </array>
30+ </dict>
31+ </array>
1932 <key>CFBundleVersion</key>
2033 <string>1</string>
34+ <key>LSApplicationQueriesSchemes</key>
35+ <array>
36+ <string>vkauthorize</string>
37+ <string>vk6097091</string>
38+ </array>
2139 <key>LSRequiresIPhoneOS</key>
2240 <true/>
2341 <key>UILaunchStoryboardName</key>
Melk/MainCoordinator.swift+70−1
@@ -7,20 +7,89 @@
77//
88
99import UIKit
10+import SwiftyVK
1011
1112class MainCoordinator {
1213 let window: UIWindow
1314 let mainVC = MainViewController()
15+ let loadingVC = UIViewController() // TODO: An actual loading VC
1416 let navC: UINavigationController!
1517 let secondaryCoord = SecondaryCoordinator()
1618 init(window: UIWindow) {
1719 self.window = window
20+ window.rootViewController = loadingVC
21+
22+
1823 navC = UINavigationController(rootViewController: mainVC)
19− window.rootViewController = navC
2024 updateScreens()
25+
26+ }
27+
28+ func start() {
29+ VK.configure(withAppId: appID, delegate: self)
30+ if VK.state == .authorized {
31+ authorized()
32+ } else {
33+ VK.logIn()
34+ }
2135 }
2236
2337 func updateScreens() {
2438 secondaryCoord.updateScreens()
2539 }
40+
41+ fileprivate func authorized() {
42+ print("didAuthorize")
43+ window.rootViewController = navC
44+ }
45+}
46+
47+extension MainCoordinator: VKDelegate {
48+ /** ---DEPRECATED. TOKEN NOW STORED IN KEYCHAIN--- Called when SwiftyVK need know where a token is located
49+ - returns: Path to save/read token or nil if should save token to UserDefaults*/
50+ func vkShouldUseTokenPath() -> String? {
51+ return nil
52+ }
53+
54+ func vkWillAuthorize() -> Set<VK.Scope> {
55+ //Called when SwiftyVK need authorization permissions.
56+ return permissions
57+ }
58+
59+ func vkDidAuthorizeWith(parameters: Dictionary<String, String>) {
60+ //Called when the user is log in.
61+ //Here you can start to send requests to the API.
62+ print("authorized")
63+ mainSync {
64+ self.window.rootViewController = self.navC
65+ }
66+ authorized()
67+ }
68+
69+ func vkAutorizationFailedWith(error: AuthError) {
70+ //Called when SwiftyVK could not authorize. To let the application know that something went wrong.
71+ print("failed auth with error \(error.errorCode): \(error.description)")
72+ mainSync {
73+ self.window.rootViewController = self.navC
74+ }
75+ }
76+
77+ func vkDidUnauthorize() {
78+ print("did unauthorize")
79+ mainSync {
80+ self.window.rootViewController = self.navC
81+ }
82+ //Called when user is log out.
83+ }
84+
85+ func vkWillPresentView() -> UIViewController {
86+ //Only for iOS!
87+ //Called when need to display a view from SwiftyVK.
88+ let authController = UIViewController()
89+ mainSync {
90+ self.window.rootViewController = authController
91+ }
92+ return authController //UIViewController that should present authorization view controller
93+ }
94+
2695}
Melk/PublicCrap.swift+10
@@ -247,3 +247,13 @@ struct DecayingAverage {
247247 value = (value * weight + num) / (weight + 1)
248248 }
249249}
250+
251+func mainSync(_ code: (() -> ())) {
252+ if Thread.isMainThread {
253+ code()
254+ } else {
255+ DispatchQueue.main.sync {
256+ code()
257+ }
258+ }
259+}
Melk/VKPublicCrap.swift+18−18
@@ -9,29 +9,29 @@
99import SwiftyVK
1010
1111let permissions: Set<VK.Scope> = [
12− .notify,
13− .friends,
14− .photos,
15− .audio,
16− .video,
17− .docs,
18− .notes,
12+// .notify,
13+// .friends,
14+// .photos,
15+// .audio,
16+// .video,
17+// .docs,
18+// .notes,
1919 .pages,
20− .status,
21− .offers,
22− .questions,
20+// .status,
21+// .offers,
22+// .questions,
2323 .wall,
2424 .groups,
25− .messages,
26− .email,
27− .notifications,
28− .stats,
29− .ads,
30− .offline,
31− .market
25+// .messages,
26+// .email,
27+// .notifications,
28+// .stats,
29+// .ads,
30+// .offline,
31+// .market,
3232]
3333let vkAPIVersion = "5.60"
34−let appID = "5529752"
34+let appID = "6097091"
3535
3636let multichatchatId = 2000000000
3737