SwiftyInAppMessaging

共存您的自定义视图和 InAppMessaging 默认视图的最简单方法。

特性

只需一步即可开始使用 SwiftyInAppMessaging。

func application(_ application: UIApplication, didFinishLaunchWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  InAppMessaging.inAppMessaging().messageDisplayComponent = SwiftyInAppMessaging()
}

用法

定义您的消息处理程序

struct InAppMyModalMessageHandler: InAppModalMessageHandler {
    let messageForDisplay: InAppMessagingModalDisplay

    init(message messageForDisplay: InAppMessagingModalDisplay) {
        self.messageForDisplay = messageForDisplay
    }

    func displayMessage(with delegate: InAppMessagingDisplayDelegate) throws {
        let alert = UIAlertController(title: self.messageForDisplay.title)
        let ok = UIAlertAction(title: "OK", style: .default, handler: { [weak self] _ in
            guard let messageForDisplay = self?.messageForDisplay else {
                return
            }
            
            delegate.messageClicked?(messageForDisplay, with: InAppMessagingAction(actionText: "OK", actionURL: nil)
        })

        alert.addAction(ok)

        DispatchQueue.main.async {
            UIApplication.shared.topViewController?.present(alert, animated: true, completion: nil)
        }
    }

    func displayError(_ error: Error) {
        debugLog(error)
    }
}

定义您的消息路由器

enum YourOwnMessageRouter {
    case banner(InAppMessagingBannerDisplay)
    case card(InAppMessagingCardDisplay)
    case customModal(InAppMessagingModalDisplay)
    case imageOnly(InAppMessagingImageOnlyDisplay)

    static func match(for message: InAppMessagingDisplayMessage) -> Self? {
        switch message.typeWithDisplayMessage {
        case .banner(let message):
            return .banner(message: message)
        case .card(let message):
            return .card(message: message)
        case .imageOnly(let message):
            return .imageOnly(message: message)
        case .modal(let message):
            return .customModal(message: message)
        @unknown default:
            return nil
    }
    
    var messageHandler: InAppMessageHandler {
        switch self {
        case .banner(let message):
            return message.defaultHandler
        case .card(let message):
            return message.defaultHandler
        case .imageOnly(let message):
            return message.defaultHandler
        case .customModal(let message):
            return InAppMyModalMessageHandler(message: message)
        }
    }
}

通过配置传递处理程序

func application(_ application: UIApplication, didFinishLaunchWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  InAppMessaging.inAppMessaging().messageDisplayComponent = SwiftyInAppMessaging<YourOwnMessageRouter>()
}

依赖

安装

Carthage

只需添加到您的 Cartfile

github "fumito-ito/SwiftyInAppMessaging" ~> 2.0.0

并运行 carthage update

⚠️firebase ios sdk 宣布停止支持 carthage。如果 firebase-ios-sdk 停止支持 carthage,此库也将随之停止。

Swift Package Manager

只需将其添加到您的 Package.swift 中的 dependencies 下

let package = Package(
    name: "MyPackage",
    products: [...],
    dependencies: [
        .package(url: "https://github.com/fumito-ito/SwiftyInAppMessaging.git", .upToNextMajor(from: "2.0.0"))
    ]
)

Cocoapods

只需添加到您的 Podfile

pod 'SwiftyInAppMessaging'

并运行 pod install

如果您遇到关于 Double-quoted include "*.h" in framework header, expected angle-bracketed instead 的错误,您可以通过以下步骤避免这些错误。

推荐

  1. 更新您的 cocoapods 到 1.10 或更高版本
  2. 再次运行 pod install

如果您无法更新 Cocoapods

  1. 点击 Pods 项目
  2. 点击项目的 Build Settings
  3. Quoted include in Framework Header 更改为 No

有关更多信息,请参阅 一个 firebase-ios-sdk 问题

许可证

SwiftyInAppMessaging 根据 Apache License 2.0 许可。 有关更多详细信息,请参见 LICENSE 文件。