首页 > 解决方案 > App Clip - 支持多个营业地点

问题描述

当我设计我的 App Clip Launch 体验时,我想到 App 只能通过QR code,NFC或来触发App Clip Code。这就是为什么我将应用程序启动链接到具有特定 ID 的特定位置。

当我的应用程序上周上线时,当我尝试扫描 NFC 标签时,应用程序每次都按预期启动。

现在,如果我点击主屏幕上的 App Clip 图标,该应用程序正在启动并扫描最后一个 URL。

这对我不起作用!所以我正在寻找一种方法来检查应用程序是通过扫描还是点击启动的?我试图记录应用程序的启动,但它总是通过扫描(NFC)或图标点击按顺序运行:

AppDelegate.didFinishLaunchingWithOptions()
SceneDelegate.willConnectTo() // It's here where I am handling the Universal Link

如何检查用户是否通过点击或扫描启动了应用程序?知道点击图标时应用程序总是在模拟通用启动链接!

或者我如何查找保存的 URL?我试图获取所有UserDefaults和一些Keychain数据,但我什么也没找到!

标签: iosios14apple-appclips

解决方案


我遇到了同样的问题!不幸的是,没有办法:

  • 检查应用程序的启动方式、图标点击或 NFC/QR 扫描
  • UserDefaults或检索缓存的数据Keychain

Apple 在他们的人机界面指南中明确表示,如果您想支持多个业务,您应该添加位置服务因素!

考虑多个业务。App Clip 可以为许多不同的企业或具有多个位置的企业提供支持。在这两种情况下,人们最终可能一次将 App Clip 用于多个业务或位置。App Clip 必须处理此用例并相应地更新其用户界面。例如,考虑一种在您的 App Clip 内最近的企业或位置之间切换的方法,并在用户启动它时验证用户的位置

因此,现在您的特定位置标签应该映射到坐标[Longitude, Latitude]。Apple 为 App Clips 引入了一个新的位置验证 API,它允许您一次性检查用户扫描的 App Clip 代码、NFC 标签或 QR 代码是否在它所说的位置。

启用您的 App Clip 以验证用户的位置 要启用您的 App Clip 以验证用户的位置,请修改您的 App Clip 的Info.plist文件:

  1. 打开您的 App Clip Info.plist,添加NSAppClip键,并将其类型设置为Dictionary
  2. 将条目添加到字典中,NSAppClipRequestLocationConfirmation作为键,选择Boolean作为其类型,并将其值设置为true

但是使用 App Clip Location 服务是不同的:

  1. 解析启动 App CLip 的 URL 上的信息
  2. 向您的数据库发送请求以获取此商家的位置信息
  3. 用于activity.appClipActivationPayload确认位置(在步骤 2 中)是否在用户当前所在的区域。

下面的代码(从Apple复制)解释了如何做到这一点。

import UIKit
import AppClip
import CoreLocation

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    
    // Call the verifyUserLocation(_:) function in all applicable life-cycle callbacks.

    func verifyUserLocation(_ activity: NSUserActivity?) {
        
        // Guard against faulty data.
        guard activity != nil else { return }
        guard activity!.activityType == NSUserActivityTypeBrowsingWeb else { return }
        guard let payload = activity!.appClipActivationPayload else { return }
        guard let incomingURL = activity?.webpageURL else { return }

        // Create a CLRegion object.
        guard let region = location(from: incomingURL) else {
            // Respond to parsing errors here.
            return
        }
        
        // Verify that the invocation happened at the expected location.
        payload.confirmAcquired(in: region) { (inRegion, error) in
            guard let confirmationError = error as? APActivationPayloadError else {
                if inRegion {
                    // The location of the NFC tag matches the user's location.
                } else {
                    // The location of the NFC tag doesn't match the records;
                    // for example, if someone moved the NFC tag.
                }
                return
            }
            
            if confirmationError.code == .doesNotMatch {
                // The scanned URL wasn't registered for the App Clip.
            } else {
                // The user denied location access, or the source of the
                // App Clip’s invocation wasn’t an NFC tag or visual code.
            }
        }
    }

    func location(from url:URL) -> CLRegion? {
        
        // You should retrieve the coordinates from your Database
        let coordinates = CLLocationCoordinate2D(latitude: 37.334722,
                                                 longitude: 122.008889)
        return CLCircularRegion(center: coordinates,
                                radius: 100,
                                identifier: "Apple Park")
    }
}

就是这样,这就是您使用 App Clip 支持多个企业的方式


推荐阅读