OAuth2 integration using Swift | Sequencing.com

Real-Time Personalization (+RTP) API Guide

Here we will show how to quickly integrate Sequencing OAuth2 harnesses into your Swift based environment.

Unless you have specific reason, please refer to "Master plugins" or "Sample applications" section as they provide kit for quick boot-straping mobile applications in all in one package.

 

We have developed a CocoaPod component to simplify using Swift library in your project. If you are new to CocoaPods, refer to the official guide.

There are several prerequisites in order to complete the integration:

  • you need to have a Sequencing account
  • you need to have an OAuth2 application secret

In order to complete integration follow instructions below

 

Prepare environment

1) Open existing XCode project or create new one

2) create Podfile in your project directory:

$ pod init

3) uncomment following row (as this is Swift pod)

use_frameworks!

3) specify "sequencing-oauth-api-swift" pod parameters:

$ pod 'sequencing-oauth-api-swift', '~> 2.5.0'

4) install the dependency in your project using terminal

$ pod install

5) Use Xcode workspace instead of the project file

$ open *.xcworkspace

 

Configure authorization

1) Add Application Transport Security setting

  • open project settings > Info tab
  • add App Transport Security Settings row parameter (as Dictionary)
  • add subrow to App Transport Security Settings parameter as Exception Domains dictionary parameter
  • add subrow to Exception Domains parameter with sequencing.com string value
  • add subrow to App Transport Security Settings parameter with Allow Arbitrary Loads boolean value
  • set Allow Arbitrary Loads boolean value as YES

2) Create bridging header file. Select File > New > File > Header File > name it as

project-name-Bridging-Header.h

3) Add SQOAuthFramework class import in the bridging header file

#import <OAuth/SQOAuthFramework.h>

4) Register your bridging header file in the project settings. select your project > project target > Build Settings > Objective-C Bridging Header specify path for bridging header file

$(PROJECT_DIR)/project-name-Bridging-Header.h

For our test feedme application mentioned the section and looking as shown below, 

4) Register these parameters in OAuth module instance. Should be done once before authentication happens. 

SQOAuth.sharedInstance().registerApplicationParametersCliendID("feedme",
                                                      clientSecret: "at867K7W88Pxrut1kEFZiOXh2Ij36q6dgEDBZyXte-rhYzBqrI_ay609awMD6ZEtGbuPHQnnW9zzncnOpxbdyg",
                                                      redirectUri: "feedme://login",
                                                      scope: "demo,external",
                                                      delegate: <your delegate>,
                                                      viewControllerDelegate: <your VC delegate>)

where:

  • delegate - class instance that conforms to "SQAuthorizationProtocol" protocol
  • viewControllerDelegate - provide your class instance as UI delegate

5) Subscribe your class for Authorization protocol SQAuthorizationProtocol

func userIsSuccessfullyAuthorized(_ token: SQToken) -> Void { }
func userIsNotAuthorized() -> Void { }
func userDidCancelAuthorization() -> Void { }

6) Authorize your user via authorizeUser method

SQOAuth.sharedInstance().authorizeUser()

In method userIsSuccessfullyAuthorized you'll receive SQToken object, that contains following 5 properties with clear titles for usage:

accessToken:    String
expirationDate:    NSDate
tokenType:        String
scope:            String
refreshToken:    String

To receive up-to-date token use token: method from SQOAuth API (it returns the updated token): 

SQOAuth.sharedInstance().token({ (SQToken?, String?) in
})

Where SQToken is a instance of SQToken object, and accessToken is string

SQOAuth is already aware about possible token expiration and handles this all. Once you need a token using code snippet shown above and it will guarantee token validity.