2

所有,我一直在阅读 Apple 文档,并且找到了显示通行证和更改应用程序通行证的信息。但是有没有办法可以将 .pass zip 存储在 XCODE 中并显示在弹出视图控制器中?

我已经想通了,但我不工作。

有什么建议吗??

var pkfile : NSData = NSData(contentsOfFile: "Event.pkpass")!
        var pass : PKPass = PKPass(data: pkfile, error: nil)
        let vc = PKAddPassesViewController(pass: pass)
        self.presentViewController(vc, animated: true, completion: nil)
4

3 回答 3

6

我已经这样做了:

 var filePath = NSBundle.mainBundle().pathForResource("Event", ofType:"pkpass")
        var pkfile : NSData = NSData(contentsOfFile: filePath!)!
        var pass : PKPass = PKPass(data: pkfile, error: nil)
        let vc = PKAddPassesViewController(pass: pass) as PKAddPassesViewController
        self.presentViewController(vc, animated: true, completion: nil)

并确保在构建设置/复制捆绑资源中设置了 pkpass 文件。

希望这可以帮助某人。

于 2015-02-26T16:01:32.690 回答
3

对于斯威夫特 4

if let filepath = Bundle.main.path(forResource: "BoardingPass", ofType: "pkpass") {
    let pkfile : NSData = NSData(contentsOfFile: filepath)!
    let pass : PKPass = PKPass(data: pkfile as Data, error: nil)
    let vc = PKAddPassesViewController(pass: pass) as PKAddPassesViewController
    self.present(vc, animated: true, completion: nil)
} else {
    print("Boarding pass not found.")
}
于 2017-12-29T09:52:29.160 回答
2
    let filePath = Bundle.main.path(forResource: "BoardingPass", ofType: "pkpass")
    let pkfile : NSData = NSData(contentsOfFile: filePath!)!

    do {
        let pass = try PKPass(data: pkfile as Data)
        let vc = PKAddPassesViewController(pass: pass)
        self.present(vc!, animated: true)
    }
    catch {
        print("something went wrong!")
    }
于 2019-07-17T13:28:29.670 回答