Swift Module: Delegate

Swift Module: Delegate

by on ‎02-21-2017 08:45 AM - edited a month ago by Community Manager (79 Views)
This guide shows how to install and use the Delegate module from the Tealium Swift library.
 
The following topics are covered:
Table of Contents Placeholder

Features

  • Adds a multicast delegate handler to allow delegates to monitor or suppress dispatch events.

Requirements

  • Swift 3.0+
  • Foundation dependency

Installation & Configuration

The delegate module is now automatically included with the default framework build if using Carthage.  See the 'Adding Tealium to your Swift App' document.  If adding manually, include the 'tealium/delegate' folder and all it's files to the target build.

Methods

add()

Adds a weak pointer to a class instance conforming to the TealiumDelegate protocol.

add(delegate: TealiumDelegate)
Parameters Description Example Value
delegate Any class conforming to the TealiumDelegate protocol. -
// SAMPLE
func someSetupMethod {

// Presuming 'tealium' was initialized earlier
tealium.delegates().add(delegate:self)

}

remove()

Removes the weak pointer reference to the given class conforming to the TealiumDelegate protocol.

remove(delegate: TealiumDelegate)
Parameters Description Example Value
delegate Any class conforming to the TealiumDelegate protocol. -
// SAMPLE
func someDeinitMethod {

// Presuming 'tealium' was initialized earlier
tealium.delegates().remove(delegate:self)

}

removeAll()

Removes all weak pointers from tracking.

removeAll()
Parameters Description Example Value
(None) - -
// SAMPLE
func someMethod {

// Presuming 'tealium' was initialized earlier
tealium.delegates().removeAll()

}

TealiumDelegate Protocol

The following methods make up the protocol:

tealiumShouldTrack()

Should the Tealium instance dispatch a track event with the given dictionary data.

tealiumShouldTrack(data: [String:Any]) -> Bool
Parameters Description Example Value
data [String:Any] dictionary of data. -
// SAMPLE
class SomeClass {

func someSetupMethod {
// Presuming 'tealium' initialized earlier
tealium.delegates().add(delegate:self)
}

}

extension SomeClass : TealiumDelegate {
func tealiumShouldTrack(data:[String:Any]) -> Bool {
// Can check keys and values for data that should trigger a false, otherwise..
return true
}
}

tealiumTrackCompleted()

Response data regarding a dispatch just completed.

tealiumTrackCompleted(success: Bool, info: [String:Any]?, error: Error?)
Parameters Description Example Value
success Boolean if dispatch completed acceptably. true
info Dictionary of the original payload and any other returned data regarding dispatch. -
error Error, if any, encountered during dispatch processing. -
// SAMPLE
class SomeClass {

func someSetupMethod {
// Presuming 'tealium' initialized earlier
tealium.delegates().add(delegate:self)
}

}

extension SomeClass : TealiumDelegate {
func tealiumTrackCompleted(success: Bool, info: [String:Any]?, error: Error?) {
// Can check if successful, response info, and if any errors occurred.
}
}

Additional Resources

Change Log

- Build 1 - Current