Search code examples
objective-cvcf-vcard

How to attach image in vCard (vcf file) using Objective C?


In my project I need to create and send a vCard (vcf file) that must include an image too. I did everything right except I can not add image to the vCard. I have shared my code below.

- (IBAction)shareButtonPressed:(UIButton *)sender {

  NSError *error;
  NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]     stringByAppendingPathComponent:@"vCard.vcf"];
  [[self vCardRepresentation] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

  UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[@"Test", [NSURL fileURLWithPath:filePath]] applicationActivities:nil];
  activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypeMessage, UIActivityTypeCopyToPasteboard];
  [self presentViewController:activityVC animated:YES completion:^{

}];
}

- (NSString *)vCardRepresentation
{
   NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
   NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"Rokon"], 1.0);

  [mutableArray addObject:@"BEGIN:VCARD"];
  [mutableArray addObject:@"VERSION:3.0"];
  [mutableArray addObject:[NSString stringWithFormat:@"FN:%@", @"Rokon"]];
  [mutableArray addObject:[NSString stringWithFormat:@"TEL:%@",@"+8801811536248"]];
  [mutableArray addObject:[NSString stringWithFormat:@"PHOTO;BASE64:%@",[imageData base64EncodedDataWithOptions:0]]];
  [mutableArray addObject:@"END:VCARD"];

  return [mutableArray componentsJoinedByString:@"\n"];
}

Solution

  • - (void)shareContact{
            [self emptySandbox];
            NSString *contactName = [NSString stringWithFormat:@"%@ %@",[Person sharedInstance].firstName, [Person sharedInstance].lastName];
            NSError *error;
            NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.vcf", contactName]];
            [[self vCardRepresentation] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
            UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[contactName, [NSURL fileURLWithPath:filePath]] applicationActivities:nil];
    
            activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard];
            [activityVC setValue:contactName forKey:@"subject"];
    
            [self presentViewController:activityVC animated:YES completion:^{
    
            }];
     }
    
    
    - (NSString *)vCardRepresentation
      {
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
    
    NSData *imageData = UIImageJPEGRepresentation([Person sharedInstance].profileImage, 1.0);
    
    [mutableArray addObject:@"BEGIN:VCARD"];
    [mutableArray addObject:@"VERSION:3.0"];
    
    [mutableArray addObject:[NSString stringWithFormat:@"FN:%@", [NSString stringWithFormat:@"%@%@", [Person sharedInstance].firstName, [Person sharedInstance].lastName]]];
    [mutableArray addObject:[NSString stringWithFormat:@"TEL:%@",[Person sharedInstance].phone]];
    [mutableArray addObject:[NSString stringWithFormat:@"email:%@", [Person sharedInstance].email]];
    [mutableArray addObject:[NSString stringWithFormat:@"PHOTO;BASE64;ENCODING=b;TYPE=JPEG:%@",[imageData base64EncodedStringWithOptions:0]]];
    [mutableArray addObject:@"END:VCARD"];
    return [mutableArray componentsJoinedByString:@"\n"];
    }
    
    -(void)emptySandbox
    {
    NSFileManager *fileMgr = [[NSFileManager alloc] init];
    NSError *error = nil;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
    
    while (files.count > 0) {
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
        if (error == nil) {
            for (NSString *path in directoryContents) {
                NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
                BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error];
                files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil];
                if (!removeSuccess) {
                    // Error
                }
            }
        } else {
            // Error
        }
     }
    }