我设置了一个返回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
}
];
}
无论如何,当我运行此代码时,我会出现以下错误:
*由于未捕获的异常"NSInternalConferenceException"而终止应用程序,原因:"仅支持RGBA或白色空间,这种方法是一种破解
我不知道虫子是什么。。。当我用Safari下载通行证时,它似乎还可以,甚至代码也可以(只有3行…)有Passkit经验的人可以帮助我吗?
编辑:奇怪的是,完全相同的代码在一个新的项目中工作
编辑2:从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()
处于当前状态的代码不会调用PKAddPassesViewController
来向用户呈现通行证。
假设通行证库可用,以下内容将与您在iOS6&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
}
];
}
}
}];
所以问题出在AppDelegate中的这一行:
[[UINavigationBar appearance] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbar_bg_gen.png"]]];
在我添加存折之前,这个图像似乎一直运行良好,但显示通行证的控制器不喜欢它。