如果发送短信,则保存切换状态

时间:2016-03-11 10:42:04

标签: ios

我一直在尝试将一个开关连接到一个很好的SMS文本,我甚至能够保存交换机所处的状态。

但是现在我只需要在发送短信时保存状态,如果用户没有发送消息但是开关已经打开,则应该使用AlertView提醒他们。

到目前为止,我已经能够保存交换机离开它的状态并连接标签以表示它是否是" ON"或" OFF"在短信中。

- (void)viewDidLoad {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];

    self->switch1.on = ([[standardDefaults stringForKey:@"switchKey"]


                             isEqualToString:@"On"]) ? (YES) : (NO);

if(switch1.on){
    label.text = @"ON";

    NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
    [defults setObject:label.text forKey:@"labelkey"];
    [defults synchronize];


} else label.text = @"OFF"; {

    NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
    [defults setObject:label.text forKey:@"labelkey"];
    [defults synchronize];
}


label.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"labelkey"];

}


- (IBAction)switchChanged:(UISwitch *)sender {

NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];

if (sender.on == 0) {

    label.text = @"OFF";
    [standardDefaults setObject:@"Off" forKey:@"switchKey"];


} else if (sender.on == 1) {

    label.text = @"ON";
    [standardDefaults setObject:@"On" forKey:@"switchKey"];

}

[standardDefaults synchronize];
}

这就是我发送短信的方式。

 - (IBAction)sendRequest:(id)sender
{
    MFMessageComposeViewController *messageVC =    [[MFMessageComposeViewController alloc] init];

messageVC.body = [@[label.text] componentsJoinedByString:@""];
messageVC.recipients = @[_phoneNumber.text];
messageVC.messageComposeDelegate = self;
[self presentViewController:messageVC animated:NO completion:NULL];
}

1 个答案:

答案 0 :(得分:1)

通过使用 MFMessageComposeViewControllerDelegate 委托,您可以找到用户发送或取消的消息。

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result

{

switch (result) {
    case MessageComposeResultCancelled:
        //show alert here as per your requirement break;

    case MessageComposeResultFailed:
    {
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to send SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [warningAlert show];
        break;
    }

    case MessageComposeResultSent:
        break;

    default:
        break;
}

[self dismissViewControllerAnimated:YES completion:nil];

}

参考此链接 - > Send sms