GeoOffersSDK 0.4.2

GeoOffersSDK 0.4.2

Zappit 维护。



  • PJ

GeoOffersSDK

GeoOffers:更智能城市的奖励平台

https://zappitrewards.com

这是什么?

The GeoOffersSDK lets you turn your mobile app into a powerful marketing tool. Effortlessly add geolocation driven proximity experiences to your app, engage your users and understand them with analytics. The SDK lets you quickly connect to the GeoOffers platform where you can configure your campaigns and marketing setup.

如何使用它

推荐使用 CocoaPods 安装 GeoOffersSDK。SDK 支持最低 iOS 版本为 11.4。

CocoaPods

  1. 如果您尚未安装 CocoaPods,请执行以下命令安装:
$ sudo gem install cocoapods

如果安装过程中遇到任何问题,请参阅他们的入门指南

  1. 将以下内容添加到您的Podfile中
pod 'GeoOffersSDK'
  1. 使用以下命令安装您的Pod更新
pod install
  1. 打开项目工作空间,而不是项目文件,并确保依赖项加载正确。

配置

本节描述了如何在您的应用中配置iOS SDK。

应用 info.plist

请确保以下条目存在于您的info.plist中

类型 详情
NSLocationAlwaysUsageDescription 字符串 我们将使用您的位置提供基于位置的相关优惠。
NSLocationAlwaysAndWhenInUseUsageDescription 字符串 我们将使用您的位置提供基于位置的相关优惠。
NSLocationWhenInUseUsageDescription 字符串 我们将使用您的位置提供基于位置的相关优惠。

重要 如果这些键不在您的 info.plist 中,iOS位置服务将无法启动,这将阻止应用启动地理围栏和接收位置更新。

应用能力

GeoOffersSDK要求应用启用选定的 能力。在XCode中打开项目,转到目标,并选择 能力 配置页面。

后台模式 部分中,确保以下权限已启用:• 背景获取 • 远程通知 • 位置更新

background modes

您还应启用 推送通知 并向平台提供 推送通知AppKey推送通知证书

push notificaions

SDK配置

您应从平台团队收到 注册码身份验证令牌,您需要使用这些值配置SDK,以便它能够成功与平台通信。

GeoOffers配置

您需要创建一个 GeoOffersConfiguration 对象,并将其传递给 GeoOffersSDKService 类。下面是配置属性的说明:

类型 描述 默认值
registrationCode 字符串 从平台团队提供的值
authToken 字符串 从平台团队提供的值
testing 布尔型 指向 SDK 的生产或测试环境 false
selectedCategoryTabBackgroundColor 字符串 提供列表视图头部按钮的高亮颜色 #FF0000
minimumRefreshWaitTime 双精度浮点型 为了不浪费用户带宽,最好配置一个合理的刷新周期(以秒为单位) 10分钟
minimumDistance 双精度浮点型 用户移动的最小距离,在 SDK 检查优惠更新之前(以米为单位) 500米
mainAppUsesFirebase 布尔型 SDK 使用 Firebase 来实现部分消息功能。如果您的应用程序使用 Firebase,请设置此标志以防止冲突。如果使用 Firebase 而不设置此选项,则应用在启动时将失败。 false

GeoOffersSDKService

您应该只创建此类的单个实例,并在您的应用程序中共享它。我们强烈建议创建一个 Singleton 包装类,在该类中,您可以使用上面定义的 GeoOffersConfiguration 初始化该实例。请参考 SampleApp 以获取示例。

class GeoOffersWrapper {
   static let shared = GeoOffersWrapper()
   
   var geoOffers: GeoOffersSDKService = {
      let registrationCode = <registrationCode>
      let authToken = <authenticationToken>
      let configuration = GeoOffersConfigurationDefault(registrationCode: registrationCode, authToken: authToken, testing: true)
      let geoOffers = GeoOffersSDKServiceDefault(configuration: configuration)
      return geoOffers
   }()
}

集成

AppDelegate

SDK 需要主应用程序将某些 AppDelegate 方法调用转发到 SDK,以允许其正确运行。请在您的 AppDelegate 中实现以下功能。

didFinishLaunchingWithOptions

初始化 GeoOffersSDK,建议使用 Singleton 以简化过程,但使用您自己的依赖注入模式

