如何在特定时间发送短信

时间:2012-11-26 06:07:31

标签: iphone objective-c ios nsdateformatter

我是iphone应用程序开发的新手。

我正在创建一个发送短信的应用程序。

在我的应用程序中,我需要在指定的时间发送短信。

我有一个时间选择器来指定UITextField中的时间。

- (无效)sendInAppSMS {     的NSLog(@ “的SendMessage”);

if([textFieldRounded.text isEqualToString:@""] || [textFieldRounded1.text isEqualToString:@""] || [textFieldRounded2.text isEqualToString:@""] || [textFieldRounded.text isEqualToString:@"(null)"] ||  [textFieldRounded1.text isEqualToString:@"(null)"] ||  [textFieldRounded1.text isEqualToString:@"(null)"] || [textFieldRounded.text isEqualToString:nil]  || [textFieldRounded1.text isEqualToString:nil]|| [textFieldRounded2.text isEqualToString:nil])
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"SMS" message:@"Please Enter All Fields!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}
else 
{
    NSLog(@"Done");
    NSDateFormatter *SentTime = [[NSDateFormatter alloc] init];
    [SentTime setDateFormat:@"dd/MM/YYYY hh:mm aa"];
    NSDate *now1 = [NSDate date];
    NSString *Time1 = [SentTime stringFromDate:now1];
    NSLog(@"Time is :%@",Time1);
    NSString *Sentime=[NSString stringWithFormat:@"%@",textFieldRounded2.text];

    if([Sentime isEqualToString:Time1])
    {
        NSLog(@"Time Matching... can send msg now");

        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        if([MFMessageComposeViewController canSendText])
        {

            NSString *Message = [NSString stringWithFormat:@"%@, %@",textFieldRounded1.text,textFieldRounded2.text];
            NSLog(@"Message is %@", Message);
            controller.body = Message;
            controller.recipients = [NSArray arrayWithObjects:@"+919999999999" , nil];
            controller.messageComposeDelegate = self;
            [self presentModalViewController:controller animated:YES];
        }

    }
    else
    {
        NSLog(@"Send Message when time reach at %@",textFieldRounded2.text);

        //Here What code i should write
    }
}

}

如果时间等于当前时间,则自动发送短信(无需将消息存储在任何地方)。

其他方面我需要发送该短信,当前时间变为指定时间。直到达到如何保存(存储)消息并发送时间为止。

此致

Rajendran.B

3 个答案:

答案 0 :(得分:2)

您无法在iOS中使用计时器发送短信,Apple不允许此功能。

see this

答案 1 :(得分:0)

就用户体验而言,除非当前时间与指定时间的确切分钟匹配,否则您的代码不太可能允许用户发送消息。检查时间是否在指定时间的某个范围之后或之内更有意义。另外值得注意的是,如果不比较字符串文字,那么进行这些比较会更容易。 (使用NSDate和相关的便捷方法进行这些比较)

在某个时间发送消息方面,这可能需要一个服务器组件,您可以在其中注册要发送的消息及其发送时间,然后按指定的时间间隔处理消息中的消息。消息发送队列。

答案 2 :(得分:0)

正如其他答案中所指出的那样,不可能“保存”用户在MFMessageComposeViewController中编写的消息以便稍后发送(也没有任何其他方法可以这样做) )。

您的选择是:

  • 提前收集短信的详细信息,然后在正确的时间提示用户。如果应用可能已关闭,您可以使用UILocalNotification。您可以使用setTitle:实例上的setBody:setRecipients:MFMessageComposeViewController预先填写正确的详细信息,以便用户只需点击发送。

  • 将消息详细信息发送回服务器,并使用某些SMS系统(例如Twilio api)在适当的时间发送SMS。这将增加显着的复杂性,并且SMS不会来自用户的电话号码。

相关问题