Time Touch Events Take in iPhone SDK - iphone

I am wondering how I would go about measuring the time in between touches inside a view in the iPhone SDK. I know that the first event triggered is touchesBegan: and then the last one triggered is touchesEnded:, however I just don't know how I would go about measuring the time that the user has touched the view for. For instance, if they keep their finder in the view for 2 seconds, it will automatically trigger function twoSeconds: or something like that.
Thanks for any help!

Use [NSDate date] to get the current date and time. Store it in touchesBegan:, and fetch the duration in touchesEnded: as this
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:beganDate];
You will now have the duration between the events measured in seconds in length.

Related

NSTimer stops in Background while power cable is disconnected

I have created GPS base Application.
In which App logged GPS data every 1 second.
App used the NSTimer to fetch GPS data every second.
This NSTimer is start in background. The NSTimer is start when app received the silent push notification from APNS.
I have seen a problem in iOS 7 that when Phone attached with power cable at that time timer call appropriately but without attached power cable timer stops while App in background.
Any inputs to resolve this issue greatly appreciated.
An NSTimer is not guaranteed to fire if your app is not in foreground. Once you unplug the cable the system puts your app into background to save battery.
Using an NSTimer is not the supported method to get location data. Your CLLocationManager will tell its delegate when there is a new location. No need to poll it.
If you need to track geolocation in background you need to declare location updates as a background mode from the capabilities tab in Xcode 5 target settings. Otherwise your location manager will stop delivering location updates once your app is not in foreground.
There are only few use cases that you can implement for foreground. If you don't want your application to be refused within review please don't use any hacks. Of course, you can use NSTimer in background, but it must be created in thread (runloop) of background task. But this background task lives for the only certain amount of time, so your timer must fire during this period. In other words, your first goal is to create background task, and only then you can use the timer. There are few cases suitable for your purpose (that allow you to create this task): 1.Monitor significant location changes or region 2.Fetch data (iOs 7). So please refer
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"BG fetch in action");
//the only 30 sec is allowed to perform all your actions
//during this period you can each second track GPS data
[self performSelector:#selector(finishBackgroundFetch:) withObject:completionHandler afterDelay:27];
[[NSNotificationCenter defaultCenter] postNotificationName:#"StartMyGPSRoutine" object:nil];
}
-(void)finishBackgroundFetch:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"BG fetch completion handler");
[[NSNotificationCenter defaultCenter] postNotificationName:#"ForceStopAnyBackgroundTaskCreatedWithStartMyGPSRoutine" object:nil];
completionHandler(UIBackgroundFetchResultNewData);
}
of your application delegate -- this will be useful in your case as well as monitor significant location changes.
You ought to put anywhere:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(setMinimumBackgroundFetchInterval:)]) {
NSLog(#"Set Force BG interval to %ld", interval);
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:interval];
}
In my case I fetch the GPS data each 5 min so 27sec to complete this task is not so long. Anyway you can play with time intervals with XCode. Please refer Main Menu->Debug->Simulate Background Fetch

Detect when song stops playing

This seems like something really basic but how do I execute a function when a song ends? I am playing one item at at time, and when it ends (or if possible, 10 seconds before it ends) I would like to call a function that gets the next song from a webservice.
I think it's handled in the MPMusicPlayerControllerPlaybackStateDidChangeNotification notification but am not sure. For the record, this notification only needs to be when the song ends (not pauses).
All I have is this, which is the function for the above observer:
- (void) handle_PlaybackStateChanged: (id) notification
{
MPMusicPlaybackState playbackState = [musicPlayer playbackState];
}
How do I find out if the song has ended? Also, is it possible create an observer/function when there are 10 seconds left on the song?
When you receive the notification you can look up the playbackState property of the MPMusicPlayerController.
As for executing a function 10 seconds before the song ends, you could set up a timer to execute in the future based on the length of the song minus 10 seconds, you would need to cancel that timer if the user changes the playbackState or selects a different song.

Local Notification Alert Timing

notificaiton = [[UILocalNotification alloc] init];
notificaiton.fireDate = [NSDate Date];
notificaiton.repeatInterval = 0;
notificaiton.alertBody = #"Alarm";
notificaiton.timeZone = [NSTimeZone defaultTimeZone];
notificaiton.repeatCalendar = [NSCalendar currentCalendar
notificaiton.soundName = #"Alarm.wav"
[[UIApplication sharedApplication] scheduleLocalNotification:notificaiton];
my question is ,
Local Notification alert stay only for few seconds but, is it possible, local notification alert stay for few minutes?
please any body has answer
Thanks, In Advance
No it is not possible to customize the AlertView and its timings. System does it and we don't have any control over it. We can only customize, the alertbody and title of the action button.
Configure the substance of the notification: alert, icon badge number, and sound.
The alert has a property for the message (the alertBody property) and
for the title of the action button or slider (alertAction); both of
these string values can be internationalized for the user’s current
language preference.
if you mean that you need to play notification sound more than 30 sec ??? then it is not possible.
you wrote that Local Notification alert stay only for few seconds but check proper that, this stay as per your sound file length.
and this sound file Must be maximum 30 sec in length.
if you will try to give soundName more than 30 sec. then it will play default sound in notification.
find this line
"Sounds that last longer than 30 seconds are not supported. If you specify a file with a sound that plays over 30 seconds, the default sound is played instead." into apple notification doc.

How to disable UILocalNotification in iPhone for some time?

I have implemented one reminder iphone applicaion in which I have used local notification for reminder.
In this application their one functionality alert on/off.
So when user set on then user get notification and if its off then user can get notification.
I have done googling but not got sucess.
Can you give me idea for that is it possible or not.
Thanks in advance
Do you have other notifications in your appln.
If you don't have other notification in your app than,
Do one thing,
When you set OFF
- cancel all the notification
- calculate the time difference of current time & your notification firing time. set this in to some variable.
when you set it ON
- reschedule your Notification based on the time which you have saved earlier.
I am considering that you know how UISwitch works, according to switch position
UILocalNotification *localNotif;
//if switch is on
localNotif = [[UILocalNotification alloc] init];
//else if localNotif is not equal to null then
localNotif = nil;
You can start a timer which continuously check switch position and do the above stuff that depends on your coding.

How to manage screen displaying time in iphone for my app?

I am creating an iphone application. In which I am needed to keep screen on till 30 mins. If user don't perform any task within that time the light of device in which we can see scree may be down untill user again tap. No matter if light goes dimmed within the 30 min but it should not be fully down? Like Media player if user don't tap on screen we can see screen coz moview is playing. but other screen if user don't interact display light will be down and user need to tap on screen.
Is there any way to do this in iphone sdk.
Please help me out.
Thanks
First of all to disable dimming use
[UIApplication sharedApplication].idleTimerDisabled = YES;
If you want to set appropriate amount of time then you can create an NSTimer with time interval of 30 mins (30*60 sec). When the timer fires you set
[UIApplication sharedApplication].idleTimerDisabled = NO;
If user touches the screen you invalidate current timer and start the new one.