iphone - how to send a sms message at a scheduled time - iphone

I am creating an application where user can send an SMS to the Recipient, and it is working fine. But I want to send Message at a scheduled time. For this I have two picker views, from one picker sender we can set date and from other sender can set time as I have shown in the attached screenshot.
How to trigger send button or send the sms at a scheduled time? Can I use an NSTimer?
Any help would be greatly appreciated. Thank you. If someone have any doubt related to question then let me know.

You can use NSTimer to schedule message.
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
here you can pass target as Your view controller that has sms sending functionality and selector will be your action method that gets called on sms button click.
But problem with NSTimer is that it stops when App is in background.If you have such requirement then NSTimer will not be useful.

You can use NSTimer in background also...
You need to make some changes in appDelegate...
Follow the below link to make your app wait for background task to complete
beginBackgroundTaskWithExpirationHandler
for long time in background try (more than 10 min)
How do I make my App run an NSTimer in the background?

Related

UIWebView loadRequest when applicationDidEnterBackground

In my app from applicationDidEnterBackground i want to ask the application for more time to
create a UIWebView and load request with UIBackgroundTaskIdentifier, then in the delegate
method of UIWebView (webViewDidFinishLoad) i want to do a stuff there and show an alert or
notification while the
application is still reining in the background .
so how i can do that?.
Apple's documentation for UIApplication class for beginBackgroundTaskWithExpirationHandler: method says:
You can call this method at any point in your application’s execution.
You may also call this method multiple times to mark the beginning of
several background tasks that run in parallel. However, each task must
be ended separately. You identify a given task using the value
returned by this method.
This method can be safely called on a non-main thread.
So, once web view finish loading in background you can trigger another operation from webViewDidFinishLoad to show alert.
When you receive applicationDidEneterBackground your app is already effectively in the background. At that moment all your networking should be closed and you really shouldn't try to show any alerts or notifications.

How to schedule events programmatically in ios?

I have been tasked to write an app that allows a user to schedule emails to be sent out in future.
The user selects a date time from a date picker, composes the message and recipient and then schedules the event. When the date/time occurs the message is sent out.
Can someone guide me to how to get about scheduling lets say a text message. I know how to send a text message. Just was not sure on the scheduling aspect of things.
Any pointers will be much appreciated.
The first response will technically allow you to establish a timer that will fire every 2.5 seconds, however the original poster asked for a solution that would fire at a specific time. For that you need to use the following method of NSTimer:
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
The first argument is an NSDate indicating when the timer should fire.
The original poster did not specify, but if this is an iOS app then it is important to understand that timers scheduled to fire at a distant date/time will not fire if your app is not the foreground app. In fact there is no way to schedule such an event to occur when your app is in the background on iOS, so you must take that into account.
Here's a snippet of code which sets a one use timer to call self's imageSavedLabelOff: selector with itself (the timer) as the object parameter to the method. The timer schedules the call to be made in 2.5 seconds.
NSTimer *quickie = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(imageSavedLabelOff:) userInfo:nil repeats:NO];
You may have already found the answer by now but for future visiters like me I would like to suggest an answer- i.e. EventKit :
https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/EventKitProgGuide/ReadingAndWritingEvents.html
You can schedule/fetch events for any time and do your stuff accordingly. Hope this helps somebody.
You should be able to achieve this using NSRunLoop. Check out the Threading Programming Guide.
Apart from the use of NSTimer, you should be aware that sending of the E-Mail can fail for several reasons (no network available and others). Then you need to reschedule the request, maybe give up after 3 retries and notify the user about this.
You can use -
[self performSelector:#selector(myFunc:) withObject:nil afterDelay:5.0];

iPhone - Get user interaction event and automatic logout

In my iPhone app I want to logout the user if nothing happens till about 2 minutes (e.g. the user puts down the phone). Does anybody has such issue? What is the best way to implement this feature? I think I save the date of last event to NSUserDefaults, then on the next event first I check the current date. If the difference is larger than 2 minutes go to login screen, else refresh the stored date. But how can I get the touch event generally?
Thanks, madik
There's a method in UIApplicationDelegate for that:
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
Note that it also will be called when the app is going to background state. That will help you store the data whenever the app is going to inactive state. If you want to check if a certain amount of time has passed, you will have to use a NSTimer and store the last touch event. I think it cannot be done because you can't intercept all the touch events (Maybe it's over an object managed by the system. The status bar is an example). I guess is better to let the system to manage all the activity/inactivity stuff and store your data when necessary.
EDIT: I didn't understand what you mean the first time. Check this accepted answer, it accomplish what you need. Basically you have to subclass UIApplication and override sendEvent method.
'NSTimer'
When you say "how can I get the touch event generally?", if you mean how can you tell if the user is idle or not, you'll have to set up some system to gather all touch events at a higher level in your app. You could update the last touch time you mentioned in NSUserDefaults but that may be inefficient during the run of the app, so you could just post the touch event to your main app delegate and have it save the time of last touch. Which would also be where you could set up the 2 minute timer.
Something like:
- (void) someAppDelegateMethodThatYouCallForAnyUserEvent
{
[self.idleTimer invalidate];
self.lastEvent = [NSDate now];
self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:#selector(logoutAndGotoLogin) userInfo:nil repeats:NO];
...
}
You'll also have to do some cleanup in your app delegate methods when the app goes to background etc if you support that behavior.

