简体   繁体   English

移动 Safari (iOS) 中的 javascript 不会下载日历邀请

[英]javascript in mobile safari (iOS) won't download calendar invite

I have a js client side application wherein I create a calendar event and want to download it to the client as an ics file.我有一个 js 客户端应用程序,我在其中创建了一个日历事件,并希望将其作为 ics 文件下载到客户端。 This works fine on desktop browsers, but mobile safari (and mobile chrome) on iOS refuse to download it.这在桌面浏览器上运行良好,但 iOS 上的移动 safari(和移动 chrome)拒绝下载它。

Here is the code I am using for the function:这是我用于该函数的代码:

let link = document.createElement('a')
// link.setAttribute('target', '_blank')
link.href = ics_string_from_indexedDB
link.download = 'Reminders.ics'
link.click()

When I use the code above and click on my link, mobile safari tells "This website is trying to show you a calendar invite. Do you want to allow this?"当我使用上面的代码并单击我的链接时,mobile safari 会提示“此网站正在尝试向您显示日历邀请。您要允许吗?” with an "Ignore" and "Allow" link.带有“忽略”和“允许”链接。 But clicking on the "Allow" link results in:但是点击“允许”链接会导致:

"Safari cannot download this file". “Safari 无法下载此文件”。

I have tried adding a target as suggested elsewhere (and commented out above), but that doesn't work either, and clicking on my link does nothing at all if present.我曾尝试按照其他地方的建议(并在上面注释掉)添加目标,但这也不起作用,如果存在,单击我的链接根本不执行任何操作。

I have also tried creating a blob, but safari refuses to download that in the exact same way.我也尝试过创建一个 blob,但 safari 拒绝以完全相同的方式下载它。

I have also tried base64 encoding, but again safari refuses (and gives a 404)我也尝试过 base64 编码,但 safari 再次拒绝(并给出 404)

Does anyone know of a way to allow iOS safari (client side) to download this file to the calendar?有谁知道允许 iOS Safari(客户端)将此文件下载到日历的方法?

side note: I can get this working if I send the ical stream from a server instead and set various headers, but I want to do this all on the client side and don't see why I should need a server, all of the correct ics info is present.旁注:如果我从服务器发送 ical 流并设置各种标头,我可以让它工作,但我想在客户端完成这一切并且不明白为什么我需要一个服务器,所有这些都是正确的ics信息存在。

Well, I solved this myself, here is what I learned:好吧,我自己解决了这个问题,这是我学到的:

Mobile Safari is VERY PICKY about what it considers to be a valid .ics file, and it simply won't recognize a file that it doesn't like. Mobile Safari 非常挑剔它认为是有效的 .ics 文件,它根本不会识别它不喜欢的文件。 Even though it is valid, and even though iCal and other apps on the desktop (and Safari on the desktop!) will recognize and import these ics files just fine.即使它是有效的,并且即使桌面上的 iCal 和其他应用程序(以及桌面上的 Safari!)将识别并导入这些 ics 文件也很好。

I used the same php generator I was using before on my server because it seems to make files in a format that Mobile Safari likes.我使用了我之前在服务器上使用的相同 php 生成器,因为它似乎以 Mobile Safari 喜欢的格式制作文件。 I don't have the time to exhaustively test which fields it requires and doesn't, but if I had to guess looking at the source, it is maybe timezone related or perhaps the formatting of the UID.我没有时间详尽地测试它需要哪些字段,哪些不需要,但是如果我不得不猜测查看源代码,它可能与时区相关,或者可能与 UID 的格式有关。 If I get the time after this project I will try to get some more specificity on this.如果我在这个项目之后有时间,我会尝试在这方面获得更多的特异性。

Once my format was all ok, blob downloading from my local indexedDB now works just fine:一旦我的格式一切正常,从我的本地 indexedDB 下载 blob 现在就可以正常工作了:

let blob = new Blob([ics_string_from_indexedDB], { type: 'text/calendar' })
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = 'Reminders.ics'
link.click()

I had similar problem, iPhone doesn't want to recognize .ics file.我有类似的问题,iPhone 不想识别 .ics 文件。 Problem was in charset=utf-8问题出在 charset=utf-8

let blob = new Blob([ics_string_from_indexedDB], { type: 'text/calendar;charset=utf-8' })

When I removed it当我删除它时

let blob = new Blob([ics_string_from_indexedDB], { type: 'text/calendar' })

it started working.它开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM