我遇到了同样的问题!不幸的是,没有办法:
- 检查应用程序的启动方式、图标点击或 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
文件:
- 打开您的 App Clip
Info.plist
,添加NSAppClip键,并将其类型设置为Dictionary
。
- 将条目添加到字典中,
NSAppClipRequestLocationConfirmation
作为键,选择Boolean
作为其类型,并将其值设置为true
。
但是使用 App Clip Location 服务是不同的:
- 解析启动 App CLip 的 URL 上的信息
- 向您的数据库发送请求以获取此商家的位置信息
- 用于
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 支持多个企业的方式