iOS
Steps to integrate the zenda AutoAuth on iOS using ready to use code snippets.
/**
The `ZendaAuth` class provides methods for interacting with the Zenda authentication system.
It enables users to open the Zenda application using encrypted tokens and handles authentication-related operations.
- Author: zenda <[email protected]>
- Version: 1.0
- Since: 1.0
*/
public class ZendaAuth {
/**
Opens the Zenda application using the provided encrypted token.
- Parameter token: The encrypted token.
- Throws: An error if the token is nil or invalid.
*/
public static func Open(token: String?) throws {
guard let token = token else {
throw ZendaAuthError.nilToken
}
let authData: AuthData
do {
// Decode base64 string
let json = try DecodeBase64(token: token)
// Convert json string to class object
authData = try AuthData.FromJson(json: json)
} catch {
throw ZendaAuthError.invalidToken
}
// Open Zenda
Open(authData: authData)
}
/**
Decodes a Base64 encoded string.
- Parameter token: The encoded Base64 string.
- Returns: The decoded Base64 string.
- Throws: An error if the decoding process fails.
*/
private static func DecodeBase64(token: String) throws -> String {
// Decode the base64 encoded string
guard let decodedData = Data(base64Encoded: token) else {
throw NSError()
}
// Convert the decoded data in to a string
guard let decodedString = String(data: decodedData, encoding: .utf8) else {
throw NSError()
}
return decodedString
}
/**
Opens the Zenda application using the provided authentication data.
- Parameter authData: The authentication data containing the URLs for redirection.
*/
private static func Open(authData: AuthData) {
// Validate app redirect URL
guard let appRedirectURL = URL(string: authData.getAppRedirectURL()) else {
print("Invalid redirect URI")
return
}
// Open Zenda application using custom scheme URI if Zenda applicaiton is installed
if UIApplication.shared.canOpenURL(appRedirectURL) {
UIApplication.shared.open(appRedirectURL)
return
}
// Validate app store redirect URL
guard let appStoreRedirectURL = URL(string: authData.getAppStoreRedirectURL()) else {
print("Invalid redirect URL")
return
}
// Open Zenda application in app store if the Zenda application isn't installed
if UIApplication.shared.canOpenURL(appStoreRedirectURL) {
UIApplication.shared.open(appStoreRedirectURL)
}
}
/**
Represents the authentication data required to open the Zenda application.
*/
private class AuthData {
private let appRedirectURL: String
private let appStoreRedirectURL: String
/**
Initializes an instance of `AuthData` with the provided URLs.
- Parameters:
- appRedirectURL: The URL for redirecting to the Zenda application.
- appStoreRedirectURL: The URL for redirecting to the app store to download the Zenda application.
*/
init(appRedirectURL: String, appStoreRedirectURL: String) {
self.appRedirectURL = appRedirectURL
self.appStoreRedirectURL = appStoreRedirectURL
}
/**
Parses the JSON string into an `AuthData` object.
- Parameter json: The JSON string containing the authentication data.
- Returns: An `AuthData` object parsed from the JSON string.
- Throws: An error if the JSON parsing fails.
*/
public static func FromJson(json: String) throws -> AuthData {
guard let data = json.data(using: .utf8),
let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let appRedirectURL = jsonObject["appRedirectURL"] as? String,
let appStoreRedirectURL = jsonObject["appStoreRedirectURL"] as? String else {
throw NSError()
}
return AuthData(appRedirectURL: appRedirectURL, appStoreRedirectURL: appStoreRedirectURL)
}
/**
Returns the app redirect URL.
- Returns: The app redirect URL.
*/
public func getAppRedirectURL() -> String {
return appRedirectURL
}
/**
Returns the app store redirect URL.
- Returns: The app store redirect URL.
*/
public func getAppStoreRedirectURL() -> String {
return appStoreRedirectURL
}
}
}
/**
Represents the possible errors that can occur during Zenda authentication.
*/
public enum ZendaAuthError: Error {
/// Indicates that the token parameter is nil.
case nilToken
/// Indicates that the token parameter is invalid.
case invalidToken
/// Provides a localized description for the error.
var localizedDescription: String {
switch self {
case .nilToken:
return "Token parameter is nil"
case .invalidToken:
return "Token parameter is invalid"
}
}
}
Last updated