Setting up SPiD

This will provide a small introduction on how to setup the SPiD SDK for your iOS application.

The all SDK interactions is centered around SPiDClient which is a singleton. All client calls should go through this class.

The SDK needs to be setup with parameters received from SPiD which are client ID and client secret and SPiD server. The SDK also need to know the app URL scheme. App URL Scheme and SPiD server are used for generating requests and redirect URLs, they can be overridden by using the properties of SPiDClient if needed. The setup should be done after the app has loaded, preferably in onCreate method. The app also needs to take Androids garbage collection into account, there is no guarantee that the singleton will persist a app suspension.

The following code is used to setup the SPiDClient.

SPiDConfiguration config = new SPiDConfigurationBuilder()
        .clientID("your-client-id")
        .clientSecret("your-client-secret")
        .appURLScheme("your-app-url-scheme")
        .serverURL("your-spidserver-url")
        .context(this)
        .build();
SPiDClient.getInstance().configure(config);

After setup of the SPiDClient the application can check if there are any access token saved in the keychain or if authorization is required. This can be done with the following code.

if (SPiDClient.getInstance().isAuthorized()) {
    // Access token was saved in keychain
} else {
    // Not logged in
}

Since there are times when there are redirects to the app this must be handled. One example is opening the app from a browser. This is done by calling the the handleIntent method of the SPiDClient as shown below.

Uri data = getIntent().getData();
if (data != null && !SPiDClient.getInstance().isAuthorized()) {
    SPiDClient.getInstance().handleIntent(data, new LoginListener());
}

As the code shows this needs a LoginListener which is a implementation of SPiDAuthorizationListener, an example is seen below.

protected class LoginListener implements SPiDAuthorizationListener {
    private LoginListener() {
        super();
    }

    @Override
    public void onComplete() {
        // Successful login
    }

    @Override
    public void onSPiDException(SPiDException exception) {
        // Handle SPiDException (server errors)
    }

    @Override
    public void onIOException(IOException exception) {
        // Handle IOException (connection problems)
    }

    @Override
    public void onException(Exception exception) {
        // Handle general Exception (fatal errors, should never happen if SPiDClient is correctly configured)
    }
}

The actual authorization can be done with either browser redirect or WebView.