iPhone:如何让safari识别出vcard? [英] iPhone: how to get safari to recognize a vcard?

查看:247
本文介绍了iPhone:如何让safari识别出vcard?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个二维码,以便iPhone用户可以导入我的地址簿信息。我是这样做的:




  • 在我的网络服务器上放一个VCF(vcard)文件

  • 创建包含此URL的QR图像。



进行工作演示。该演示使用HTML5缓存清单,该清单将内容下载到支持iphone或HTML5的浏览器,以便在离线时使用。如果您想了解更多信息,请谷歌了解有关离线网络应用的详细信息。



VCALENDAR中的嵌入式VCARD for iPhone下载
您可能已经读过它无法使用Mobile Safari从网页直接将联系人文件(VCARD格式数据为.vcf文件)下载到您的iPhone。浏览器只是无法将.vcf扩展名和mime类型(text / x-vcard)识别为它应该处理的内容。顺便说一句,Android和大多数其他移动设备应该能够轻松处理VCARD文件 - 标准本身就像山丘一样古老!



你可能也读过通过请求用户的电子邮件地址,然后通过电子邮件发送联系人文件或创建指向Google Map条目的链接并从中提取联系信息(Google Map条目需要数周才能在英国获得),可以实现可行的工作。



虽然这些都是可行的解决方案,但它们并不是我称之为用户友好的方式,我试图让我的客户接受其中任何一个他们在移动网络应用程序上下载联系人。



现在我还要提到你可以下载一些增加对VCARD(.vcf)文件支持的第三方应用程序 - 例如QRAFTER和VCARD GETTER,来自Kerem Erkan优秀的QR阅读器和他关于该主题和iPad解决方案的博客,以及HIPSCAN vcard导入器。但是假设你的读者安装了这些应用程序是我认为的一步太远,所以我搜索了电子邮件,谷歌地图和第三方应用程序的替代解决方案。



之前我继续,有很多链接更详细地描述了这个问题:



堆栈溢出有几个关于主题的线程
强制vCard下载(thesheep.co.uk)
来自Code Train的EMAIL解决方案和相关博客vCard下载选项
此处还有一个可能的解决方案。 Dataplex的这篇文章也涵盖了这一点。
Jonas Schmid谈到正确提供文件类型。
MacRumours线程



然后我想,iPhone DOES支持从IOS5开始从网页下载的vcalendar文件。 VCALENDAR文件通常具有.ics扩展名,并由移动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:iphone@mobicontact.info
TEL; type = CELL; type = VOICE; type = pref:012 -345-6789
END:VCARD

创建一个新日历 - 按你喜欢的方式调用它,我在我的Mac区域使用vcal - 这样当您导出此日历以生成.ics文件,您获得的只是带有附加卡的单个事件,而不是您使用现有日历时可能拥有的所有事件。
创建一个新事件 - 称之为你喜欢的任何事件 - 并给它一个任意的时间和日期。
将(1)的VCARD文件附加到这个新事件 - 见截图。
将事件保存到日历中。
从BusyCal的主菜单中,将日历导出到本地磁盘上的.ics文件 - 在此处下载zip文件 - >附带联系人文件的Apple日历事件。
您现在可以使用自己喜欢的文本编辑器来检查Apple如何在日历事件中存储附件,结果是使用:ATTACH; VALUE = BINARY; ENCODING = BASE64; FMTTYPE = text / directory;
X-APPLE-FILENAME = iPhone Contact.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M ...等... [base64编码的VCARD]
所以我随后删除了所有我不需要的额外内容(这里是试验和错误),直到我有一个绝对最小值,仍然被iPhone识别为有附件的有效日历事件。这样做的原因是使在最终网页上创建日历事件的PHP文件尽可能简单 - 这是我将其减少到的最小值。



因此,正如我所说,以上所有内容都没有必要实现联系人下载解决方案 - 我只是想告诉您如何使用X-APPLE-FILENAME联系线了解Apple如何附加文件。当然,他们如何使用base64编码对VCARD数据进行编码。所以现在我们拥有了动态创建VEVENT所需的所有信息,并将VCARD附加到它们上面,可以通过Mobile Safari直接下载到iPhone。



1)上传您要下载的联系人文件(.vcf) - 您需要此文件,以便您可以将其直接下载到非iphone或base64为iPhone编码。



2)创建一个PHP文件的链接,该文件将动态生成日历事件,例如:
下载可能在以下HTML5移动应用程序中联系iPhone:



