1

我已经设置了一个返回 PKPass 的服务器。如果我将 URL 复制到浏览器,则会显示一个通行证(在我的 Mac 和我的 iPhone 中)。我用来下载通行证的代码如下:

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:kAPIPass]];
if (nil != data) {
    PKPass *pass = [[PKPass alloc] initWithData:data error:nil];
    PKAddPassesViewController *pkvc = [[PKAddPassesViewController alloc] initWithPass:pass];
    pkvc.delegate = self;
    [self presentViewController:pkvc
                       animated:YES
                     completion:^{
                         // Do any cleanup here
                     }
     ];
}

无论如何,当我运行此代码时,出现以下错误:

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“仅支持 RGBA 或白色空间,此方法是 hack。”

我不知道是什么错误...当我使用 Safari 下载它时,通行证似乎还可以,甚至代码也似乎还可以(只有 3 行简单的行...)有 Passkit 经验的人可以帮助我吗?

编辑:奇怪的是完全相同的代码正在一个全新的项目中工作

编辑 2:从 AppDelegate 中删除以下行,删除崩溃,这很奇怪!

[[UINavigationBar appearance] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar_bg_gen.png"]]];
4

3 回答 3

3

斯威夫特 3

这是使用完成处理程序从服务器下载 .pkpass(存折文件)并显示 pkpassviewcontroller 以进一步添加到苹果钱包的代码。

import PassKit


let url : NSURL! = NSURL(string: "YOUR .pkpass URL GOES HERE")
        let request: NSURLRequest = NSURLRequest(url:
            url as URL)
        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)

    let task : URLSessionDataTask = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in

        var error: NSError?
        let pass = try? PKPass(data: data!, error: &error)
        if error != nil {
            DispatchQueue.main.async {
                let alertView = UIAlertView(title: "Error", message: (error?.localizedDescription)!, delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "Cancel")
                alertView.show()
            }
        }
        else {
            let passLibrary = PKPassLibrary()
            if passLibrary.containsPass(pass!) {
                DispatchQueue.main.async {
                    let alertView = UIAlertView(title: "Already Exist", message: "This pass already added in wallet. Thanks!", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "Cancel")
                    alertView.show()
                    self.hideLoading()
                }
            } else {
                let pkvc = PKAddPassesViewController(pass: pass!)
                pkvc.delegate = self
                self.present(pkvc, animated: true, completion: {() -> Void in
                    // Do any cleanup here
                    self.hideLoading()
                })

            }
        }

    })
    task.resume()
于 2017-03-13T10:37:03.040 回答
2

您当前状态的代码不会调用以PKAddPassesViewController向用户展示通行证。

假设 Pass Library 可用,以下内容适用于您在 iOS6 和 iOS7 中提供的 url:

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:kAPIPass]];

if (nil != data) {
    PKPass *pass = [[PKPass alloc] initWithData:passData error:&error];

    if(error) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
        [alertView show];

    } else {

        PKAddPassesViewController *pkvc = [[PKAddPassesViewController alloc] initWithPass:pass];
        pkvc.delegate = self;
        [self presentViewController:pkvc
                           animated:YES
                         completion:^{
                                      // Do any cleanup here                     
                                     }
        ]; 
    }                       
}

或者,您可以异步加载它,以免阻塞主线程。

[NSURLConnection sendAsynchronousRequest:_request 
                                   queue:_browser 
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    if (nil != error) {
        // handle error
    } else if (nil != data) {
        PKPass *pass = [[PKPass alloc] initWithData:passData error:&error];

        if(error) {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
            [alertView show];

        } else {

            vc = [[PKAddPassesViewController alloc] initWithPass:pass];
            pkvc.delegate = self;
            [self presentViewController:pkvc
                               animated:YES
                             completion:^{
                                          // Do any cleanup here                     
                                         }
            ]; 
        }
    }
}];    
于 2013-06-25T01:27:38.037 回答
0

所以问题是 AppDelegate 中的这一行:

[[UINavigationBar appearance] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar_bg_gen.png"]]];

显示通行证的控制器似乎不喜欢在我添加存折之前完美运行的图像。

于 2013-06-25T13:08:22.637 回答