Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


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

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900