Zap to App Communication
Zap experiences can send arbitrary strings of data to the host app using the Z.device.messageHost(…) function. This article shows how to receive these messages within your iOS host app.
Attaching Handler Block
Section titled “Attaching Handler Block”Once the Zappar UIViewController has been constructed its onMessage
property can be set to a block that takes a single String parameter:
zapparComponent = ZapparEmbed.zapcodeViewController(withFinish: self, embedOptions: options)zapparComponent.onMessage = {(_ msg: String) -> Void in print("Experience sent this message: \(msg)")}
The block will be executed when Z.device.messageHost(…) is called by a zap experience.
Threading Considerations
Section titled “Threading Considerations”The Zappar API makes no guarantees about which thread the onMessage
block will be executed. For this reason, it’s best to ensure that any thread-sensitive methods are called from the main thread:
// Use a weak reference to self to avoid circular reference issuesweak var parentViewController = self
func zapparSetup() {
…
zapparComponent.onMessage = {(_ msg: String) -> Void in // Call the processMessage method on the main thread parentViewController.performSelector(onMainThread: #selector(self.processMessage), with: msg, waitUntilDone: false) }}
// Elsewherefunc processMessage(_ msg: String) { print(String(format: "@%", msg))}