Please Wait iPhone - No NSTimer

I am currently using NSTimer to load a "Please Wait" alert view in my application while it contacts a server. I want to get rid of this NSTimer and change it so that it runs the "Please Wait" alert view in a loop until it gets a response from the server.
How would I do this?
A loop would be bad (blocking the main thread), but if you use NSURLConnection object you get a connectionDidFinishLoading: message in the delegate.
You pop up the alert when you start the connection.
You dismiss the alert in the connectionDidFinishLoading: of your delegate.
And also when an error occurs.
Though I agree with #Radu when it's about user experience.
Set up a delegate for the NSURLConnection. Your delegate will then receive messages at interesting points in the connection, such as when data is received and when the connection is finished.
try this page. It helps manage cases where there might be more than one network event going on.

How can I dismiss UILocalNotifications that appear when my app is in the background?

I'm working on an iPhone app that needs to remind a user to check in at regular intervals using UILocalNotifications. If they don't check in for a few hours, they may be reminded a couple times, but I just want to show the latest notification.
Now, if the app is open, I get a callback to didReceiveLocalNotification:(UILocalNotification *)notification, and I can keep track of whether there's a notification showing.
If the app is not running and the user clicks the -action- button, I get a callback to
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
so I can dismiss any old notifications. However, if they click 'cancel', they have to click 'cancel' on a bunch of layered notifications, because as far as I can tell I don't get a callback (how could I, if the app isn't launched) and it doesn't seem like there's a flag or something when creating the UILocalNotification to have newer reminders from an app automatically dismiss other ones.
If the app is in the background but running, it's worse - first, I don't get any sort of callback there if the user click's cancel, so I have the same problem - the user has to click cancel a bunch of times. Second, if they click the action, I get a call to ApplicationDidBecomeActive, but there's no distinguishing between that and when the user just switches back and forth; I can dismiss and reschedule them here, but it doesn't seem to work perfectly, sometimes a few pop up before they're dismissed.
Any suggestions? If there were a way for the notifications to expire automatically that would be great too. I've looked online a bit and haven't found much help, but it seems like a big oversight, so hopefully there's some way to handle this gracefully.
Thanks.
You won't be able to get any callback when the user "cancel"s as you pointed out.
Is it possible to just remind the user once in your case? Only schedule one notification at a time and renew it on app launch/resume.
I have not tried the following yet, but I believe this can be a work around for your case. Use CLLocationManager's startMonitoringSignificantLocationChanges
Every time the device's location changes significantly, your app will be launched in the background with options passed to locationManager:didUpdateLocations: and you can probably schedule a UILocalNotification from there!