2016-07-26 18 views
0

In meinem Projekt muss ich eine vCard (vcf-Datei) erstellen und senden, die auch ein Bild enthalten muss. Ich habe alles richtig gemacht, außer dass ich der vCard kein Bild hinzufügen kann. Ich habe meinen Code unten geteilt.Wie man Bild in VCard (VCF-Datei) mit Objective C anhängen?

- (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"]; 
} 

Antwort

1
- (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 
    } 
} 
}