Search code examples
objective-cif-statementmfmessagecomposeviewcontroller

If Within If Statement


I'm writing a bit of code which will write a text message within my app as shown below:

MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc]  init];
[messageComposer setMessageComposeDelegate:self];
//    Check The Device Can Send Text Messages
if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]];
    [messageComposer setBody:messageBodyText];
    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
//        Need to add an alert view
    NSLog(@"TEXT ISNT WORKING");
}

}

So I currently have an if statement checking that the device is capable of sending messages, but how can I add ANOTHER if statement within that? I basically want to decide what the message body is depending on a switch position within my view like:

If switch is left : Message Body is A
If Switch is Right: Message Body is B


Solution

  • Try given code. Check your switch value is in left or right, I'm consider your switch varible is yourSwitchIsRightSide.

    MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc]  init];
    [messageComposer setMessageComposeDelegate:self];
    //    Check The Device Can Send Text Messages
    if ([MFMessageComposeViewController canSendText]) {
        [messageComposer setRecipients:[NSArray arrayWithObjects: nil]]
    
      //yourSwitchIsRightSide should be bool value
        if(yourSwitchIsRightSide){
            [messageComposer setBody:yourRightMessageBodyText];
        }
        else{
            [messageComposer setBody:yourLeftMessageBodyText];
        }
    
        [self presentViewController:messageComposer animated:YES completion:NULL];
    } else {
    //        Need to add an alert view
        NSLog(@"TEXT ISNT WORKING");
    }
    }