1

我正在使用 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 拖到模拟器中就可以了)

4

2 回答 2

3
Please use the following code in swift 4.

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

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)
        }
    }
于 2019-08-20T14:11:05.090 回答
1

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

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

于 2016-10-11T19:22:59.493 回答