gpt4 book ai didi

iphone - 如何使用 ios sdk 从地址簿创建 vCard?

转载 作者:可可西里 更新时间:2023-11-01 05:37:31 25 4
gpt4 key购买 nike

我不知道如何从地址簿创建 vCard。我可以通过搜索为 vCard 数据生成 NSString,但我不知道如何将 NSString 转换为 .vcf。这是我的 vCard 数据的 NSString 代码。

NSString *vcard = @"BEGIN:VCARD\nVERSION:3.0\n";

// Name
vcard = [vcard stringByAppendingFormat:@"N:%@;%@;%@;%@;%@\n",
(contact.lastname ? contact.lastname : @""),
(contact.firstname ? contact.firstname : @""),
(contact.middlename ? contact.middlename : @""),
(contact.prefix ? contact.prefix : @""),
(contact.suffix ? contact.suffix : @"")
];

vcard = [vcard stringByAppendingFormat:@"FN:%@\n",contact.compositeName];
if( contact.nickname ) vcard = [vcard stringByAppendingFormat:@"NICKNAME:%@\n",contact.nickname];
if( contact.firstnamephonetic ) vcard = [vcard stringByAppendingFormat:@"X-PHONETIC-FIRST-NAME:%@\n",contact.firstnamephonetic];
if( contact.lastnamephonetic ) vcard = [vcard stringByAppendingFormat:@"X-PHONETIC-LAST-NAME:%@\n",contact.lastnamephonetic];


// Work
if( contact.organization || contact.department ) vcard = [vcard stringByAppendingFormat:@"ORG:%@;%@\n",(contact.organization?contact.organization:@""),(contact.department?contact.department:@"")];
if( contact.jobtitle ) vcard = [vcard stringByAppendingFormat:@"TITLE:%@\n",contact.jobtitle];


// Mail
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"email" items:contact.emailArray labels:contact.emailLabels]];

// Tel
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"phone" items:contact.phoneArray labels:contact.phoneLabels]];

// Adress
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"address" items:contact.addressArray labels:contact.addressLabels]];

// url
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"url" items:contact.urlArray labels:contact.urlLabels]];

// IM
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"im" items:contact.smsArray labels:contact.smsLabels]];

// birthday
NSDate *birthday = contact.birthday;
if (birthday)
{
NSString *bday = [NSString stringWithFormat:@"%@",birthday];
NSArray *bdayArr = [bday componentsSeparatedByString:@" "];
bday = [bdayArr objectAtIndex:0];

vcard = [vcard stringByAppendingFormat:@"BDAY;value=date:%@\n",bday];
}

// Photo
NSData *imageData = contact.thumbData;//contact.imageData;
if (imageData)
{
//vcard = [vcard stringByAppendingFormat:@"PHOTO;BASE64:%@\n",[imageData base64Encoding]];
}

// end
vcard = [vcard stringByAppendingString:@"END:VCARD"];

我得到 NSSTring 作为输出:

vCard : BEGIN:VCARD
VERSION:3.0
N:def;Abc;;;
FN:Abc def
ORG:slk;
EMAIL;type=INTERNET;type=HOME:fdf@gmail.com
TEL;type=CELL:(258) 963-4889
TEL;type=IPHONE:(435) 768-6343
item1.ADR;type=HOME:;;aashram road;Ahmedabad;;;India
item1.X-ABADR:in
END:VCARD

谁能帮我把这个 NSString 转换成 .vcf 文件来获取 vCard?

最佳答案

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory

NSError *error;
BOOL succeed = [yourNSString writeToFile:[documentsDirectory stringByAppendingPathComponent:@"contact.vcf"]
atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!succeed){
// Handle error here
NSLog(@"Error");
}

此代码将 vcard 保存在您应用的 Documents 目录中。

yourNSString 是你生成 vcard 时使用的变量

关于iphone - 如何使用 ios sdk 从地址簿创建 vCard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13249327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com