我正在用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 (device)上运行xCode 8
(边注:拖拽dealspass。Pkpass进入模拟器工作正常)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)
}
}
问题原来是与将PKAddPassesViewController推送到导航控制器的堆栈有关。
取代navigationController?.pushViewController(passVC, animated: true)
用present(passVC, animated: true, completion: nil)
解决了这个问题!