Creating a vCard in iPhone
I'm trying to create a vCard representation in the iPhone. I created a string representation for the vCard. I'm not sure how to convert it into the NSData form in order to mail it as an attachment. This is what I have so far:
NSString *vCardString = [vCard getVCFString]; // retur开发者_运维知识库ns string representation for vCard
NSData *vCardData = [vCardString dataUsingEncoding:NSUTF8StringEncoding];
[mailController addAttachmentData:vCardData mimeType:@"text/x-vcard" fileName:@"LocationInfo"];
When I click on the attachment when I do a test email, it goes to the create new contact/add as existing contact. Is this correct since the iPhone recognizes it as a contact? I guess I was interested in getting the location information out of it, but that didn't seem to show up in my attachment. The code for creating my VCF representation is:
vcfString = [[NSMutableString allocWithZone:[self zone]] initWithCapacity:kDefaultStringSize];
[vcfString appendString:@"BEGIN:VCARD\n"];
[vcfString appendString:@"VERSION:3.0\n"];
if (s) {
NSMutableString *aString = [[NSMutableString alloc] initWithFormat:@"%@;", s];
[vcfString appendString:aString];
[aString replaceOccurrencesOfString:@";" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [aString length])];
street = [[NSString allocWithZone:[self zone]] initWithString:aString];
[aString release];
}
if (c) {
NSMutableString *aString = [[NSMutableString alloc] initWithFormat:@"%@;", c];
[vcfString appendString:aString];
[aString replaceOccurrencesOfString:@";" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [aString length])];
city = [[NSString allocWithZone:[self zone]] initWithString:aString];
[aString release];
}
if (st) {
NSMutableString *aString = [[NSMutableString alloc] initWithFormat:@"%@;", st];
[vcfString appendString:aString];
[aString replaceOccurrencesOfString:@";" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [aString length])];
state = [[NSString allocWithZone:[self zone]] initWithString:aString];
[aString release];
}
// TODO: look up semicolon for VCard representation
if (z) {
NSString *aString = [[NSString alloc] initWithFormat:@"%@\n", z];
[vcfString appendString:aString];
zip = [[NSString allocWithZone:[self zone]] initWithString:aString];
[aString release];
}
[vcfString appendString:@"END:VCARD"];
Totally agree with Dave DeLong's answer. Just want to add that since I simply wanted to create a vCard file (.vcf) and attach it to an in-app email action what I did was create a string, then a temporary file, with all the vCard fields I had data for.
In my case I made a method in my NSManagedObject subclass to return a -vCardRepresentation string.
An Objective-c category or simple framework would be a great help - I might do it when time.
The Wikipedia page and the formal spec for vCard (3.0) really help.
UPDATE 2: I'm rushing to get an app complete, but because I did want to create vCard data and add it as attachments to in-app mail messages in several places I've now made a separate class controller that makes / hides the details of the vCard syntax. Then I just have a method that returns an NSData version of the vCard string to add directly to a mail message as an attachment. This is a much cleaner solution, you don't need to create any files, even if they were only temporary. Plus you don't really need an NSString representation of the data anyway, unless you did want to create a file and use it more than once to save re-making the data?
My controller class has a number of -setXXXX methods that add values to a dictionary where the keys are strings with the vCard field names, like FN, TEL and ADR. Then when I call its -vCardRepresentation, this now returns an NSData object (built by scanning the dictionary) that can be used directly in MFMailComposeViewController's -addAttachmentData:mimeType:fileName: method.
If I can tidy my code, so it's generic enough, I'll post it later.
UPDATE: Here's my code, it might help someone get started:
- (NSString *)vCardRepresentation
{
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
[mutableArray addObject:@"BEGIN:VCARD"];
[mutableArray addObject:@"VERSION:3.0"];
[mutableArray addObject:[NSString stringWithFormat:@"FN:%@", self.name]];
[mutableArray addObject:[NSString stringWithFormat:@"ADR:;;%@",
[self addressWithSeparator:@";"]]];
if (self.phone != nil)
[mutableArray addObject:[NSString stringWithFormat:@"TEL:%@", self.phone]];
[mutableArray addObject:[NSString stringWithFormat:@"GEO:%g;%g",
self.latitudeValue, self.longitudeValue]];
[mutableArray addObject:[NSString stringWithFormat:@"URL:http://%@",
self.website]];
[mutableArray addObject:@"END:VCARD"];
NSString *string = [mutableArray componentsJoinedByString:@"\n"];
[mutableArray release];
return string;
}
Obviously I'm making reference to properties in my class, plus a method called -addressWithSeparator to build-up the data for the address, which has to include the ; separator even for optional address fields.
精彩评论