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

Once the Zappar UIViewController has been constructed its onMessage property can be set to a block that takes a single NSString parameter:

// Construct embed component UIViewController
self.zapparComponent = [ZapparEmbed zapcodeViewControllerWithFinish:self embedOptions:options];

self.zapparComponent.onMessage = ^(NSString * msg) {
    NSLog(@"Experience sent this message: %@", msg);
};

The block will be executed when Z.device.messageHost(...) is called by a zap experience.

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 issues
__weak typeof(self) parentViewController = self;

self.zapparComponent.onMessage = ^(NSString * msg) {
    // Call the processMessage method on the main thread
    [parentViewController performSelectorOnMainThread:@selector(processMessage:) withObject:msg waitUntilDone:NO];
};

// Elsewhere
-(void)processMessage:(NSString*)msg {

}
zapcode branded_zapcode i