如何从网页将联系人添加到 iPhone 的地址簿?
在桌面 Mac 世界中,如果我单击网页中链接到 VCF 文件的链接,该文件将在地址簿应用程序中打开。然而,在 iPhone 世界中,这样做会导致显示消息“下载失败。Safari 无法下载此文件”。所以,我的问题是,是否真的可以从网页将联系人添加到 iPhone 的地址簿中?
我拥有原始 VCF 文件和 hCard 微格式版本的数据,因此该解决方案不必与下载原始文件相关 - 如果需要,我可以重新处理数据。唯一的要求是从 Mobile Safari 中查看的网页将联系人添加到 iPhone 的地址簿中。
那么,有什么想法吗?
In the desktop Mac world, if I were to click on a link in a web page which linked to a VCF file, this file would be opened in the Address Book application. In the iPhone world, however, doing that results in the message "Download failed. Safari cannot download this file" being displayed. So, my question is, is it actually possible to add a contact to your iPhone's Address Book from a web page?
I have both the original VCF file and an hCard microformatted version of the data available to me, so the solution does not have to be tied to downloading the original file - I'm able to reprocess the data if necessary. The only requirement is to add the contact to the iPhone's Address Book from a webpage being viewed from within Mobile Safari.
So, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我可以使用以下资源在网络应用程序上实现此功能:
vCard
“helloWorld.txt”
替换为vCard.getFormattedString()
。文件名(即name
参数)当然也应该以.vcf
结尾。已确认适用于 Android、IOS 和 Outlook。
I was able to get this working on a web app using the following resources:
vCard
"helloWorld.txt"
withvCard.getFormattedString()
. the filename (i.e.name
argument) should of course end in.vcf
as well.Confirmed working on Android, IOS and Outlook.
根据参考库,将联系人添加到 iPhone 的地址簿是不可能的。
可用的方案有:邮件、电话、文本、地图、YouTube 和 iTunes 链接。
Iphone URL 方案参考
According to the reference library adding contact to the iPhone's Address Book is not possible..
The available schemes are: mail, phone, text, map, youtube and iTunes links.
Iphone URL Scheme Reference
[更新 - 2013 年 9 月 - iOS7 现在支持从我们的页面直接下载 VCARD 并导入本机联系人应用程序,因此您只需要下面的解决方案,而 iOS6 及以下版本仍然不支持它 - 由于 Apple 已接受,将来可能会发生变化功能]
[2013 年 1 月更新 - 现在添加了用户代理嗅探并合并到一个 download.php 文件中,该文件检测 iOS、Mobile Safari、UiWebView 场景以防止“Farme Load Interrupted”错误,并将 .vcf 文件提供给所有 bnut Apple将 .vcf 嵌入到 .ics 中的设备。]
查看更新的完整解决方案
下面是通过 Mobile Safari 将联系人从网页下载到 iPhone 的替代方法的完整描述。基本思想是将所需的联系信息作为文件附加到由 mobile safari 处理的日历事件中。联系人文件本身使用简单的 PHP 脚本在日历文件中动态进行 base64 编码。所以请继续阅读...
只是想要源代码吗?在此处下载 [iphone-contact-download-demo],以获得完整运行的 HTML5 Web 应用程序,您可以自由复制和复制或访问 http://iphone.mobicontact.info 查看工作演示。该演示使用 HTML5 缓存清单,将内容下载到您的 iPhone 或支持 HTML5 的浏览器,以便在离线时使用。如果您想了解更多信息,请谷歌搜索有关“离线网络应用程序”的更多详细信息。
VCALENDAR 中嵌入 VCARD for iPhone 下载
您可能已经了解到,无法使用 Mobile Safari 从网页将联系人文件(VCARD 格式数据作为 .vcf 文件)直接下载到您的 iPhone。浏览器只是无法将 .vcf 扩展名和 mime 类型 (text/x-vcard) 识别为它应该处理的内容。顺便说一句,Android 和大多数其他移动设备应该能够足够轻松地处理 VCARD 文件 - 标准本身就像山一样古老!
您可能还读过,可以通过请求用户的电子邮件地址,然后通过电子邮件将联系文件发送给他们,或者创建指向 Google 地图条目的链接并从中提取联系信息(Google 地图条目需要数周时间才能获得)来实现可行的目标。在英国)。
虽然这些都是可行的解决方案,但它们并不是我所说的用户友好型解决方案,我尝试让我的客户接受其中任何一个以在移动网络应用程序上下载联系人信息,但没有成功。
现在,我还应该提到,您可以下载一些添加对 VCARD (.vcf) 文件支持的第三方应用程序 - 例如 QRAFTER 和 VCARD GETTER,它们均来自 Kerem Erkan 出色的 QR 阅读器及其关于该主题和 iPad 解决方案的博客,和 HIPSCAN vcard 导入器。但在我看来,假设你的读者安装了这些应用程序就太过分了,所以我寻找电子邮件、谷歌地图和第三方应用程序的替代解决方案。
在我继续之前,有许多链接更详细地描述了该问题:
堆栈溢出有几个关于该主题的线程
强制下载 vCard (thesheep.co.uk)
EMAIL 解决方案和相关博客可从 Code Train 此处下载 vCard 选项
这里还有一个使用谷歌地图的可能解决方案。 Dataplex 的这篇文章也涵盖了这一点。
Jonas Schmid 谈论如何正确提供文件类型。
MacRumours 线程
然后我开始思考,从 IOS5 开始,iPhone 确实支持从网页下载 vcalendar 文件。 VCALENDAR 文件通常具有 .ics 扩展名,并由 mobile safari 处理,并打开一个窗口,可以在其中打开文件并将其保存到日历中。我觉得令人难以置信的是,Apple 和 Mobile Safari 支持 VCAL 文件,但不支持 VCARD 文件,但事实就是如此。那么如果我可以将 VCARD 文件附加到 VCALENDAR 文件呢?
第一个障碍是在日历事件上获取附件...
最初,我尝试在 Windows 中向 Outlook 日历约会添加附件,但从网页链接时可能无法正确下载,并且肯定没有产生我想要的结果。所以,我决定尝试使用 Apple 软件,毕竟我们正在尝试下载到 iPhone 上。使用 Apple 的默认日历应用程序“iCal”会带来一个基本问题 - 您无法向事件/日历约会添加附件!因此,我在谷歌上搜索了“在 OSX 中向日历事件添加附件”或类似内容,并找到了这篇优秀的文章,它为我指明了正确的方向。
在 OSX 中向日历约会添加附件。
几分钟后,安装了 BusyCal 的副本后,我就能够创建一个事件并向其附加 VCARD 文件(之前从我的联系人中保存/导出)在 OSX 中)。此步骤仅需要了解将附件添加到日历项目时创建的文件的格式 - 您不需要安装 BusyCal 来实现下面描述的解决方案,但我将其包含在内以供参考,以便您可以了解 VCARD 是如何实现的嵌入在 VCALENDAR/VEVENT (.ics) 文件中。
我使用的步骤是在运行 OSX Lion 的 Apple iMac 上进行的:
从联系人/地址簿导出联系人以创建 VCARD 文件 (.vcf) - 您可以使用文本编辑器编辑此文件以删除所有额外的内容,例如UID 和 PROD-ID(如果您愿意)。BEGIN:VCARD
版本:3.0
N:联系人;iPhone;;;
FN:iPhone 联系方式
EMAIL;type=INTERNET;type=WORK;type=pref:[电子邮件受保护]
电话;类型=手机;类型=语音;类型=首选项:012-345-6789
结束:V卡
创建一个新日历 - 随意称呼它,我在“在我的 Mac 上”区域中使用了“vcal”,这样当您导出此日历以生成 .ics 文件时,您得到的只是带有附加卡片的单个事件而不是使用现有日历时可能发生的所有事件。
创建一个新事件 - 随意命名 - 并指定任意时间和日期。
将 (1) 中的 VCARD 文件附加到此新事件 - 请参阅屏幕截图。
将事件保存到日历中。
从 BusyCal 的主菜单中,将日历导出到本地磁盘上的 .ics 文件 - 在此处下载 zip 文件 --> Apple 日历活动并附有联系人文件。
现在,您可以使用您最喜欢的文本编辑器来检查 Apple 如何在日历事件中存储附件,结果是使用:ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
X-APPLE-FILENAME=iPhone 联系人.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M...等...[base64 编码的 VCARD]
因此,我然后删除了所有不需要的额外内容(此处反复试验),直到获得 iPhone 仍将其识别为带附件的有效日历事件的绝对最小值。这样做的原因是为了使在最终网页上创建日历事件的 PHP 文件尽可能简单 - 这是我将其简化为的 vcal-minimal。
因此,正如我所说,上述所有内容都不是实现联系人下载解决方案所必需的 - 我只是想向您展示我如何了解 Apple 如何使用 X-APPLE-FILENAME 联系线附加文件。当然,还有他们如何使用 Base64 编码对 VCARD 数据进行编码。现在我们已经拥有了即时创建 VEVENT 并将 VCARD 附加到其上所需的所有信息,这些信息可以通过 Mobile Safari 直接下载到 iPhone。
1) 上传您想要下载的联系人文件 (.vcf) - 您将需要此文件,以便您可以将其直接下载到非 iPhone 或针对 iPhone 对其进行 Base64 编码。
2) 创建一个指向 PHP 文件的链接,该文件将动态生成日历事件,例如:
也许可以在 HTML5 移动应用程序中下载 Contact iPhone,如下所示:
3) 使用此处的代码创建或上传 vcal.php iphonecontact -源代码。此 PHP 文件为要下载的日历文件应用正确的标头/内容类型,然后您可以选择直接获取日历文件内容“iphonecontact.ics”,如“vcal-from-file.php”中所做的那样,或者生成动态日历如“vcal.php”所示。后者是我的首选方法,因为您会得到一个很好的带时间戳的日历事件,显示下载的时间和日期。
4)就是这样!一切就绪 - 现在在您的 iPhone 上浏览网页并单击链接来执行“vcal.php”。您的浏览器现在应该显示 iphonecontact.ics 文件并要求您在手机日历应用程序中打开它
5)选择“打开方式...”,您将看到日历约会并所附的联系文件。
6) 请注意我如何将日历事件的标题设置为有用的内容,以告诉用户什么处理嵌入的联系人文件(您可以在 vcal.php 中看到为事件设置 SUMMARY 字段的行)。现在单击附加的联系文件...
7) 然后“创建新联系人”,您就快到了...
保存联系人并在苹果让您跳过的圈子里低声咒骂!
现在,根据我在 HTML5 Web 应用程序上使用此技术的经验,我想在此提及几点:
使用缓存清单 - 我在提供日历文件 (iphonecontact.ics) 时遇到了一些意外的行为/问题,如果它已被缓存 - 我只是无法让它工作,所以我将其从清单中排除,这意味着它总是被下载 - 源代码包含在这个 zip 文件 iphonecontact-source-code 中。
我添加了一些用户代理嗅探,以检测“vcal.php”或任何您称之为(最新在线示例中的 download.php)PHP 文件是否将 VCALENDAR 文件提供给 iPhone,并将 VCARD 文件本身提供给所有其他浏览器。
另外要小心,如果通过 UiWebView 应用程序查看,那么我会将用户重定向到一个显示“使用 Mobile Safari 打开”的页面(正如我博客上的某人建议的那样),否则您会收到可怕的“帧加载中断”错误。
我认为就是这样 - 总结一下:
Mobile safari 不直接支持 VCARD (.vcf) 文件,但支持 VCALENDAR (.ics) 文件。
我希望您喜欢这个解决方案 - 在 Apple 做出让步并允许 Mobile Safari 接受 VCARD 文件之前,它已经是我认为最好的解决方案了。
下次见...
[UPDATED - Sep 2013 - iOS7 now supports direct download of VCARDs from we page and import into native contact application so you only need the solution below whilst iOS6 and below still do not support it - that may change in future now that Apple have accepted the feature ]
[UPDATED JAN 2013 - now have added the user agent sniffing and combined into a single download.php file which detects iOS, Mobile Safari, UiWebView scenarios to prevent "Farme Load Interrupted" errors and serves the .vcf file to all bnut Apple devices which get the .vcf embedded in the .ics.]
See the updated complete solution
What follows is a full description of an alternative method for downloading contacts from a web page to an iPhone via Mobile Safari. The basic idea is to attach the required contact information as a file in a calendar event which IS handled by mobile safari. The contact file itself is base64 encoded in the calendar file on the fly using a simple PHP script. So read on...
Just want the source code? Download it here [iphone-contact-download-demo] for a fully working HTML5 Web App that you are free to copy and reproduce or go to http://iphone.mobicontact.info for a working demo. The demo uses the HTML5 cache manifest which downloads the content to your iphone or HTML5 supporting browser for use when offline. Please google for more details about "offline web apps" if you want to know more.
Embedded VCARD in VCALENDAR for iPhone download
You may have read that it is not possible to download contact files (VCARD format data as .vcf file) direct to your iPhone from a web page using Mobile Safari. The browser just does not recognise the .vcf extension and mime type (text/x-vcard) as something it should handle. As an aside, Android and most other mobile devices should be able to handle VCARD files easily enough - the standard itself is as old as the hills!
You may also have read that it IS possible to achieve something workable by requesting the users email address and then email them the contact file OR creating a link to a Google Map entry and extract the contact information from that (Google Map entry requires weeks to attain in the UK).
Whilst these are both viable solutions, they are not what I would call user friendly and I tried, unsuccessfully, to get a client of mine to accept either one of them for their contact download on a mobile web app.
Now at this point I should also mention that you CAN download some third party apps that add support for VCARD (.vcf) files - such as QRAFTER and VCARD GETTER both from Kerem Erkan's excellent QR reader and his blog on the subject and iPad solution, and HIPSCAN vcard importer. But assuming your readers have these apps installed is one step too far in my opinion so I searched for an alternative solution to email, google maps and third party apps.
Before I continue, there are many links describing the problem in more detail:
Stack overflow has several threads on the subject
Forcing vCard download (thesheep.co.uk)
The EMAIL solution and associated blog from the Code Train here vCard options for download
There is also a possible solution using Google Maps here. And this is also covered by this article from Dataplex.
Jonas Schmid talks about serving the file types correctly.
MacRumours thread
Then I got thinking, iPhone DOES support vcalendar files downloaded from a webpage as of IOS5. The VCALENDAR files usually have a .ics extension and are handled by mobile safari bringing up a window where the file can be opened and saved to the calendar. I find it unbelievable that Apple and Mobile Safari support VCAL files but not VCARD files but that is just how it is. So what IF I could attach a VCARD file to a VCALENDAR file?
First obstacle is getting an attachment on a calendar event...
Initially I tried adding an attachment to an Outlook calendar appointment in Windows which whilst possible did not download correctly when linked from a webpage and certainly didn't yield the result I was after. So, I decided to try using Apple software as, after all, we are trying to download onto an iPhone. Using Apple's default calendar application "iCal" presents one fundamental problem - you can't add attachments to an event/calendar appointment! So I googled "adding attachments to calendar events in OSX" or similar and found this excellent article which pointed me in the right direction.
Adding attachments to calendar appointments in OSX.
So a few minutes later and with a copy of BusyCal installed, I was able to create an event and attach a VCARD file to it (previously saved/exported from my contacts in OSX). This step is only necessary to understand the format of the file created when an attachment is added to a calendar item - you DO NOT need to install BusyCal to implement the solution described below but I include it for reference so that you can see how the VCARD is embedded in the VCALENDAR/VEVENT (.ics) file.
The steps I used were on an Apple iMac running OSX Lion:
Export a contact from your Contacts/Address Book to create a VCARD file (.vcf) - you can edit this file with a text editor to strip out all the extra stuff such as UID and PROD-ID if you like.BEGIN:VCARD
VERSION:3.0
N:Contact;iPhone;;;
FN:iPhone Contact
EMAIL;type=INTERNET;type=WORK;type=pref:[email protected]
TEL;type=CELL;type=VOICE;type=pref:012-345-6789
END:VCARD
Create a new calendar - call it what you like, I used "vcal"- in the "On my Mac" area so that when you export this calendar to generate the .ics file, all you get is the single event with the attached card rather than all the events you might have if you use an existing calendar.
Create a new event - call it anything you like - and give it an arbitrary time and date.
Attach the VCARD file from (1) to this new event - see screenshot.
Save the event to the calendar.
From the main menu of BusyCal, export the calendar to an .ics file on your local disk - download the zip file here --> Apple calendar event with attached contact file.
You can now use your favourite text editor to examine how Apple store attachments in calendar events and the result is using:ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
X-APPLE-FILENAME=iPhone Contact.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M...etc... [base64 encoded VCARD]
So I then stripped out all the extra stuff I didn't need (trial and error here) until I had the absolute minimum that was still recognised by the iPhone as a valid calendar event with attachment. The reason for doing this is to make the PHP file that creates the calendar event on the final webpage as easy as possible - here is the vcal-minimal of what I reduced it to.
So all of the above, as I said, is not necessary to implement the contact download solution - I just wanted to show you how I got to understanding how Apple attach files using the X-APPLE-FILENAME contact line. And of course, how they encode the VCARD data using base64 encoding. So now we have all the info we need to create VEVENTs on the fly and attach VCARDs to them that can be downloaded direct to an iPhone via Mobile Safari.
1)Upload the contact file (.vcf) you want to be downloaded - you'll need this file so that you can either download it direct to non-iphones or base64 encode it for the iPhone.
2)Create a link to a PHP file that will generate the calendar event on the fly such as:
Download Contact iPhone perhaps in a HTML5 mobile app like below:
3)Create or upload the vcal.php with the code here iphonecontact-source-code. This PHP file applies the correct header/content type for the calendar file to be downloaded then you have a choice of either getting the calendar file contents directly "iphonecontact.ics" as done in "vcal-from-file.php" or generating the calendar on the fly as shown in "vcal.php". The latter is my preferred method because you get a nice timestamped calendar event showing the time and date of the download.
4)That's it! You're all set - now browse to the web page on your iphone and click the link to execute "vcal.php". Your browser should now show the iphonecontact.ics file and ask you to open it in the phones Calendar application
5)Select "Open in..." and you will be presented with the calendar appointment and the attached contact file.
6)Notice how I set the title of the calendar event to something useful to tell the user what to do with the embedded contact file (you can see the line in vcal.php that sets the SUMMARY field for the event). So now click the attached contact file...
7)And then "Create New Contact" and you're nearly there...
Save the contact and curse under your breath at the hoops Apple made you jump through!
Now there are a couple of points I'd like to mention here based on my experiences using this technique on HTML5 web apps:
Using the cache manifest - I had some unexpected behaviour/problems serving the the calendar file (iphonecontact.ics) if it had been cached - I just couldn't get it to work so I exclude it from the manifest which means it is always downloaded - source code is included in this zip file iphonecontact-source-code.
I have added some user agent sniffing to detect in the "vcal.php" or whatever you call it (download.php in latest online example) PHP file whether to serve the VCALENDAR file to an iPhone and the VCARD file itself to all other browsers.
Also be careful, if viewed through a UiWebView application then I redirect the user to a page that says open with Mobile Safari (as suggested by someone on my blog) otherwise you get the dreaded "Frame Load Interrupted" error.
I think that's about it - so to summarise:
Mobile safari doesn't support VCARD (.vcf) files directly but does support VCALENDAR (.ics) files.
I hope you like this solution - it is as good as I think we are going to get until Apple relent and allow Mobile Safari to accept VCARD files.
Until next time...
虽然稍微超出了我最初的要求范围,但我找到了一个可以解决的问题的解决方案。
事实证明,如果您收到一封电子邮件,其中包含 vcard 作为附件,那么您可以打开此附件并将其添加到您的地址簿中。因此,如果不是向 iPhone 用户提供 vcard 下载链接,而是为他们提供通过电子邮件发送 vcard 的方式,那么他们就可以将该卡添加到他们的地址簿中。
这是一个稍微迂回的解决方案,但大约是我能想到的最好的解决方案,而苹果不允许你在本地这样做。当然,这比什么都不做更好。
我最终写了一篇关于此的博客文章:从网页将 vCard 添加到您的 iPhone 地址簿
Whilst slightly outside of the scope of my original requirements, I have found a solution to the problem that I can work with.
As it turns out, if you receive an email with a vcard in it as an attachment, then you are able to open this attachment and add it to your address book. So, if instead of offering a vcard download link to iPhone users you provide them with a way to be sent the vcard via email instead then they are able to add the card to their address book.
A slightly roundabout solution, but about the best I'm able to come up with whilst Apple don't allow you to do it natively. Certainly it's a better solution than doing nothing at all.
I ended up writing a blog post about this: Adding a vCard to your iPhone Address Book from a web page
对于 Google 员工,截至这篇文章,在 iOS 7(beta 6)中可以直接在联系人应用程序中打开文件并保存。
For Googlers, as of This post, in iOS 7 (beta 6) The file can be opened directly into the contacts app and saved.