2012-11-26 20 views
2

我是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 
    } 
} 

}

如果时间等于当前时间,然后发送短信nowitself(无需存储任何地方的讯息话题)。

其他明智的我需要发送短信,当前时间成为指定的时间。直到达到如何保存(存储)消息并发送的时间。

问候,

Rajendran.B

+1

plz分享你的努力,并写一些你做的代码。 – Milind

+0

textFieldRounded2。文本包含指定的时间... – Rajendran

回答

2

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

see this

+0

是的,我只是发布这个。 – klcjr89

0

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

就在特定时间发送消息而言,这可能需要您注册要发送的消息的服务器组件以及它应该发送的时间,然后按指定的时间间隔处理消息发送队列中的消息。

+0

是以其他方式来做到这一点... – Rajendran

0

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

的选项有:

  • 收集细节提前短信,但随后在正确的时间提示用户。如果应用可能关闭,您可以使用UILocalNotification。您可以在MFMessageComposeViewController实例上使用setTitle:,setBody:setRecipients:预填的正确详细信息打开邮件,因此用户只需点击发送即可。

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