Skip to content

How to migrate

Hassan Shahbazi edited this page May 8, 2020 · 3 revisions

Since this is the first stable version of the Swift SDK, this document takes a quick guide for apps migrating from Objc SDK to the new modern Swift version. All examples are written in Swift for both SDKs. Versions that are compared when this guide is written:

Imports

To have access to public functions of both SDKs, they have to be imported. Both SDKs are usable with importing the name, however, it is recommended to import NinchatLowLevelClientwhen using the Swift SDK as it may be needed by some delegates and functions.

__OBJC_SDK__
import NinchatSDK

__SWIFT_SDK__
import NinchatSDKSwift
import NinchatLowLevelClient

Session initialization

Both SDKs come with almost the same init functions, however, Swift SDK can be initiated with a custom configuration object that overrides fetched configurations from the server.

__OBJC_SDK__
let ninchat: NINChatSession = NINChatSession(configKey: YOUR_CONFIG_KEY, queueID: OPTIONAL_QUEUE_ID, environments: OPTIONAL_STRING_ARRAY)

__SWIFT_SDK__
let ninchat: NINChatSession = NINChatSession(configKey: YOUR_CONFIG_KEY, queueID: OPTIONAL_QUEUE_ID, environments: OPTIONAL_STRING_ARRAY, configuration: CUSTOM_CONFIGURATION)

Session metadata

An example of how Swift SDK helps developers to code less and gain more is to set metadata for a session. Metadata for the new SDK is initiated using a [String:AnyHashabe] map when a session is initiated. This is a situation where NinchatLowLevelClient has to be imported in the code.

__OBJC_SDK__
ninchat.audienceMetadata = NINLowLevelClientProps()
ninchat.audienceMetadata?.setString(KEY, val: VALUE)
ninchat.audienceMetadata?.setInt(KEY, val: VALUE)

__SWIFT_SDK__
import NinchatLowLevelClient
let ninchat: NINChatSession = NINChatSession(configKey: YOUR_CONFIG_KEY, metadata: NINLowLevelClientProps.initiate(metadata: [KEY: VALUE]))

Session delegates

Both SDKs come with identical delegate methods, though there is just a quick change to types that are used on the Swift SDK. To override colors and images in Swift SDK, AssetConstants and ColorConstants enums are used instead of NINImageAssetKey and NINColorAssetKey respectively.

__OBJC_SDK__
ninchat.delegate = self
func ninchatDidEnd(_ ninchat: NINChatSession)
func ninchat(_ session: NINChatSession, overrideImageAssetForKey assetKey: NINImageAssetKey) -> UIImage?
func ninchat(_ session: NINChatSession, overrideColorAssetForKey assetKey: NINColorAssetKey) -> UIColor?
func ninchat(_ session: NINChatSession, onLowLevelEvent params: NINLowLevelClientProps, payload: NINLowLevelClientPayload, lastReply: Bool)
func ninchat(_ session: NINChatSession, didOutputSDKLog message: String)
func ninchatDidFail(toResumeSession session: NINChatSession) -> Bool

__SWIFT_SDK__
ninchat.delegate = self
func ninchatDidEnd(_ ninchat: NINChatSession)
func ninchat(_ session: NINChatSession, overrideImageAssetForKey assetKey: AssetConstants) -> UIImage?
func ninchat(_ session: NINChatSession, overrideColorAssetForKey assetKey: ColorConstants) -> UIColor?
func ninchat(_ session: NINChatSession, onLowLevelEvent params: NINLowLevelClientProps, payload: NINLowLevelClientPayload, lastReply: Bool)
func ninchat(_ session: NINChatSession, didOutputSDKLog message: String)
func ninchatDidFail(toResumeSession session: NINChatSession) -> Bool

Session start

  • Swift SDK introduces throwable start functions, meaning that any unexpected error should be handled with a do-catch statement.
  • Both SDKs have almost identical parameters and closure bodies, however, Swift SDK returns credentials as a Codablestruct.
__OBJC_SDK__
ninchat.start { (error: Error?, credentials: NINSessionCredentials?) -> Void in }
ninchat.start(with: credentials) { (error: Error?, credentials: NINSessionCredentials?) -> Void in }

__SWIFT_SDK__
try ninchat.start { (error: Error?, credentials: NINSessionCredentials?) -> Void in }
try ninchat.start(credentials: credentials) { (error: Error?, credentials: NINSessionCredentials?) -> Void in }

Session UI

In both SDKs, Ninchat can be started as either an UIViewController or UINavigationControllerdepending on how it is called. However, some changes have to be applied for the migration. Please note that this API on Swift SDK is throwable too.

__OBJC_SDK__
let viewController = ninchat.viewController(withNavigationController: false)
let navigationController = ninchat.viewController(withNavigationController: true)

__SWIFT_SDK__
let viewController = try ninchat.chatSession(within: self.navigationController)
let navigationController = try ninchat.chatSession(within: nil)