【问题标题】:Passbook : get base64 string from the server, convert and save pkpass to iOSPassbook:从服务器获取base64字符串,转换并保存pkpass到iOS
【发布时间】:2015-10-30 06:55:18
【问题描述】:
#import <PassKit/PassKit.h>

// convert base64 string to pkpass data
NSData *passData = [[NSData alloc] initWithBase64EncodedString:strEncodeData options:0];
NSLog(passData);
// init a pass object with the data
PKPass *pass = [[PKPass alloc] initWithData:passData];
NSLog(pass);

//init a pass library
PKPassLibrary *passLib = [[PKPassLibrary alloc] init];

//check if pass library contains this pass already
if([passLib containsPass:pass]) {
    //pass already exists in library, show an error message
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Pass Exists" message:@"The pass you are trying to add to Passbook is already present." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

} else {
    //present view controller to add the pass to the library
    PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass];
    [vc setDelegate:(id)self];
    [self presentViewController:vc animated:YES completion:nil];
}

我正在尝试将存折保存到 ios 钱包。由于安全问题,我需要使用 base64 数据而不是 uri。我为此假设的流程如下所示

  1. 从服务器获取base64字符串。
  2. 使用“initWithBase64EncodedString”将base64转换为pkpass数据
  3. 使用“PKAddPassesViewController”将 pkpass 保存到钱包

使用上面的代码,进度在第二步停止并出现 nil 错误,即使解码的 base64 字符串是正确的。所以我不能确定第二步之后的代码是否会正常运行。

感谢您提前回答。

【问题讨论】:

  • 您是否确认base64字符串中包含的pass是有效的,并且可以通过pkpass文件或url添加到Passbook?
  • 我只是把使用在线工具手动生成的base64字符串(UTF-8格式)。我可以检查 xcode(object-c) 上的字符串是否正确吗?

标签: base64 passbook passkit


【解决方案1】:

我知道为时已晚,但经过大量研究后我遇到了同样的问题,问题出在initWithBase64EncodedString:passBase64 上,当从服务器响应中使用 mime 类型(如:data:application/vnd.apple.pkpass;base6NSData 得到零值)进行 base64 字符串解码时。也许这是一个错误,但如果你使用 https://github.com/l4u/NSData-Base64 中的 NSData+Base64 类,它是旧的,但你可以将其配置为与 ARC 一起使用,而不是将数据从 base64 字符串转换为 NSData 问题就消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 2021-11-10
    • 2014-05-04
    • 1970-01-01
    • 2021-08-18
    相关资源
    最近更新 更多