Reputation: 141
i'm new to iphone application development.
i'm creating a application to send sms.
In my application i need to send sms on specified time.
I have a time picker to specify the time in UITextField.
-(void)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
}
}
}
if the time is equal to current time then send the sms nowitself (no need to store the messsage anywhere).
Other wise i need to send that sms , when current time becomes as that specified time. Until the time reach how to keep(store) the message and send on the time.
Regards,
Rajendran.B
Upvotes: 2
Views: 5254
Reputation: 832
As has been pointed out in the other answers, it's not possible to "save" the message that the user has composed in the MFMessageComposeViewController
to send at a later time (nor are there any other means to do so).
Your options are:
Collect the details for the SMS ahead of time, but then at the correct time prompt the user. You could use a UILocalNotification
for this if the app might be closed. You can open a message with the right details prefilled using setTitle:
, setBody:
, and setRecipients:
on the MFMessageComposeViewController
instance, so the user will just have to hit send.
Send the message details back to a server, and send the SMS at the right time using some SMS system such as the Twilio api. This will add significant complexity, and the SMS wouldn't come from the user's phone number.
Upvotes: 0
Reputation: 1917
In terms of user experience, your code is very unlikely to allow the user to send a message unless the current time matches the exact minute of the specified time. It would make more sense to check to see if the time is after or within a certain range of the specified time. Also of note, it would be easier to make these comparisons if you weren't comparing string literals. (use NSDate and associated convenience methods to make these comparisons)
In terms of sending a message at a certain time, this will likely require a server component in which you register the message to be sent and the time it should be sent, and then at specified intervals, process messages that are in the message send queue.
Upvotes: 0