Friday, 24 February 2012

converting the contact in the Vcard represantation and Sending Mail + iphone

- (NSString *)vCardRepresentation
{
  NSMutableArray *mutableArray = [[NSMutableArray alloc] init];

  [mutableArray addObject:@"BEGIN:VCARD"];
  [mutableArray addObject:@"VERSION:3.0"];

  [mutableArray addObject:[NSString stringWithFormat:@"FN:%@", self.name]];

  [mutableArray addObject:[NSString stringWithFormat:@"ADR:;;%@",
                           [self addressWithSeparator:@";"]]];

  if (self.phone != nil)
    [mutableArray addObject:[NSString stringWithFormat:@"TEL:%@", self.phone]];

  [mutableArray addObject:[NSString stringWithFormat:@"GEO:%g;%g",
                           self.latitudeValue, self.longitudeValue]];

  [mutableArray addObject:[NSString stringWithFormat:@"URL:http://%@",
                           self.website]];

  [mutableArray addObject:@"END:VCARD"];

  NSString *string = [mutableArray componentsJoinedByString:@"\n"];

  [mutableArray release];

  return string;
}
For sending mail for vcf file
 CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
  
 if ([MFMailComposeViewController canSendMail]){
       
    NSData *vCards = (NSData *) ABPersonCreateVCardRepresentationWithPeople(allPeople);
   
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 
    mc.mailComposeDelegate = self; 
   
    [mc setSubject:@"Contact"];
   
    [mc setMessageBody:@"<p>My message...<p>" isHTML:YES];
       
        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, 1);
       
       
      //  CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
       
     //   NSString *contactFirst = [NSString stringWithFormat: @"%@", (NSString *)firstName];
       
     //  NSString *label = ([allPeople count] == 1)? (NSString *)ABRecordCopyCompositeName([allPeople objectAtIndex:0]): @"Contacts";
   
    [mc addAttachmentData:vCards mimeType:@"text/x-vcard" fileName:@"All.vcf"];//
   
    [self presentModalViewController:mc animated:YES]; 
    [mc release]; 
  
    }

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{  
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }
    [self becomeFirstResponder];
   
    [controller dismissModalViewControllerAnimated:YES];
}

No comments:

Post a Comment