Git Product home page Git Product logo

account-sdk-ios's Introduction

SchibstedAccount iOS SDK

The SchibstedAccount iOS SDK provides you access to Schibsted identity services via the SPiD APIs. It provides the following:

  • User management (profiles, logins, signups)
  • UI based logins
  • Authenticated request and oauth management

Note: The APIs provided by the SDK do not cover the full spectrum of APIs provided by SPiD. If there is functionality missing, please look at the SPiD API reference and open up an issue for an enhancement request.

Build Status license codecov

  • Documentation: Here's the documentation to the latest tagged release (which can be different than the version that's in master).
  • Contributing: For details on getting up and running if you are thinking of contributing

Setup

The pod is available as a public cocoapod under the name SchibstedAccount

The SDK is divided in to different subspecs:

  • pod 'SchibstedAccount': this is the default and contains APIs to create a User
  • pod 'SchibstedAccount/Manager': the default installs the entire SDK. If you want to only use headless login (i.e. no UI libraries included), then just use this.

You may want to include a tracking subspec as well

The UI does some internal tracking, and allows a TrackingEventsHandler to be set in the UI's configuration. To fulfill this, you can either implement it yourself or use one which is already implemented.

Internal: There is an internal Schibsted Tracking implementation for the identity SDK availabe here and is available from source "[email protected]:CocoaPods/Specs.git, so in your pod file you may:

  • pod 'SchibstedIDTracking': Adds dependency to the new pulse SDK

Add this to your Cartfile

git "[email protected]:schibsted/account-sdk-ios.git"

Then run:

$ carthage bootstrap --platform ios

or

$ carthage update --platform ios

Internal: And, if you also want to include support for tracking events in the identity UI with the new pulse SDK, add this

git "[email protected]:spt-identity/identity-sdk-ios-tracking"

Carthage will build the frameworks you need to link in your project, so make sure you follow these steps to include all of them in your project and you should be up and running. If there is something missing -> email us.

Get some client credentials

The SDK works by giving you access to Schibsted users. And what kind of access is allowed is determined by a set of client credentials. The first thing you must do is get some credentials, which can be done through self service. Learn about environments and merchants.

NOTE: The SDK will not work across environments or merchants.

Usage

The most common use case is to get a hold of a User object. You can obtain a User object by using a visual or a headless login process. The visual login is the recommended approach, but if the need arises the headless approach is also documented, but not "officially" supported.

Set your client configuration

See above for getting credentials. But once you have them you have to set up a ClientConfiguration object:

let clientConfiguration = ClientConfiguration(
        environment: .preproduction,
        clientID: "<you-client-code>",
        clientSecret: "<your-client-secret>",
        appURLScheme: nil
    )
}

Check if you already have a user

To check if you already have a user logged in, you may use the IdentityManager:

let user = IdentityManager(clientConfiguration: clientConfiguration).currentUser
user.delegate = //...
switch user.state {
case .loggedIn:
    // Yay, carry on with app
case .loggedOut:
    // Ok, fire up the IdentityUI
}

Visual login with IdentityUI

To start the login process you can fire up an IdentityUI object, configure it and it will create a User object for you if everything goes right. It is recommended to use the IdentityUI to create a User object because it makes sure that the correct login flows supported by the Schibsted Identity backend are adhered to (ie: required fields are filled in, and terms and conditions are accepted, etc).

Logging in with the IdentityUI

import UIKit
import SchibstedAccount

class ViewController: UIViewController {

    var identityUI: IdentityUI?
    var user: User?

    @IBAction func didTapLoginButton(_ sender: UIButton) {
        let identifierType: IdentifierType = (sender.tag == 0) ? .phone : .email
        self.identityUI = IdentityUI(clientConfiguration: defaultClientConfiguration, identifierType: identifierType)
        self.identityUI?.delegate = //...
        self.identityUI?.presentIdentityProcess(from: self)
    }

    @IBAction func didTapLogoutButton(_: Any) {
        self.user?.logout()
        print("User logout done!")
    }
}

IdentityUIDelegate and UserDelegate

extension ViewController: IdentityUIDelegate {
    func didFinish(result: IdentityUIResult) {
        switch result {
        case .canceled:
            print("The user canceled the login process")
        case let .completed(user):
            self.user = user
            self.user.delegate = //...
            print("User with id \(String(describing: user.id)) is logged in")
        }
    }
}
extension ViewController: UserDelegate {
    func user(_: User, didChangeStateTo newState: UserState) {
        switch newState {
        case .loggedIn:
            print("The user logged in")
        case .loggedOut:
            print("The user logged out")
        }
    }
}

UI Customization

The SDK includes an IdentityUITheme object that allows you to customize the look and feel of the identity UI. See the docs for, hopefully, more details.

Localization

The SDK comes with the following localization support:

  1. ๐Ÿ‡ณ๐Ÿ‡ด Norwegian Bokmรฅl
  2. ๐Ÿ‡ธ๐Ÿ‡ช Swedish
  3. ๐Ÿ‡ซ๐Ÿ‡ฎ Finnish

Headless login with IdentityManager

Note: It is recommended to use the UI approach.

The IdentityManager is more bare bones and doesn't ensure any kind of Schibsted identity flows. It can create a user object and also notify you of any changes that happen to the user object. You can assign an IdentityManagerDelegate to it to handle various events that take place:

import SchibstedAccount

class ViewController: UIViewController, IdentityManagerDelegate {
    let identityManagaer = SchibstedAccount.IdentityManager(clientConfiguration: clientConfiguration)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.identityManager.delegate = self
        if self.identityManager.currentUser.state == .loggedIn {
            // User is already logged in
            return
        }

        // User not logged in
        self.login()
    }

    func userStateChanged(_ state: UserState) {
        // User has just logged in or just logged out
        if state == .LoggedIn {
            print("User with id \(String(describing: self.identityManager.currentUser.id)) is logged in")
        }
    }

    func login() {
        self.identityManager.sendCode(to: PhoneNumber(...) ...) { result in
            if case .failure(let error) = result {
                print("failed to send code", error)
            }
        }
    }

    @IBAction func validateCode(_ sender: UIButton) {
        let code = self.codeTextField.text
        identityManager.validate(oneTimeCode: code, for: PhoneNumber(...) ...) {

        }
    }
}

Building, testing, documentaion

See contributing.md.

account-sdk-ios's People

Contributors

aliak00 avatar lrossi avatar torsph avatar robertmusialek avatar labun avatar

Watchers

James Cloos avatar Aksel Dybdal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.