gpt4 book ai didi

ios - 如果发送短信,则保存开关状态

转载 作者:行者123 更新时间:2023-11-29 01:09:31 24 4
gpt4 key购买 nike

我一直在尝试将开关连接到 SMS 文本,效果很好,我什至能够保存开关所在的状态。

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

到目前为止,我已经能够保存开关离开时的状态,并连接一个标签以在 SMS 中表示它是“打开”还是“关闭”。

- (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];
}

最佳答案

通过使用MFMessageComposeViewControllerDelegate委托(delegate),您可以发现消息被用户发送或取消。

- (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

关于ios - 如果发送短信,则保存开关状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com