Last active
August 29, 2019 12:40
-
-
Save agiguere/4585d76f3101508fb16f4382cdba8bd2 to your computer and use it in GitHub Desktop.
SAP Fiori for iOS SDK Code Snippet: SAPURLSession & OnboardingSessionManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SAPFoundation | |
import SAPFioriFlows | |
public extension SAPURLSession { | |
/// Unregisters all observers. | |
func unregisterAllObservers() { | |
observers.forEach( {unregister($0)} ) | |
} | |
/// Terminate an active user session from the client using the POST method | |
/// | |
/// - Parameters: | |
/// - backendURL: SCPms backend URL (BaseURL) | |
/// - completionHandler: A completion handler called after the request | |
func logout(backendURL: URL, completionHandler: @escaping (Error?) -> Void) { | |
let logoutURL = URL(string: "/mobileservices/sessions/logout", relativeTo: backendURL)! | |
var logoutRequest = URLRequest(url: logoutURL) | |
logoutRequest.httpMethod = SAPURLSession.HTTPMethod.post | |
let logoutTask = dataTask(with: logoutRequest) { data, response, error in | |
if error == nil { | |
// Delete Cache | |
URLCache.shared.removeAllCachedResponses() | |
// Delete Cookies | |
let cookieStorage = HTTPCookieStorage.shared | |
if let cookies = cookieStorage.cookies { | |
cookies.forEach({cookieStorage.deleteCookie($0)}) | |
} | |
self.invalidateAndCancel() | |
} | |
completionHandler(error) | |
} | |
logoutTask.taskDescription = "SAPcpms logout request" | |
logoutTask.resume() | |
} | |
} | |
public extension OnboardingSessionManager { | |
/// Logout from Server | |
/// | |
/// - Parameter completionHandler: a completion handler called after logout | |
func logout(_ completionHandler: @escaping (Error?) -> Void) { | |
guard let urlSession = onboardingSession?.sapURLSession, | |
let backendURL = onboardingSession?.settingsParameters?.backendURL else { | |
completionHandler(nil) | |
return | |
} | |
urlSession.logout(backendURL: backendURL) { (error) in | |
if error == nil { | |
self.removeSession(completionHandler: completionHandler) | |
} else { | |
completionHandler(error) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment