1

I am currently trying to pass data from one ViewController to a SMS ViewController provided by Apple (through MFMessageComposeViewController) using NSUserDefaults.

I am successfully passing the name of the person to the SMS textfield within my MFMessageComposeViewController, but for some reason the space in between the persons first name and last name gets deleted (i.e. instead of John Smith, it shows up in the textfield as johsmith).

I have logged out all my values and they show that there is indeed a space between the first name and last name (i.e. printing out John Smith)

Below is my code, hopefully someone else has experienced this weird issue or could point me in the right direction. Thanks!!!

-(void)launchMessengerContacts {

    if(![MFMessageComposeViewController canSendText]) {

    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:@"Your device doesn't support SMS!" preferredStyle:UIAlertControllerStyleAlert];


    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];

    [alertController addAction:confirmAction];
    [self presentViewController:alertController animated:YES completion:nil];
    return;
}

 NSString *message = @"Hey! I'm inviting you to play this game!";


 MFMessageComposeViewController *messageController =  [[MFMessageComposeViewController alloc] init];
 messageController.messageComposeDelegate = self;
 [messageController setBody:message];

 NSUserDefaults *contactsUD = [NSUserDefaults standardUserDefaults];

 [[NSUserDefaults standardUserDefaults] synchronize];

 NSMutableString *contactName = [contactsUD     objectForKey:@"contactName"];

 NSLog(@"Contact Name: %@", contactName);

 messageController.recipients = [NSArray arrayWithObjects:contactName, nil];

 NSLog(@"Message Controller Recs: %@", messageController.recipients);

 [self presentViewController:messageController animated:YES completion:nil];
 }

In my didselectrowatindexpath:

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:   
(NSIndexPath *)indexPath {
AddressBookTableViewCell *cell = [tableView  
cellForRowAtIndexPath:indexPath];


NSUserDefaults *contactUD = [NSUserDefaults standardUserDefaults];
[contactUD setObject:cell.contactLabel.text forKey:@"contactName"];
NSLog(@"cell : %@", cell.contactLabel.text);

NSLog(@"NSUserDefaults is %@", contactUD);
NSLog(@"User Defaults Contact Name: %@", [contactUD   
objectForKey:@"contactName"]);

[self launchMessengerContacts];
}
a2b123
  • 573
  • 1
  • 5
  • 19
  • Right here: NSMutableString *contactName = [contactsUD objectForKey:@"contactName"]; NSLog(@"Contact Name: %@", contactName); You get the "John Smith"? Also remove the [[NSUserDefaults standardUserDefaults] synchronize]; make this higher then the [self launchMessengerContacts]; – Andrew Veresov Oct 27 '16 at 15:19
  • 2
    FYI - this is an inappropriate use of NSUserDefaults. – rmaddy Oct 27 '16 at 15:41
  • @AndrewVeresov yes I am getting the console to print out "Contact Name: John Smith" Any ideas on why its not passing that value? – a2b123 Oct 27 '16 at 15:51
  • @rmaddy you are correct, but not sure at this point what the proper way is to pass that value to a iOS message VC. Any ideas on the proper way to do this? – a2b123 Oct 27 '16 at 15:52

1 Answers1

0

From the MFMessageComposeViewController documentation:

Each string in the array should contain the phone number of the intended recipient.

So you need to add the phone number, not the name.

Yonat
  • 4,382
  • 2
  • 28
  • 37
  • yes I will eventually pass the phone number but I am having trouble getting the correct phone numbers to match up with the correct contacts (since there are multiple phone numbers for any given contact) – a2b123 Oct 27 '16 at 15:54
  • `MFMessageComposeViewController` will not do that for you, you'll need to use the iOS Contacts framework for that. See [CNContactStore](https://developer.apple.com/reference/contacts/cncontactstore) – Yonat Oct 28 '16 at 09:01