Swift Module: Remote Commands

Swift Module: Remote Commands

by on ‎04-28-2017 10:18 AM - edited 4 weeks ago by Community Manager (81 Views)

This guide shows how to install and use the Remote Command module for the Tealium Swift library. This module adds the ability to trigger custom code blocks from specially formatted url calls from Tealium enabled websites, the Tag Management module, or URLScheme links.

Table of Contents Placeholder

Features

  • Code blocks triggerable by url requests, urlschemes, or specially formatted strings.

Requirements

Installation & Configuration

The Lifecycle module is now automatically included with the default framework build if using Carthage. See Adding Tealium to your Swift App for more information. If adding manually, include the 'tealium/remotecommands' folder and all its files to the target build.

Configuration Methods

disableRemoteCommands()

Configuration option to disable the remote commands module.

//Sample
let config = TealiumConfig(account: "myAccount",
                           profile: "myProfile",
                       environment: "prod")
config.disableRemoteCommands()

 

disableRemoteHTTPCommand()

Configuration option to disable the _http reserved command for triggering url calls.

//Sample
let config = TealiumConfig(account: "myAccount",
                           profile: "myProfile",
                       environment: "prod")
config.disableRemoteHTTPCommand()

 

enableRemoteCommands()

Configuration option to explicitly enable the remote commands module. This is the default setting.

//Sample
let config = TealiumConfig(account: "myAccount",
                           profile: "myProfile",
                        environment: "prod")
config.enableRemoteCommands()

 

disableRemoteHTTPCommand()

Configuration option to enable the reserved _http remote command. This is the default setting.

//Sample
let config = TealiumConfig(account: "myAccount",
                           profile: "myProfile",
                       environment: "prod")
config.enableRemoteHTTPCommand()

Setup Methods

remoteCommands()

remoteCommands() -> TealiumRemoteCommands 

 

Returns the modules instance of the remoteCommands object.

//Sample
let config = TealiumConfig(account: "myAccount",
                           profile: "myProfile",
                       environment: "prod")
let tealium = Tealium(config: config)
let remoteCommands = tealium.remoteCommands()

add()

add(_ remoteCommand: TealiumRemoteCommand) 

 

//Sample
let customCommand = TealiumRemoteCommand(commandId: "myCommand",
                                        description: nil,
                                        queue: DispatchQueue.main)
                                        { (response) in
                                            // code to execute
                                        })
tealium_instance.remoteCommands().add(customCommand)

remove()

remove(commandWithIdString: String) 

 

//Sample
tealium_instance.remoteCommands().remove(commandIdWithString:"myCommand") 

Trigger Methods

init()

init(commandId: String,
        description : String?,
        queue: DispatchQueue?,
        completion : @escaping ((_ response:TealiumRemoteCommandResponse)->Void)) 

 

Parameters Description Example Value
commandId Required String identifier for the remote command "loginUpdate"
description Optional String description of the remote command "A/B Login Update"
queue Queue to trigger block on. DispatchQueue.main
completion The block of code to trigger -
// Sample
let customCommand = TealiumRemoteCommand(commandId: "myCommand",
                                        description: nil,
                                        queue: DispatchQueue.main)
                                        { (response) in
                                            // code to execute
                                            print("Custom command response: (response)")
                                        })

 

triggerCommandFrom(string)

triggerCommandFrom(urlString: String) -> TealiumRemoteCommandsError? 

 

Parameters Description Example Value
urlString String representation of a url call. "tealium://commandid?request={config={},payload={}}"
// Sample
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    let tealium = someHelperTealiumFunction()
    tealium.remoteCommands().triggerCommandFrom(urlString: url.absoluteString)
    return true
}

 

triggerCommandFrom(urlRequest)

triggerCommandFrom(request : URLRequest) -> TealiumRemoteCommandsError? 

 

Parameters Description
request Requested URL to trigger remote command
// Sample
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    let tealium = someHelperTealiumFunction()
    if tealium.remoteCommands().triggerCommandFrom(request: request) == nil {
        // Request was meant for remote command use, no need to have webview process request.
        return false
    }
    return true
}

 

Remote Command Response

The object returned and available to remote command completion blocks.

Property Type Description
status TealiumRemoteCommandStatusCode enum HTTP-style status code for the request e.g. 200, 404
urlRequest URLRequest The URL Request object
urlResponse? URLResponse The url response related to the request, if applicable
error? Error Error object containing a description of the error message and suggested resolution

Additional Resources

Change Log

  • Build 1 - Initial