[GH-ISSUE #432] How to maintain session with OAuthSwift lib #280

Closed
opened 2026-03-03 16:47:21 +03:00 by kerem · 0 comments
Owner

Originally created by @Rupesh1234fbbf on GitHub (Feb 3, 2018).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/432

I am using OAuthSwift lib to call web services. We are using OAuth 1 at server side for authentication. But every time I call services it creates a new session at server side. by using Cookies Jar, my android app able to maintain session and no new session is created at server for same. But this not a case in IOS. The Code I tried is as follows

let oauthswift = OAuth1Swift(
consumerKey:"1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
consumerSecret:"5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
oauthswift.client.credential.oauthToken = "cxxxxxxxxxxxxxxxxxxxxxxxxx"
oauthswift.client.credential.oauthTokenSecret = "xxxxxxxxxxxxxxxxxxxxxxxx"

oauthswift.client.post(register_URL , parameters:requestDict, headers: ["Accept": "application/json", "Content-Type":"application/json"],success: { response in

    let dataString = response.string

    let data = dataString?.data(using: .utf8)
       do {
        //Parsing data & get the Array

    } catch let error as NSError {
        print("Failed to load: \(error.localizedDescription)")
        DispatchQueue.main.async {
            errorBlock(error.localizedDescription)
        }
    }
},
        failure: { error in
        print(error)
})

i also tried to keep the singleton pattern to keep oauthswift object same form to request but not helped. Also tried to set cookies in oAuthSwift Client as follows.

import Foundation
import OAuthSwift

class ClientHelper{

static var oauthswift:OAuth1Swift?

/* Create cookie and place in cookie storage */
static let cookieStorage = HTTPCookieStorage.shared
static func getInstance()->OAuth1Swift{


if(oauthswift == nil){
oauthswift = OAuth1Swift(
        consumerKey:"1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        consumerSecret:"5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
     )
    oauthswift?.client.credential.oauthToken = "cxxxxxxxxxxxxxxxxxxxxxxxxx"
    oauthswift?.client.credential.oauthTokenSecret = "xxxxxxxxxxxxxxxxxxxxxxxx"
    oauthswift?.client.sessionFactory.configuration.httpCookieStorage = cookieStorage
   oauthswift?.client.sessionFactory.configuration.httpShouldSetCookies = true

}
    return oauthswift!;

}

}

Originally created by @Rupesh1234fbbf on GitHub (Feb 3, 2018). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/432 I am using OAuthSwift lib to call web services. We are using OAuth 1 at server side for authentication. But every time I call services it creates a new session at server side. by using Cookies Jar, my android app able to maintain session and no new session is created at server for same. But this not a case in IOS. The Code I tried is as follows let oauthswift = OAuth1Swift( consumerKey:"1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", consumerSecret:"5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) oauthswift.client.credential.oauthToken = "cxxxxxxxxxxxxxxxxxxxxxxxxx" oauthswift.client.credential.oauthTokenSecret = "xxxxxxxxxxxxxxxxxxxxxxxx" oauthswift.client.post(register_URL , parameters:requestDict, headers: ["Accept": "application/json", "Content-Type":"application/json"],success: { response in let dataString = response.string let data = dataString?.data(using: .utf8) do { //Parsing data & get the Array } catch let error as NSError { print("Failed to load: \(error.localizedDescription)") DispatchQueue.main.async { errorBlock(error.localizedDescription) } } }, failure: { error in print(error) }) i also tried to keep the singleton pattern to keep oauthswift object same form to request but not helped. Also tried to set cookies in oAuthSwift Client as follows. import Foundation import OAuthSwift class ClientHelper{ static var oauthswift:OAuth1Swift? /* Create cookie and place in cookie storage */ static let cookieStorage = HTTPCookieStorage.shared static func getInstance()->OAuth1Swift{ if(oauthswift == nil){ oauthswift = OAuth1Swift( consumerKey:"1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", consumerSecret:"5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) oauthswift?.client.credential.oauthToken = "cxxxxxxxxxxxxxxxxxxxxxxxxx" oauthswift?.client.credential.oauthTokenSecret = "xxxxxxxxxxxxxxxxxxxxxxxx" oauthswift?.client.sessionFactory.configuration.httpCookieStorage = cookieStorage oauthswift?.client.sessionFactory.configuration.httpShouldSetCookies = true } return oauthswift!; } }
kerem closed this issue 2026-03-03 16:47:21 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#280
No description provided.