【问题标题】:Unable to add Pass to Apple Wallet无法将通行证添加到 Apple 钱包
【发布时间】:2016-10-07 23:27:15
【问题描述】:

我正在使用 Apple Wallet 进行一些测试。我有一张通行证,我想在用户点击按钮时将其添加到用户的钱包中。这是我的代码:

        let filePath = Bundle.main.path(forResource: "DealsPasses", ofType: "pkpass")!
        let passData = try? Data(contentsOf: URL.init(fileURLWithPath: filePath), options: .alwaysMapped)
        let pass = PKPass(data: passData!, error: nil)
        let passVC = PKAddPassesViewController(pass: pass)
        navigationController?.pushViewController(passVC, animated: true)

但是;当用户点击按钮时,

AX Exchange error: Error Domain=Accessibility Code=0 "Remote service does not respond to _accessibilityMachPort" UserInfo={NSLocalizedDescription=Remote service does not respond to _accessibilityMachPort}

以 ~200/min 的速度向控制台发送垃圾邮件,并且没有显示 PKAddPassesViewController(或者如果是,它只是有一个纯白色视图)

在 iPhone SE(设备)上运行 xCode 8

(旁注:将 DealsPasses.pkpass 拖到模拟器中就可以了)

【问题讨论】:

  • 您使用的是 KIF、Xarmin 还是其他框架?两者都使用 iOS 辅助功能 (VoiceOver) 来定位 UI 元素,这不是正常用途。当您将控制权传递给第三方控制器(在本例中为 PKAddPassesViewController)时,可访问性无法再访问 UI 元素。
  • 这是一个有趣的观点。我没有使用 KIF 或 Xamarin,但我确实有一些其他框架可能会做类似的事情。我会尝试在没有这些的情况下进行发布构建,看看会发生什么。

标签: ios swift swift3 wallet


【解决方案1】:
Please use the following code in swift 4.

请从此Link 下载示例通行证并将这些通行证保存在应用沙箱中。

import PassKit

//Function to open wallet view in your app. 
func loadWalletView() {
        if !PKPassLibrary.isPassLibraryAvailable() {
            let alert = UIAlertController(title: "Error", message: "PassKit not available", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
                switch action.style{
                case .default:
                    print("default")
                case .cancel:
                    print("cancel")
                case .destructive:
                    print("destructive")
                @unknown default:
                    break
                }}))
            self.present(alert, animated: true, completion: nil)
        }
        let resourcePath : String? = Bundle.main.resourcePath

        do {
            let passFiles : NSArray = try FileManager.default.contentsOfDirectory(atPath: resourcePath!) as NSArray
            for  passFile in passFiles {

                let passFileString = passFile as! String
                if passFileString.hasSuffix(".pkpass") {
                    openPassWithName(passName: passFileString)
                }
            }

        }catch {
            print(error.localizedDescription)
        }
    }

func openPassWithName(passName : String) {
        print(passName)

        let passFile = Bundle.main.resourcePath?.appending("/\(passName)")
        let passData = NSData(contentsOfFile: passFile!)

        do {
            let newpass = try PKPass.init(data: passData! as Data)
            let addController =  PKAddPassesViewController(pass: newpass)
            addController?.delegate = self
            self.present(addController!, animated: true)
        } catch {
            let alert = UIAlertController(title: "Error", message: "PassKit not available", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
                switch action.style{
                case .default:
                    print("default")
                case .cancel:
                    print("cancel")
                case .destructive:
                    print("destructive")
                @unknown default:
                    break
                }}))
            self.present(alert, animated: true, completion: nil)

            print(error)
        }
    }

【讨论】:

    【解决方案2】:

    问题原来与将 PKAddPassesViewController 推送到导航控制器的堆栈有关。

    替换navigationController?.pushViewController(passVC, animated: true) present(passVC, animated: true, completion: nil) 解决了这个问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多