i am trying to develop an app that takes input of some fields from the user about a contact and then export that contact as VCard. I can successfully export the contact but the when i try to import the contact using Contacts at iCloud.com, the picture is not being imported. All the other information is being imported but not the picture.
There are two ways i tried to export the VCard.
First Way Here
public func getVCard() -> Data?{
var contact = CNMutableContact()
let homeEmail = CNLabeledValue(label:CNLabelHome, value:self.email as! NSString)
contact.emailAddresses = [homeEmail]
let phone = CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: self.phone!))
contact.phoneNumbers = [phone]
contact.givenName = self.name!
if let d = self.data{
contact.imageData = d
}
let address = CNMutablePostalAddress()
address.country = self.address!
contact.postalAddresses = [CNLabeledValue(label: CNLabelHome, value: address)]
contact.organizationName = self.company!
contact.jobTitle = title!
let sc1 = CNSocialProfile(urlString: "\(socialMedia1!)", username: self.name!, userIdentifier: self.name!, service: CNContactSocialProfilesKey)
let sc2 = CNSocialProfile(urlString: "\(socialMedia2!)", username: self.name!, userIdentifier: self.name!, service: CNContactSocialProfilesKey)
//contact.socialProfiles = [CNSocialProfile(]
let sc3 = CNSocialProfile(urlString: "\(socialMedia3!)", username: self.name!, userIdentifier: self.name!, service: CNContactSocialProfilesKey)
contact.socialProfiles = [CNLabeledValue(label: "Profile1", value: sc1), CNLabeledValue(label: "Profile2", value: sc2), CNLabeledValue(label: "Profile3", value: sc3)]
contact.note = self.notes!
let urlAddresses = CNLabeledValue(label: CNLabelURLAddressHomePage, value: self.web as! NSString)
contact.urlAddresses = [urlAddresses]
var data: Data?
do{
data = try CNContactVCardSerialization.data(with: [contact])
}catch let error{
print(error.localizedDescription)
}
return data
}
When i debug this function, i can see the picture data in the contact object but when that VCard is exported and i try to import it on iCloud.com it does not import picture of that contact.
Now the second method i used to export the contact is as follow and i would prefer the answer using this method because this is the best fit for me.
func textBasedVCard()-> Data?{
var string = "BEGIN:VCARD\nVERSION:3.0\n"
string += "N:\(self.name!);\nFN:\(self.name!)\nORG:\(self.company!)\nTITLE:\(self.title!)\nTEL;TYPE=WORK,VOICE:\(self.phone!)\nADR;TYPE=WORK:;;\(self.address!)\nNOTE:\(self.notes!)\nitem1.URL:\(self.web!)\nitem2.URL:\(self.blog!)\nitem3.URL:\(self.socialMedia1!)\nitem4.URL:\(self.socialMedia2!)\nitem5.URL:\(self.socialMedia3!)\nEMAIL;TYPE=PREF,INTERNET:\(self.email!)\nEND:VCARD"
print(string)
let utf8str = string.data(using: String.Encoding.utf8)
//utf8str?.base64EncodedStringWithOptions(NSData.Base64EncodingOptions(rawValue: 0))
if let base64Encoded = utf8str?.base64EncodedString(options: .init(rawValue: 0))
{
return Data(base64Encoded: base64Encoded)!
}
return nil
}
In this method i do not know how to attach picture data. Please guide me through this problem. I will be really grateful. Thanks.
Edit:
This question in no way a duplicate of any other question. You can see the link yourself someone posted just to make himself feel proud.