How to get VCF data with contact images using CNContactVCardSerialization dataWithContacts: method?

前端 未结 3 589
忘了有多久
忘了有多久 2020-12-19 06:25

I\'m using CNContacts and CNContactUI framework and picking a contact via this

CNContactPickerViewController *contactPicker = [CNContactPickerViewController          


        
3条回答
  •  隐瞒了意图╮
    2020-12-19 06:42

    As a workaround you can create PHOTO field inside of VCard.

    NSError* error = nil;
    NSData* vCardData = [CNContactVCardSerialization dataWithContacts:@[contact] error:&error];
    NSString* vcString = [[NSString alloc] initWithData:vCardData encoding:NSUTF8StringEncoding];
    NSString* base64Image = contact.imageData.base64Encoding;
    NSString* vcardImageString = [[@"PHOTO;TYPE=JPEG;ENCODING=BASE64:" stringByAppendingString:base64Image] stringByAppendingString:@"\n"];
    vcString = [vcString stringByReplacingOccurrencesOfString:@"END:VCARD" withString:[vcardImageString stringByAppendingString:@"END:VCARD"]];
    vCardData = [vcString dataUsingEncoding:NSUTF8StringEncoding];
    

    For some reasons CNContactVCardSerialization does not use any photo of contact. VCard after serialization is looks like:

    BEGIN:VCARD
    VERSION:3.0
    PRODID:-//Apple Inc.//iPhone OS 9.3.2//EN
    N:Contact;Test;;;
    FN: Test  Contact 
    END:VCARD
    

    After insertion the PHOTO field inside VCard you will get

    BEGIN:VCARD
    VERSION:3.0
    PRODID:-//Apple Inc.//iPhone OS 9.3.2//EN
    N:Contact;Test;;;
    FN: Test  Contact 
    PHOTO;TYPE=JPEG;ENCODING=BASE64:
    END:VCARD
    

    After this insertion contact will looks fine in CNContactViewController

提交回复
热议问题