Hi,
I'm trying to customize the In-App Message so it adapts to our App popups, banners e.t.c. so i've made a class that adapts to the UAInAppMessageAdapterProtocol, though the documentation is really lacking on Swift I got it working. I kinda want to know if this approach is correct:
Code:
class UACustomBannerMessage: NSObject, UAInAppMessageAdapterProtocol {
required init(Message message: UAInAppMessage) {
DispatchQueue.main.async {
if let viewController = UIApplication.shared.keyWindow?.rootViewController,
let banner: UAInAppMessageBannerDisplayContent = message.displayContent as? UAInAppMessageBannerDisplayContent,
let messageText = banner.body?.text {
viewController.showToast(message: messageText) // Triggers our own Toast with our own styling
}
}
}
static func adapter(for message: UAInAppMessage) -> Self {
return self.init(Message: message)
}
func prepare(_ completionHandler: @escaping (UAInAppMessagePrepareResult) -> Void) {
print("Preparing for message")
completionHandler(.success)
}
func isReadyToDisplay() -> Bool {
print("Ready to display!!")
return true
}
func display(_ completionHandler: @escaping (UAInAppMessageResolution) -> Void) {
completionHandler(.timedOut())
}
}
This code is triggered by implementing:
UAirship.inAppMessageManager()?.setFactoryBlock({ (message) -> UAInAppMessageAdapterProtocol in
return UACustomBannerMessage(Message: message)
}, for: .banner)
The toast that's fired when the in-app message arrives:
class ToastView: UIView, Themable {
@IBOutlet weak var messageLabel: UILabel!
private var message: String?
override func awakeFromNib() {
ThemeManager.shared.addThemable(self)
messageLabel.text = "toast_message".localized()
}
func setMessage(Message message: String) {
messageLabel.text = message
}
func applyTheme(_ theme: Theme) {
self.backgroundColor = theme.primaryColor
self.messageLabel.font = theme.defaultTextFont
self.messageLabel.textColor = theme.lightTextColor
}
}
Is this the correct approach, if not what's the best way of customizing the in-app messages? Also I wanted to know how to correctly use the UAInAppMessageResolution for modals and other popup types (the toast is automatically dismissed after 0.5 seconds)
Thx!
Comments
9 comments