Click here to Skip to main content
Rate this: bad
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
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self-&gt;datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
// 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)
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [textFieldRounded1 text];
// Set the action button
localNotif.alertAction = @&quot;View&quot;;
localNotif.soundName = UILocalNotificationDefaultSoundName;

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

if([textFieldRounded.text isEqualToString:@&quot;&quot;] || [textFieldRounded1.text isEqualToString:@&quot;&quot;] || [textFieldRounded2.text isEqualToString:@&quot;&quot;] || [textFieldRounded.text isEqualToString:@&quot;(null)&quot;] ||  [textFieldRounded1.text isEqualToString:@&quot;(null)&quot;] ||  [textFieldRounded1.text isEqualToString:@&quot;(null)&quot;] || [textFieldRounded.text isEqualToString:nil]  || [textFieldRounded1.text isEqualToString:nil]|| [textFieldRounded2.text isEqualToString:nil])
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@&quot;SMS&quot; message:@&quot;Please Enter All Fields!&quot; delegate:self cancelButtonTitle:nil otherButtonTitles:@&quot;OK&quot;, nil];
    [alert show];
    NSDateFormatter *SentTime = [[NSDateFormatter alloc] init];
    [SentTime setDateFormat:@&quot;dd/MM/YYYY hh:mm aa&quot;];
    NSDate *now1 = [NSDate date];
    NSString *Time1 = [SentTime stringFromDate:now1];
    NSLog(@&quot;Time is :%@&quot;,Time1);
    NSString *Sentime=[NSString stringWithFormat:@&quot;%@&quot;,textFieldRounded2.text];
    if([Sentime isEqualToString:Time1])
        NSLog(@&quot;Time Matching... can send msg now&quot;);
        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        if([MFMessageComposeViewController canSendText])
            NSString *Message = [NSString stringWithFormat:@&quot;%@, %@&quot;,textFieldRounded1.text,textFieldRounded2.text];
            NSLog(@&quot;Message is %@&quot;, Message);
            controller.body = Message;
            controller.recipients = [NSArray arrayWithObjects:@&quot;+919994442409&quot; , nil];
            controller.messageComposeDelegate = self;
            [self presentModalViewController:controller animated:YES];
        NSLog(@&quot;Send Message when time reach at %@&quot;,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:@&quot;receiptent&quot;];
        [userDefault setValue:Msg forKey:@&quot;contentt&quot;];
        [userDefault setValue:Timee forKey:@&quot;Tim&quot;];
        [userDefault synchronize];
        NSLog(@&quot;Data saved&quot;);
        NSString *To1 = [userDefault valueForKey:@&quot;receiptent&quot;];
        NSString *Msg1 = [userDefault valueForKey:@&quot;contentt&quot;];
        NSString *Timee1 = [userDefault valueForKey:@&quot;Tim&quot;];
        NSLog(@&quot;Receiptent No: %@&quot;,To1);
        NSLog(@&quot;Message Content is : %@&quot;,Msg1);
        NSLog(@&quot;Sent Time Is : %@&quot;,Timee1);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@&quot;SMS&quot; message:@&quot;Message Will Send At Time&quot; delegate:self cancelButtonTitle:nil otherButtonTitles:@&quot;OK&quot;, nil];
        [alert show];
        //NSString *laterMessage =[NSString stringWithFormat:@&quot;%@&quot;,textFieldRounded1.text];

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

1 solution

Rate this: bad
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.

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

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200

Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2015
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