从我的服务器下载iOS自定义应用中的PKPass
我已经设置了一个返回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 } ]; }
无论如何,当我运行这个代码时,我有以下错误:
*终止应用程序,由于未捕获exception“NSInternalInconsistencyException”,原因:“只支持RGBA或白色空间,这种方法是一个黑客。
我不知道什么是错误…当我用Safari下载它,甚至代码似乎没问题的时候,通行证似乎没问题(只有3个简单的行…)有人使用Passkit可以帮助我吗?
编辑:奇怪的是,完全相同的代码工作在一个新的新项目
编辑2:从AppDelegate删除以下行,删除崩溃,这很奇怪!
[[UINavigationBar appearance] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar_bg_gen.png"]]];
您的代码处于当前状态,不会调用PKAddPassesViewController
向用户显示通行证。
假设通过库可用,下面的工作与您在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 } ]; } }
或者,您可以asynchronous加载它,以免阻塞主线程。
[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 } ]; } } }];
所以问题是在AppDelegate这一行:
[[UINavigationBar appearance] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar_bg_gen.png"]]];
看起来,直到我添加存折完美工作的图像,不被显示通过的控制器所喜欢。
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()