Passbook,在应用中显示.pass

时间:2015-02-26 14:27:40

标签: swift passkit

全部, 我一直在阅读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)

3 个答案:

答案 0 :(得分: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文件。

希望这有助于某人。

答案 1 :(得分:3)

对于Swift 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.")
}

答案 2 :(得分:0)

    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!")
    }