How to log out from the VK API in an iOS application?

Question:

I added a Войти через ВК button. And I want to add the ability to log out and change the account.

Tried running this request http://api.vk.com/oauth/logout . But it outputs wrong logout hash .

Used this code:

let logoutUrl = "http://api.vk.com/oauth/logout"

let request = NSMutableURLRequest(URL: NSURL(string: logoutUrl)!,
cachePolicy:.ReloadIgnoringLocalCacheData,
timeoutInterval:60.0)
let responseData = try! NSURLConnection.sendSynchronousRequest(request, returningResponse: nil)

Also tried cleaning NSDefaults, after logout:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("VKAccessUserId")
defaults.removeObjectForKey("VKAccessToken")
defaults.removeObjectForKey("VKAccessTokenDate")
defaults.synchronize()

And clear cookies:

let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies ?? [] {
    let domainName = cookie.domain
    let domainRange = domainName.rangeOfString("vk.com")

    if let range = domainRange where range.count > 0 {
        storage.deleteCookie(cookie)
    }
}

Did not help

Answer:

I found a solution. You need to call VKSdk.forceLogout()

Scroll to Top