从我的服务器下载iOS自定义应用程序中的PKPass

时间:2013-06-24 13:48:09

标签: ios passbook passkit

我已经设置了一个返回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或   白色空间,这种方法很黑。'

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

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

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

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

3 个答案:

答案 0 :(得分:2)

您的代码处于当前状态不会调用PKAddPassesViewController向用户显示该传递。

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

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                     
                                         }
            ]; 
        }
    }
}];    

答案 1 :(得分:2)

Swift 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()

答案 2 :(得分:0)

问题是AppDelegate中的这一行:

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

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