Question

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

Was it helpful?

Solution 2

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");
}
}

OTHER TIPS

This really is a prime-example for a switch, that's even how Gary describes it.:

if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]]

    //yourSwitchIsRightSide should be bool value
    switch (yourRightSide) {
        case YES:
            [messageComposer setBody:yourRightMessageBodyText];
            break;
        case NO:
            [messageComposer setBody:yourLeftMessageBodyText];
            break;
    }

    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
    //        Need to add an alert view
}

In addition to improving readability switch/case also scales a lot better. If Gary decides he would like to have a couple of additional options later on if-else would create a real mess of things. (The BOOL should probably be replaced by a check against an enum in this scenario)

switch (switchDirection) {
    case MFSwitchDirectionLeft:
        [messageComposer setBody:yourLeftMessageBodyText];
        break;
    case MFSwitchDirectionRight:
        [messageComposer setBody:yourRightMessageBodyText];
        break;
    case MFSwitchDirectionUp:
        [messageComposer setBody:yourUpMessageBodyText];
        break;
    case MFSwitchDirectionDown:
        [messageComposer setBody:yourDownMessageBodyText];
        break;
    default:
        break;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top