pkpass 文件在 Safari Mac 中打开但在 iPhone 上不打开

Posted

技术标签:

【中文标题】pkpass 文件在 Safari Mac 中打开但在 iPhone 上不打开【英文标题】:pkpass file open in Safari Mac but not on iPhone 【发布时间】:2021-02-02 17:29:15 【问题描述】:

我成功创建了一个通行证,它在我的 MacBook Safari 浏览器上正常打开,但是,当我尝试在我的物理 iPhone 或模拟器上打开它时,它看起来好像 safari 无法识别通行证文件。

在 Mac Safari 上显示如下: Snapshot of how the pass looks when I download it on Mac Safari

在 iPhone 或 Simulator Safari 上显示如下: Sanpshot of how it look on iPhone or Simulator Safari

我检查了捆绑包中是否包含所有图像资产,并且使用了正确的 mime 类型(application/vnd.apple.pkpass),我什至将其添加到了我的服务器 mime.types conf 文件中。

为了解决这个奇怪的问题,还有什么需要检查的。

请求和响应标头:

URL: http://hayak.localhost/api/request/pass?locale=en-us&uuid=eb5b16db-2a33-4bc8-b3e6-b5dcc5240dfd
Request GET /api/request/pass HTTP/1.1 
Accept: */* 
Accept-Encoding: gzip, deflate
Accept-Language: en-us 
Host: hayak.localhost 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (Khtml, like Gecko) Version/14.0.2 Safari/605.1.15 Connection: keep-alive Referer: http://hayak.localhost/request/vw
Response HTTP/1.1 200 OK 
Content-Type: application/vnd.apple.pkpass
Keep-Alive: timeout=5, max=100 
Pragma: no-cache 
Content-Disposition: attachment; filename=CP_3_731585792.pkpass
Last-Modified: Tue, 02 Feb 2021 16:00:53 GMT
Cache-Control: public 
Date: Wed, 03 Feb 2021 15:18:46 GMT 
Content-Length: 302319 
Connection: Keep-Alive 
Accept-Ranges: bytes
Vary: Authorization 
X-Powered-By: php/7.4.12 Server: Apache/2.4.46 (Unix) OpenSSL/1.0.2u PHP/7.4.12 mod_wsgi/3.5 Python/2.7.13 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.11 Perl/v5.30.1 Content-Transfer-Encoding: binary X-RateLimit-Limit: 60 X-RateLimit-Remaining: 52 Content-Description: File Transfer

JS(Vue)代码:

methods: 
    
        forceFileDownload(response)
          const url = window.URL.createObjectURL(new Blob([response.data]))
          const link = document.createElement('a')
          link.href = url
          link.setAttribute('download', 'pass.pkpass') //or any other extension
          document.body.appendChild(link)
          link.click()
        ,
        downloadWithAxios()
        axios(
          method: 'get',
          url: '/request/pass',
          params: locale: this.$i18n.locale, uuid: this.$store.state.app.request.uuid,
          responseType: 'arraybuffer'
        )
        .then(response => 
          
          this.forceFileDownload(response)
          
        )
        .catch(() => console.log('error occured'))
        
    

PHP 代码:

$path = storage_path().'/passgenerator/'.$pass_identifier.'.pkpass';
        
        return response()->download($path, $pass_identifier.'.pkpass', 
        [
          'Content-Transfer-Encoding' => 'binary',
          'Content-Description' => 'File Transfer',
          'Content-Disposition' => 'attachment; filename=pass.pkpass',
          'Content-length' => strlen($pkpass),
          'Content-Type' => PassGenerator::getPassMimeType(),
          'Pragma' => 'no-cache',
        ]);

【问题讨论】:

是否在设备上下载?您可以发布通行证的链接以便我们可以复制吗?你能发帖curl -I https://pass url.pkpass 是的,它下载正常,预期大小相同(类似于 Mac 上的文件大小)。 很遗憾,我还不能放链接,因为我还在本地做这个。 你能用 curl 粘贴标题吗? 不知道如何从 curl 获取标题,但我尝试从 safari 控制台复制它。很抱歉回复晚了,因为我一整天都在远离我的电脑。见下文。 【参考方案1】:

由于你不是直接从服务器 URL 获取文件,而是通过axios 再次构造文件,因此在 PHP 中设置这些内容将无济于事:

'Content-Transfer-Encoding' => 'binary',
'Content-Description' => 'File Transfer',
'Content-Disposition' => 'attachment; filename=pass.pkpass',
'Content-length' => strlen($pkpass),
'Content-Type' => PassGenerator::getPassMimeType(),

您可以根据需要删除它们,因为我们只需要缓冲区数据。

问题是当你在前端再次构建文件时,你必须为其提供MINE类型application/vnd.apple.pkpass,这将有助于Safari识别pkpass文件并自动打开它。

所以,替换这一行:

const url = window.URL.createObjectURL(new Blob([response.data]))

这可能会有所帮助:

const url = window.URL.createObjectURL(
  new Blob([response.data], 
    type: "application/vnd.apple.pkpass",
  )
);

【讨论】:

以上是关于pkpass 文件在 Safari Mac 中打开但在 iPhone 上不打开的主要内容,如果未能解决你的问题,请参考以下文章

Java applet无法在Safari 7下打开文件(Mac OS X 10.9)

如何打开mac application support路径

从解压缩下载的文件中禁用 Safari

Mac 上的“没有 Safari 扩展证书”错误

Mac 教程系列如何在 Mac 上破解带有密码的 ZIP 压缩文件 ?

手机 safari mac 调试