3)使用iphonecontact-source-code中的代码创建或上传vcal.php。此PHP文件为要下载的日历文件应用正确的标题/内容类型,然后您可以选择直接获取日历文件内容iphonecontact.ics,如vcal-from-file.php中所做,或生成飞行中的日历,如vcal.php所示。后者是我首选的方法,因为你得到一个很好的带时间戳的日历事件,显示下载的时间和日期。



4)就是这样!你们都已经设置好了 - 现在浏览到iphone上的网页,然后点击链接执行vcal.php。您的浏览器现在应该显示iphonecontact.ics文件,并要求您在手机中打开它日历应用程序



5)选择打开...,您将看到日历约会和附加的联系人文件。



6)请注意我如何将日历事件的标题设置为有用的信息,告诉用户如何处理嵌入的联系人文件(您可以在vcal.php中看到为事件设置SUMMARY字段的行)。现在点击附加的联系人文件...



7)然后创建新联系人,你几乎就在那里......



保存联系人并诅咒苹果让你跳过的篮球!



现在根据我在HTML5网络应用上使用这种技术的经验,我想提一下几点:



使用缓存清单 - 我有一些意外的行为/问题服务于日历文件(iphonecontact.ics),如果它已被缓存 - 我只是无法让它工作,所以我从清单中排除它,这意味着它总是下载 - 源代码包含在这个zip文件中iphonecontact-source-code。



你当然可以做一些用户代理嗅探来检测vcal.phpPHP文件是否将VCALENDAR文件提供给iPhone和VCARD文件本身适用于所有其他浏览器。它是一个简单的检查,并通过谷歌搜索记录良好,所以如果你需要,我会留下你想出来,并随时在这里发布代码。



我认为就是这样 - 所以总结一下:



Mobile safari不直接支持VCARD(.vcf)文件但是支持VCALENDAR(.ics)文件。




  • 当前最佳解决方案是通过请求用户电子邮件地址或嵌入联系谷歌地图链接或下载处理VCARDS的应用程序。

  • Apple确实支持日历文件的附件,但不容易,所以一旦我们知道如何完成,我们就可以用PHP来完成。

  • 将VCARD嵌入到VCALENDAR文件中,以允许用户只需额外点击一两次就可以将联系人保存到他们的地址簿中。



我希望你喜欢这个解决方案 - 它就像我认为的那样好,直到Apple放松并允许Mobile Safari加入pt VCARD文件。



直到下次......


I'm trying to create a QR code so that iPhone users can import my address book information. I'm doing this by:

  • putting a VCF (vcard) file on my web server
  • creating a QR image that contains this URL.

http://markharrison.net/mh-vcf-small.png

This is working on my desktop browser (it opens the vcard with the address book app).

On the iPhone, the QR reader successfully tells safari to access the vcard, but then safari complains it does not know how to handle the vcard. I've confirmed that Content-Type: text/x-vcard is being sent.

So, my questions:

  • How do I get Safari to recognize my vcard?
  • Is there another card format that safari recognizes?

解决方案

[UPDATED - Sep 2013 - iOS7 now supports direct download of VCARD files and import into the native Contacts application]

function isiOS7($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'OS 7') !== FALSE);
}

# Output file contents - simple version
if(!isIphone() || isiOS7()) {
    # Send correct headers      
    header("Content-type: text/x-vcard; charset=utf-8"); 
                // Alternatively: application/octet-stream
                // Depending on the desired browser behaviour
                // Be sure to test thoroughly cross-browser

    header("Content-Disposition: attachment; filename=\"iphonecontact.vcf\";");
    # Output file contents 
    echo file_get_contents("iphonecontact.vcf");
    exit();
}

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:iphone@mobicontact.info 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.

You could of course do some user agent sniffing to detect in the "vcal.php" PHP file whether to serve the VCALENDAR file to an iPhone and the VCARD file itself to all other browsers. Its a simple enough check and well documented via a google search so I'll leave it to you to figure that out if you need to and feel free to post the code here if you do.

I think that's about it - so to summarise:

Mobile safari doesn't support VCARD (.vcf) files directly but does support VCALENDAR (.ics) files.

  • Current best solutions are to email the contact by requesting the users email address OR to embed the contact in a google map link OR download an app that handles VCARDS.
  • Apple does support attachments to calendar files but not easily so once we know how this is done we can do it in PHP.
  • Embed a VCARD into a VCALENDAR file to allow a user to save a contact to their address book with just an extra click or two.

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...

这篇关于iPhone:如何让safari识别出vcard?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