Description
The following guide will help you transition your current application to our Swift SDK.
Migrating from the Objc SDK
When migrating from the Objc SDK to the Swift SDK, it is required to add the following code in your AppDelegate.swift before initialization to Gigya.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Migrate `UserDefaults` to Swift SDK. let gmid = UserDefaults.standard.object(forKey: "com.gigya.GigyaSDK:gmid") let hasRunBefore = UserDefaults.standard.bool(forKey: "com.gigya.GigyaSDK:hasRunBefore") if let _ = gmid, hasRunBefore == false { UserDefaults.standard.setValue(true, forKey: "com.gigya.GigyaSDK:hasRunBefore") } /* Initializing the SDK. Account schema will be set to GigyaAccount struct. */ Gigya.sharedInstance() }
Session will be cleared if upgrading without using the above snippet.
Initialization & account scheme definitions
A main difference between our Objective C and Swift SDK is that Swift supports both implicit & explicit initialization.
In addition, you now have the option to define your own account schema for a safer and more intuitive usage of the Account response.
You now reference the SDK instance as:
[Gigya initWithAPIKey:@"PUT-YOUR-APIKEY-HERE" application:application launchOptions:launchOptions]; [Gigya initWithAPIKey:@"PUT-YOUR-APIKEY-HERE" application:application launchOptions:launchOptions APIDomain:@"eu1.gigya.com"];
/* Using default domain (us1-gigya.com). */ Gigya.sharedInstance().initFor(apiKey: "YOUR-API-KEY") /* Supplying Api-Key & Api-Domain */ Gigya.sharedInstance().initFor(apiKey: "YOUR-API-KEY", apiDomain: "YOUR-API-DOMAIN")
Sending requests
Another difference is how the SDK will send request to CDC endpoints. The following is a representation of sending a simple request ("accounts.verifyLogin") in both version of the SDK:
// Step 1 - Create the request and set the parameters GSRequest *request = [GSRequest requestForMethod:@"socialize.setStatus"]; [request.parameters setObject:@"I feel great" forKey:@"status"]; // Step 2 - Send the request and handle the response [request sendWithResponseHandler:^(GSResponse *response, NSError *error) { if (!error) { // Request was successful else { // Handle error } }];
/* Setup a map of parameters. */ let params = ["status": "I feel great"] /* Sending "verifyLogin" REST api. */ let api = "socialize.setStatus"; /* Send a POST request. Will receive a general purpose Dictionary object in the success block. */ gigya.send(api: api, params: params) { (result) in switch result { case .success(let data): // Success - data is Dictionary case .failure(let error): break } }
Using Screen-Sets
The difference when using Screen-Sets is the extension of the GigyaPluginEventEnum enum which provides a more elaborate handling of the JS interface and the Account interaction.
The following example demonstrates how to open the "Default-RegistrationLogin" Screen-Set.
@interface MyViewController : UIViewController <GSPluginViewDelegate> @end @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; [Gigya showPluginDialogOver:self plugin:@"Default-RegistrationLogin" parameters:params completionHandler:nil delegate:self]; } - (void)pluginView:(GSPluginView *)pluginView firedEvent:(NSDictionary *)event { NSLog(@"Plugin event from %@ - %@", pluginView.plugin, [event objectForKey:@"eventName"]); } - (void)pluginView:(GSPluginView *)pluginView finishedLoadingPluginWithEvent:(NSDictionary *)event { NSLog(@"Finished loading plugin: %@", pluginView.plugin); } - (void)pluginView:(GSPluginView *)pluginView didFailWithError:(NSError *)error { NSLog(@"Plugin error: %@", [error localizedDescription]); } @end
/* Showing "Registration-Login" screen set in a dialog mode. Use only the onLogin case to be notified when logging in event was fired. */ gigya.showScreenSet(with: "Default-RegistrationLogin", viewController: self) { result in switch result { case .onLogin(let account): // Login success. default: break } }