应用程序剪辑-支持多个业务地点

问题描述

在设计App Clip Launch体验时,我牢记该应用只能通过QR codeNFCApp Clip Code来触发。因此,我将App Launch链接到具有特定ID的特定位置。

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

现在,如果我点击主屏幕上的“应用程序剪辑”图标,则该应用程序正在使用扫描的最后一个URL进行启动,我在谷歌上进行了一些搜索,发现该应用程序剪辑正在缓存上一​​次扫描的URL,并在启动时模拟通用链接点击图标!

这对我不起作用!因此,我正在寻找一种方法来检查该应用程序是否通过扫描或点击启动了?我试图记录该应用程序的启动,但它始终通过扫描(NFC)或点击图标依次运行:

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

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

或者如何查找保存的URL?我试图获取所有UserDefaults和一些Keychain的数据,但没有发现任何东西!

解决方法

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

  • 检查应用程序的启动方式,图标点击或NFC / QR扫描
  • 要从UserDefaultsKeychain检索缓存的数据

Apple在其Human Interface Guidelines上明确表示,如果您要支持多个业务,则应添加位置服务因素!

考虑多个业务。应用程序剪辑可能为许多不同功能提供支持 业务或具有多个位置的业务。同时 场景中,人们最终可能会使用App Clip超过一个 业务或地点。 App Clip必须处理此用例 并相应地更新其用户界面。例如,考虑一种方法 在App Clip中最近的商家或位置之间切换, 并 启动用户时验证其位置

因此,现在应该将您特定位置的标签映射到坐标[Longitude,Latitude]。 Apple引入了一个新的仅适用于App Clip的位置验证API,该API可让您进行一次检查,以查看用户扫描的App Clip代码,NFC标签或QR码是否在其显示的位置。

启用您的应用程序剪辑以验证用户的位置 要使您的App Clip能够验证用户的位置,请修改您的App Clip的Info.plist文件:

  1. 打开应用剪辑的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支持多个业务