How know if iOS device is sleeping in a background process - ios

I have a application running in background and I need to know if device is sleeping in order to start a sincronisation process, but I didn't find information about this.
Does anyone know if it is posible and how do it?
Thanks.

You cannot know if the device is asleep because you have no control over the OS.
You can, otherwise, use the App Delegate method:
- (void)applicationWillResignActive:(UIApplication *)application
{
//your code goes here
}
if you want to wait till your app goes to background

I believe you can't do this using public API. The only thing which you can check whether your application is active or in background (using AppDelegate callbacks). And as Luke pointed out in comments, checking whether device "falls asleep" isn't iOS best design practice.
There are some private API's to do what you want, you can look at following questions:
Is there a way to check if the iOS device is locked/unlocked?
Detect screen on/off from iOS service
However, you should be aware that your app won't be accepted in AppStore in such case.

Related

iOS long vibration

I have an application that can detect accidents, it is really important for us to alert users using vibration and alert sound if an accident is detected.
My questions are:
Is it possible to add long vibration in application by using custom sounds or something that apple might be ok with?
If I use private apis, is it possible to convince apple to approve my app considering that the use case is really critical?
Two questions here, really.
1st one: haptic feedback / control vibration on iOS / custom iOS patterns
No, it can't be done, even in the new Apple Watch without Jailbreaking your phone. You can have a look at this keyboard mod (needs Jailbreak). Here you have some code but it needs Jailbreaking your phone.
If you need to alert your users I recommend just playing the default vibration inside a while. Sleep the current thread for 1 sec, then vibrate again until some boolean flag changes. Objective-C Pseudocode:
while (!endVibrationAlert) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[NSThread sleepForTimeInterval:1];
}
// when user touches some button to dismiss alert
self.endVibrationAlert = true;
2nd one: never ever try to bypass Apple's reviewing system. Abide by the rules. For your 1st version, maybe it passes. Then, in the next update your App can get rejected.

Network lost on screen lock unlock

Encountered a very weird problem, using simple AFNetworking downloading operation, even tried with simple NSURLConnection operation, connection fails if you keep your app running, and lock screen and then unlock. Works absolutely fine in background though.
Any one encountered similar problem with NSURLConnection want to share some solution?
Thanks.
It looks like an iOS bug. Weird, but lock screen action affects NSURLSession somehow, so that it stops working and returns NSURLErrorNetworkConnectionLost. So in my app I gave up using shared session. I either use a new session object for every request or (if I need to maintain one session constantly) recreate it every time the screen gets unlocked. And it works. For users of AFNetworking or any other third party library working on top of NSURLSession the situation is harder, of course. You'll need to correct the code of the library, which is definitely not a good thing, but I think there's no other choice
Very helpful Andrey Chernukha,
In my case, figured out that you don't necessary need to recreate new session every time.
I ended up using array to save running NSURLSessionDataTasks and after phone is unlocked resume them.
Steps:
I created array NSMutableArray *dataTasksToResume
In - (void)applicationWillResignActive:(UIApplication *)application I saved all tasks to dataTasksToResume array
Cancel all running NSURLSessionDataTasks
In - (void)applicationDidBecomeActive:(UIApplication *)application get all tasks from array and resuming them (re-creating them)
Enjoy!
Hope it helps.

Close the app in 'applicationDidEnterBackground'

I have an iOS app and for several reasons I need to close it when the user clicks the home button of the device. I can't support background.
I now there is an option called Application does not run in background, but I can't use it because there is a bug from Facebook sdk that makes impossible to authenticate with Facebook when this option is in use. Here is the bug report at Facebook.
So I don't know what to do, how can I restart it? An [[NSThread mainThread] exit] in applicationDidEnterBackground?
Is there a workaround for this?
The legal (Apple will approve the app) way is set or create the key UIApplicationExitsOnSuspend to YES in the Info.plist.
To get this as action you would need exit(0) or [[NSThread mainThread] exit], but this is against the Human Interface Guidelines from Apple
So what you could also do is just design all your views in a manner that they have a reset function . Then when applicationDidEnterBackground store that you need to reset on next startup or directly call reset on all active views in applicationWillEnterBackground
So your App didn't really restart, but all your views look like this. To give you an advice, I would design my app in a manner that it doesn't shutdown itself when i quit. This is no good user experience.. Once clicked the home button and all your data is away, same when you receive a call.
Plain old stdlib.h exit() will terminate the app, though it violates apple guidelines (see "Don't Quit Programmatically").
Your app won't perform any background actions if you don't initiate any.

