Guest User

Untitled

a guest
Mar 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. @IBAction func vkAutorize(_ sender: UIButton) {
  2. VKSdk.authorize(["photos"])
  3. }
  4.  
  5. let SCOPE = ["wall", "photos", "email", "friends"] //то к чему нужно получить доступ см. VKPermissions.h
  6.  
  7. override func viewDidLoad() {
  8. //Initialize SDK with your APP_ID for any delegate.
  9. VKSdk.initialize(withAppId: VK_ID).register(self)
  10. ...
  11. }
  12.  
  13. func vkButtonPressed(){
  14. self.initWorkingBlock { (finished) -> Void in
  15. // пользователь авторизован
  16. }
  17. }
  18.  
  19. func initWorkingBlock (_ completion: ((Bool) -> Void)!){
  20.  
  21. VKSdk.wakeUpSession(SCOPE as [AnyObject], complete: { (state, error) -> Void in
  22. if (state == VKAuthorizationState.authorized) {
  23. print("Authorized and ready to go")
  24. } else if ((error) != nil) {
  25. print("Some error happend, but you may try later: (error)")
  26. } else {
  27. VKSdk.authorize(self.SCOPE as [AnyObject])
  28. }
  29. completion(true)
  30. self.vkGetUser()
  31. print("completion VKSdk.wakeUpSession")
  32. })
  33. }
  34. func vkGetUser(){
  35. if VKSdk.isLoggedIn() {
  36. let userId = VKSdk.accessToken().userId
  37. if (userId != nil) {
  38. VKApi.users().get([VK_API_FIELDS:"first_name, last_name, id, photo_100, sex, bdate, country",
  39. VK_API_USER_ID: userId]).execute(resultBlock: { (response) -> Void in
  40.  
  41. let user = response?.parsedModel.fields[0] as! VKUser
  42.  
  43. print("Пользователь ВК: (user.fields)")
  44.  
  45. }, errorBlock: { (error) -> Void in
  46. print("Error2: (error)")
  47. })
  48. }
  49. }
  50. }
Add Comment
Please, Sign In to add comment