Swift PKPass 错误“无法读取通行证,因为它无效”

Posted

技术标签:

【中文标题】Swift PKPass 错误“无法读取通行证,因为它无效”【英文标题】:Swift PKPass eror 'The pass cannot be read because it isn’t valid' 【发布时间】:2020-04-28 11:51:28 【问题描述】:

我正在从我的服务器创建一个 Ticket 并将其传递到我的 ios 应用程序,但是当我尝试将 JSON 数据传递到 PKPass 模型时,我不断收到此错误。

Error Domain=PKPassKitErrorDomain Code=1 "The pass cannot be read because it isn’t valid.

在我的服务器中,它说票证已成功创建,但我仍然在 Xcode 中收到此错误。我解码数据错误吗?如何使用 JSON 响应创建 PKPass

服务器端代码:

exports.createTicket = functions.https.onRequest(async (req, res) => 

const passName = "Event"

try 
  const examplePass = await createPass(
      model: "./models/Event",
      certificates: 
          wwdr: "./models/certs/wwdrc.pem",
          signerCert: "./models/certs/signerCert.pem",
          signerKey: 
              keyFile: "./models/certs/signerKey.pem",
              passphrase: "54321"
          
      ,
      overrides: 
          // keys to be added or overridden
          serialNumber: "AAGH44625236dddaffbda"
      
  );

  examplePass.barcode("36478105430"); // Random value

// Generate the stream, which gets returned through a Promise
const stream = examplePass.generate();

res.set(
  "Content-Type": "application/vnd.apple.pkpass",
  "Content-disposition": `attachment; filename=$passName.pkpass`
);

stream.pipe(res);

console.log("Successfully created ticket", stream)
 catch (err) 
console.log("There was an error", err)

    res.set(
        "Content-type": "text/html",
    );

    res.send(err.message);
 
 );

IOS端:

AF.request(URLString, method: .post, parameters: requestData).responseString  response in

        guard let data = response.data as? Data else  return 

        do 
            let pass = try PKPass(data: data)
            print(pass)
         catch 
            print("Error", error)
        


    

pass.json


"formatVersion": 1,
"passTypeIdentifier": "pass.com.CodyHost.Finder-Dev",
"teamIdentifier": "L9C6U8UKPB",
"organizationName": "Finder",
"description": "Rolling loud new york 2020",
"eventTicket": 

签名:

MMKABgkqwoZIwobDtw0BBwLCoMKAMMKAAgEBMQswCQYFKw4DAhoFADDCgAYJKsKGSMKGw7cNAQcBAADCoMKCCkkwwoIEIz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w5QDAAAAAAAA

【问题讨论】:

如果您尝试手动将您的通行证添加到手机,或者双击它并在 Mac 上使用通行证查看器打开它,控制台日志会显示什么内容?三个主要原因 - 证书/签名/清单错误;缺少必填文件(例如 icon.png);或 pass.json 中的无效/缺失值。日志会告诉您通行证被拒绝的原因。 @PassKit 当我深入挖掘时,我收到了这个错误The passTypeIdentifier or teamIdentifier provided may not match your certificate, or the certificate trust chain could not be verified. 你能发布你的 pass.json 和 base64 签名吗? 我已经在更新的问题中发布了我的 pass.json 文件,但我不知道你所说的 base64 签名是什么意思。我没有这个(至少我不认为)它会位于哪里? @PassKit 还要检查你的 wwdr.pem 是最新的证书 - 苹果几年前改变了这个。 【参考方案1】:

查看您的签名和 pass.json,您的 pass 无法加载,因为用于创建签名的证书是:

PassTypeId:pass.com.apple.devpubs.example 团队名称:A93A5CM278

这与您的pass.json 中的passTypeIdentifierteamName 不匹配。

./models/certs/signerCert.pem./models/certs/signerKey.pem 替换为您的 PassTypeId 证书的正确证书和密钥,您应该可以克服此错误。

使用上面的pass.json 编译的任何传递仍然不会加载,但原因不同。 eventTicket 对象不能为空。

【讨论】:

以上是关于Swift PKPass 错误“无法读取通行证,因为它无效”的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 MobileSafari 将生成的 pkpass 添加到 iPhone

使用 .Net 为 Apple 存折创建可下载的通行证 (*.pkpass)

PKPass 无法识别 Passbook Web 服务 URL

已颁发的 PKPass 文件的签名证书到期的后果

无需应用程序即可远程更新 pkpass 文件

UIDocumentBrowserViewController 忽略 iOS13 设备上的文档类型(com.apple.pkpass)