调用 GeoOffersSDK 实例上的匹配方法

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GeoOffersWrapper.shared.geoOffers.application(application, didFinishLaunchingWithOptions: launchOptions)

        return true
    }
应用成为活动状态

调用 GeoOffersSDK 实例上的匹配方法

    func applicationDidBecomeActive(_ application: UIApplication) {
        GeoOffersWrapper.shared.geoOffers.applicationDidBecomeActive(application)
    }
使用完成处理器执行获取操作

如果你要处理“完成处理器”,请在 geoOffers 函数的 completionHandler 参数中传递 nil

    func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        GeoOffersWrapper.shared.geoOffers.application(application, performFetchWithCompletionHandler: completionHandler)
    }
处理后台URL会话事件

调用 GeoOffersSDK 实例上的匹配方法

    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        GeoOffersWrapper.shared.geoOffers.application(application, handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
    }
使用设备令牌注册远程通知

调用 GeoOffersSDK 实例上的匹配方法

    // Required if implementing Remote notifications
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        GeoOffersWrapper.shared.geoOffers.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
    }
接收远程通知

在 GeoOffersSDK 实例上调用匹配的方法

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        GeoOffersWrapper.shared.geoOffers.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: nil)
    }
接收远程通知:completionHandler

如果你要处理“完成处理器”,请在 geoOffers 函数的 completionHandler 参数中传递 nil

调用 GeoOffersSDK 实例上的匹配方法

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        GeoOffersWrapper.shared.geoOffers.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
    }

优惠列表视图控制器

SDK 将为您提供用于向用户显示当前优惠列表的 UIViewController。请调用以下方法以获取 UIViewController。请将此 视图控制器放入 UINavigationController 中,并在您自己的应用程序中显示它。我们这样做是为了让您 从现有的视图控制器中推动或显示它,或者根据您应用程序的风格将其放置在自己的标签页内部。

GeoOffersWrapper.shared.geoOffers.buildOfferListViewController()

权限

应用程序在使用 SDK 之前直到具有所需的权限才能正常运行。我们需要 位置推送通知 权限。我们已在 SDK 中添加了您可以调用的方法来简化必要代码的调用。您应选择在哪里以及何时在您的应用程序中调用这些方法,以提高用户接受权限的可能性。

GeoOffersWrapper.shared.geoOffers.requestPushNotificationPermissions()

GeoOffersWrapper.shared.geoOffers.requestLocationPermissions()

从通知到优惠券/优惠页面的深链接

当有优惠可用且应用程序在后台运行时,SDK 会向用户发送本地通知。如果您想在用户点击其中一个通知并从应用程序启动时将用户深链接到优惠券或优惠页面,则应执行以下操作。

AppDelegate 的 application:didFinishLaunchingWithOptions 方法中,您应将自己注册为 UNUserNotifications 的代理。

// Register as the UNUserNotificationCenterDelegate to support deeplinking to the coupon when the user taps the notification and the app is closed

UNUserNotificationCenter.current().delegate = self

实现以下代理方法

// Required if you want to implement deeplinking to coupon when user taps notification when the app is closed
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let request = notification.request
        deeplinkToCoupon(request.identifier, userInfo: request.content.userInfo)
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let request = response.notification.request
        deeplinkToCoupon(request.identifier, userInfo: request.content.userInfo)
    }
}

以下代码作为呈现用户优惠券的示例实现

extension AppDelegate {
    private func deeplinkToCoupon(_ identifier: String, userInfo: [AnyHashable:Any]) {
        guard GeoOffersWrapper.shared.service.isGeoOffersNotification(userInfo: userInfo) else { return }
        let viewController = GeoOffersWrapper.shared.service.buildOfferListViewController()
        viewController.navigationItem.leftBarButtonItem = buildCloseButton()
        let navigationController = UINavigationController(rootViewController: viewController)
        window?.rootViewController?.present(navigationController, animated: true, completion: {
            GeoOffersWrapper.shared.service.deeplinkToCoupon(viewController, notificationIdentifier: identifier, userInfo: userInfo)
        })
    }
    
    private func buildCloseButton() -> UIBarButtonItem {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "close"), for: .normal)
        button.tintColor = .black
        button.addTarget(self, action: #selector(closeCouponModal), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        let item = UIBarButtonItem(customView: button)
        return item
    }
    
    @objc private func closeCouponModal() {
        window?.rootViewController?.dismiss(animated: true, completion: nil)
    }
}