赶知识网

php创建iphone可识别的联系人名片

2013-07-18 / 10640次点击 php/mysql/apache

iphone并不直接支持vcf文件导入,下面是老外写的方法,用vcalendar套vcf的方式,支持iphone直接导入到通讯录。需要注意的是,vcard版本必须使用3.0,否则iphone导入后中文都是乱码,乱码和vcard本身的编码无关。
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...

Basic layout of embedded VCARD in VCALENDAR file

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 downloadYou 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 subjectForcing vCard download (thesheep.co.uk)The EMAIL solution and associated blog from the Code Train here vCard options for downloadThere 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:VCARDVERSION:3.0N:Contact;iPhone;;;FN:iPhone ContactEMAIL;type=INTERNET;type=WORK;type=pref:iphone@mobicontact.infoTEL;type=CELL;type=VOICE;type=pref:012-345-6789END:VCARDCreate 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.BusyCal on iMac allows attachements to VCALENDAR appointmentsSave 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:Simple link to the PHP file to download the contact within VCALENDAR

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 applicationCalendar appointment downloaded from web page

5)Select "Open in..." and you will be presented with the calendar appointment and the attached contact file.Calendar event with 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...Opened contact file

7)And then "Create New Contact" and you're nearly there...Contact SAVED!!!

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.

I have just published an alternative solution on my blog which describes how to attach the contact file as an attachment to a calendar file which is handled by mobile safari from iOS5 onwards.

http://mobicontact.info/iphone/download-contact-from-web-page/

The blog shows complete solution including source code and images of the whole process and as such is a lot easier to read than what I can put here on Stack Overflow. The main point to note is that Apple use :

ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;X-APPLE-FILENAME=iPhone Contact.vcf:QkVHSU46VkNBUkQNClZFUlNJT046Metc [base64 encoded VCARD]

for embedded VCARD in VCALENDAR files. Create a VCALENDAR file and then base64 encode your VCARD within it - code snippet below (full details on my blog)

# Send correct headers     
header("Content-type: text/x-vcalendar; 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.ics\";");
# Output file contents - simple version
#echo file_get_contents("iphonecontact.ics");

# Generate file contents - advanced version
# BEGIN:VCALENDAR
# VERSION:2.0
# BEGIN:VEVENT
# DTSTART;TZID=Europe/London:20120617T090000
# DTEND;TZID=Europe/London:20120617T100000
# SUMMARY:iPhone Contact
# DTSTAMP:20120617T080516Z
# ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
#  X-APPLE-FILENAME=iphonecontact.vcf:
#  QkVHSU46VkNBUkQNClZFUlNJT046My4wDQpOOkNvbnRhY3Q7aVBob25lOzs7DQpGTjppUGhvbm
#  UgQ29udGFjdA0KRU1BSUw7VFlQRT1JTlRFUk5FVDtUWVBFPVdPUks6aXBob25lQHRoZXNpbGlj
#  b25nbG9iZS5jb20NClRFTDtUWVBFPUNFTEw7VFlQRT1WT0lDRTtUWVBFPXByZWY6KzQ0MTIzND
#  U2Nzg5MA0KRU5EOlZDQVJE
# END:VEVENT
# END:VCALENDAR

echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "BEGIN:VEVENT\n";
echo "SUMMARY:Click attached contact below to save to your contacts\n";
$dtstart = date("Ymd")."T".date("Hi")."00";
echo "DTSTART;TZID=Europe/London:".$dtstart."\n";
$dtend = date("Ymd")."T".date("Hi")."01";
echo "DTEND;TZID=Europe/London:".$dtend."\n";
echo "DTSTAMP:".$dtstart."Z\n";
echo "ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;\n";
echo " X-APPLE-FILENAME=iphonecontact.vcf:\n";
$vcard = file_get_contents("iphonecontact.vcf");        # read the file into memory
$b64vcard = base64_encode($vcard);                      # base64 encode it so that it can be used as an attachemnt to the "dummy" calendar appointment
$b64mline = chunk_split($b64vcard,74,"\n");             # chunk the single long line of b64 text in accordance with RFC2045 (and the exact line length determined from the original .ics file exported from Apple calendar
$b64final = preg_replace('/(.+)/', ' $1', $b64mline);   # need to indent all the lines by 1 space for the iphone (yes really?!!)
echo $b64final;                                         # output the correctly formatted encoded text
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>


Top10

沪ICP备09053415号 © 赶知识网