Exit an application or Go to Dash board(main page) programmatically - IOS

I want to exit my application programatically, I googled, some people suggesting to use exit(1), but apple is not supporting that I guess. If it is the case, How do I exit my application programatically. Any helps appreciated.
exit(0); will work but don't use it
You shouldn't force close an app as the standard way to terminate an application is to press the home button (or use the multitasking bar)
Don’t Quit Programmatically
Never quit an iOS application programmatically because people tend to
interpret this as a crash. However, if external circumstances prevent
your application from functioning as intended, you need to tell your
users about the situation and explain what they can do about it.
Depending on how severe the application malfunction is, you have two
choices.
Display an attractive screen that describes the problem and suggests a
correction. A screen provides feedback that reassures users that
there’s nothing wrong with your application. It puts users in control,
letting them decide whether they want to take corrective action and
continue using your application or press the Home button and open a
different application
If only some of your application's features are not working, display
either a screen or an alert when people activate the feature. Display
the alert only when people try to access the feature that isn’t
functioning.
Source
I believe u are not reading the comment properly thus posting the answer for ur question here:
"Simply Don't do that. as apple does not allow application to crash like that."
look at here. How do I exit my iOS app gracefully after handling a Local Notification and here Exit application in iOS 4.0 there are fare discussion over here.
After the release of iOS4, multitasking(new feature) was added by APPLE. This feature enabled the users to keep the app into suspended state in the background if in between he has to do some other activity(e.g. picking up phone call). So Apple considers your app should be maintained in the background until the user deletes the application from the background. And after this if you want to exit use exit(0);, using this would further lead to rejection from AppStore
Here's a wrong way to accomplished exit function in your app. This is coming to mind when I read your question, never applied anywhere, so be careful if you'll gonna implement this!
- (void) exitApp
{
NSArray *array = [[[NSArray alloc] init] autorelease];
NSLog(#"%#",[array objectAtIndex:10]); //will crash here, looks like exit.
}
P.S. You can put this code inside your UIAlertView asking exit confirmation like Do you really want to exit?. In YES button pressed you can call [self exitApp]; User think that he'll exit from the app.

How programmatically restart an iPhone app in iOS

How programmatically restart an iPhone app in iOS?
I find this way http://writeitstudios.com/david/?p=54
But may be something simple.
The only way I know to do this is not ideal, but it works.
First, your app has to opt out of background execution (multitasking) The app has to quit when exited, not run as a background task. This is done with the plist key UIApplicationExitsOnSuspend.
Second, your app needs to register a custom URL scheme that can be used to launch the app.
Third, you need a web page hosted somewhere that when loaded will redirect to your app's custom URL scheme.
Forth, the user needs an active Internet connection.
To exit and restart, call UIApplication openURL on your hosted redirecting web page. Your app will exit and safari will launch and load your page. The page will redirect Safari to your custom URL scheme, prompting Safari to internally call openURL, causing iOS to launch your app.
my post that you linked to is referring to a Cocoa Application, not the iOS. On the iOS, you can quit an application (but Apple doesn't like this) by using exit(0); but I don't recommend that. You cannot restart iPhone apps though.
Unless you're developing for jailbroken devices, Apple won't even allow you to programatically terminate your app. So restarting the device is out of the question.
Your AppDelegate instance has a method
(void)applicationDidBecomeActive:(UIApplication *)application
{
}
In here, you can put logic to figure out if the app should restart, or continue doing whatever it was doing. For example you can have a BOOL variable appMustRestart that is false at first but gets triggered as true whenever something happens in your app that you'd like the next time to be a fresh relaunch.
if (appMustRestart)
{
[self resetVars]; // call a method that resets all your vars to initial settings
// INSERT CODE HERE TO TRANSFER FOCUS TO INITIAL VIEWCONTROLLER
}

Resources