无法将Pass添加到Apple Wallet

时间:2016-10-07 23:27:16

标签: ios swift swift3 wallet

我正在使用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拖入模拟器工作正常)

2 个答案:

答案 0 :(得分: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)
        }
    }

答案 1 :(得分:0)

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

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