Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: iPhone Objective-C iOS
Hi, Every one.,, i'm new to iPhone application Development.
 
In my SMS sending application.
 
sent time and current time is equal then i can send message. if not that sent time is current time.
 
I need to send SMS on that specific time. For that how i need to store that SMS locally (Recipient No, Message, And Time (When to send) ).
 
Also when the specified time becomes equal to current time i need to activate my application. By Notification or alert.
 
my code is below
 

-(void)sendInAppSMS
{
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
 
    // Get the current date
    NSDate *pickerDate = [self->datePicker date];
 
    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:pickerDate];
    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    // Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];
//  [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    //[dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.repeatInterval=1;
 
    // Notification details
    localNotif.alertBody = [textFieldRounded1 text];
    // Set the action button
    localNotif.alertAction = @"View";
 
    localNotif.soundName = UILocalNotificationDefaultSoundName;
//    localNotif.applicationIconBadgeNumber = 5;

    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
    localNotif.userInfo = infoDict;
 
    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    //[localNotif release];

    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:@"+919994442409" , nil];
                controller.messageComposeDelegate = self;
                [self presentModalViewController:controller animated:YES];
 
            }
 
        }
        else
        {
            NSLog(@"Send Message when time reach at %@",textFieldRounded2.text);
 
            NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
            [textFieldRounded resignFirstResponder];
            [textFieldRounded1 resignFirstResponder];
            [textFieldRounded2 resignFirstResponder];
 
            NSString *To = [textFieldRounded text];
            NSString *Msg  = [textFieldRounded1 text];
            NSString *Timee = [textFieldRounded2 text];
 
            [userDefault setValue:To forKey:@"receiptent"];
            [userDefault setValue:Msg forKey:@"contentt"];
            [userDefault setValue:Timee forKey:@"Tim"];
            [userDefault synchronize];
            NSLog(@"Data saved");
 
            NSString *To1 = [userDefault valueForKey:@"receiptent"];
            NSString *Msg1 = [userDefault valueForKey:@"contentt"];
            NSString *Timee1 = [userDefault valueForKey:@"Tim"];
 
            NSLog(@"Receiptent No: %@",To1);
            NSLog(@"Message Content is : %@",Msg1);
            NSLog(@"Sent Time Is : %@",Timee1);
 

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"SMS" message:@"Message Will Send At Time" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
 
            //NSString *laterMessage =[NSString stringWithFormat:@"%@",textFieldRounded1.text];
        }
 
    }
 

 

Is it possible? please help me...
Posted 26-Nov-12 20:29pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use an NSLocalNotification to send an alert to your app at a specific time.
 
However, the only thing you can do in response to that is alert the user -- you can't actually send an SMS or anything else. The user then has the option of opening your program (at which point you could send the SMS) or ignoring the alert.
 
Under iOS the whole philosophy is that the user is in control of what is happening, and your app isn't allowed to do things in the background without the user being aware of it.
 
The only thing that an app can do in the background is play music -- if your app isn't playing music, then it doesn't run at all unless it is in the foreground.
 
There is a trick you can do, involving playing a silent music file, if you want to keep your app running when it isn't in the foreground -- but that violates Apples guidelines and won't be allowed in the App Store.
 
The only way to properly do this, would be to forward the SMS to your server, and have the server send it at the appropriate time.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 358
1 Sergey Alexandrovich Kryukov 239
2 MT_ 175
3 Yogesh Kumar Tyagi 165
4 ChauhanAjay 156
0 Sergey Alexandrovich Kryukov 6,205
1 OriginalGriff 5,333
2 CPallini 2,473
3 Richard MacCutchan 1,617
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100