iOS - On ipad standby my application gets stuck - iphone

I am using the following code to disable and enable View.
TO DISABLE VIEW.
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
_loadingSpinner.hidden = FALSE;
TO ENABLE VIEW.
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
_loadingSpinner.hidden = true;
but when device goes into standby mode. application freezes. Users have close the application in the task pane in order to run it again.
Please HELP!!!!

Why are you disabling the view? If you are wanting to prevent interaction while a process completes, why not use something like MBProgressHUD or make your own Modal view and use that?

Put a breakpoint and check weather the code to end the interaction is hit at any time.
Why dont you use a overlay view with the spinner or userInteractionEnabled Property of UIView for the purpose?

Related

How to turn Auto-Lock to Never in Swift?

Is there any Swift command to actually set the Auto-Lock to Never or a specific time period? I want to create a simple app that only has two buttons: one is to set the Auto-Lock to Never and the other one is to set it back to iOS default (1 min).
So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps. If s/he is done with other apps, s/he can open this app again and tap the Default button to set the Auto-Lock back to 1 min.
I understand this can be done from the Settings but I am just curious how I can do it from the backend using Swift.
I am new to Swift, btw.
Thanks much!
So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps
You can't do that. You are sandboxed. You cannot affect what happens to the user while running some other app.
When app is open, try to disable the idleTimer in viewDidLoad
UIApplication.shared.isIdleTimerDisabled = true
When app is closed to open other apps, try to -enable again idleTimer when yourViewController disappears, so put this in viewDidDisappear
UIApplication.shared.isIdleTimerDisabled = true
You need to make use of this API call to set the idle timer disable and enable.
This is in objective-C. Just convert it to swift. The API is available in UIApplication.h
-(void) onApplicationDidActivate:(NSNotification*) notification
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
-(void) onApplicationWillDeactivate
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Screen Time out hold down during some process

I'm downloading some file with FTP helper, it takes at least 1 to 1.30 minute during downloading process. Can i stop screen time out process Until my download is complete.
Because when screen time out then app goes in background process and FTP does not works in background i tried background thread process but did not work.
So please tell me to hold down screen and active screen while I'm downloading file.
may be bellow piece of code help's you, You can. To stop your app from timing out and going to sleep you can use:
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
and you can re-enable it
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
You aren't able to force your application to stay in the foreground while a file is being downloaded. Consider adjusting for error states in the case of an incomplete download, and also creating a progress indicator so users know that a download is taking place and should not close the application.
To stop your iPhone from getting locked you can use this code before you are starting the downloading process
[UIApplication sharedApplication].idleTimerDisabled = YES;
and when the download is completed
[UIApplication sharedApplication].idleTimerDisabled = NO;
Hope it helps :)
Googled lot for this answer.
I found that the screen time out process is dimming process when screen is in idle time then
counter start for screen time out and when counter reaches for specific screen time set by user to lock screen it will lock the screen.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
here is the code that disable counter during your process while you are downloading some files then place the above code in that function.
and you can start counter with simple 'NO' . better to place 'didunload' or 'willdisapper' method.
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];

How to know if a task is executing in background

In my app I'm downloading lots of images on a method.
I'm using a
downloadTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:downloadTask];
downloadTask = UIBackgroundTaskInvalid;
}];
This is working fine, if I press the home or sleep button, the images continue downloading.
I'm showing the progress in a UIProgressView inside an UIAlertView, and when the percent is 100% the alertView is dissmised and I change the viewController to other where I show the donwloaded images.
But I only want this to happen if the app is really active at the moment the download finish.
I have been looking at the app state and while it's downloading with the screen off.
[UIApplication sharedApplication].applicationState
the state is UIApplicationStateActive during all the donwload
How can I can know if the downloading is happening with the screen off or on?
EDITED AFTER ACCEPTING THE ANSWER:
I just discovered, if I tap the home button, the app enters in UIApplicationStateBackground, if I tap the wake/sleep it enters in UIApplicationStateInactive
Following the approach of the correct answer, my app contines donwloading in both cases.
The screen is off in two states (apart from when the app has not been even opened):
suspended : in this case you don't have to worry because the download won't procede until the app gets active again; It will enter this state on
background : it's in this state for a limited amount of time before going in suspend, and the screen is already off in this moment. Here you may want to check then whether to do all the things you said or not, because in this state code can be still executed. In this state the app status is UIApplicationStateBackground, so you could just perform a check like this:
You probably want to check whether the app is in background execution in order to achieve the result. Just like this:
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateBackground) {
// Do stuff
}
If it's in background, so the screen is off.
UPDATE: after few test, what I figured out is that the behaviour you are expieriencing is probably due to the execution of the download on the main thread.
You should send the download on (for instance) the global queue. This way the application will enter the background state as expected:
....
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.bti];
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doBackgroundStuff];
});
....
This way, if the app is put on background while the download is in progress, the application state will turn into UIApplicationStateBackground, and then you can check it as I wrote initially. If you are doing UI updates during the progress remember to send them back to the main thread (because the download is now on a different one).
You can check whether your app is running in the background or not by setting a flag in the designated application delegate methodsapplicationDidEnterBackground: and applicationWillEnterForeground:. Example:
- (void)applicationDidEnterBackground:(UIApplication *)application
_applicationRunsInForeground = NO;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
_applicationRunsInForeground = YES;
}
If you don't want to have this _applicationRunsInForeground flag inside your application delegate, you could observe the delegate's NSNotifications in your viewcontroller class instead (UIApplicationWillEnterForegroundNotification and UIApplicationDidEnterBackgroundNotification).

iOS uiwebview clear touch event history

I am disabling setUserInteraction for a uiwebview. After 5 seconds I enable it again. Problem is that if user touches it during this period of 5s, not execute an action but after this timeout, action is executed! How to disable userInteraction completely or clear event history before setting its interaction again? thank you.
Try using
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
and
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
instead.
Though this disables user interaction for whole application so it might not be the right workaround for you.

creating new view in iPhone application

i just finished my first view in my iPhone app and i want to add a second view to wich the user go when he click in the button in the first view. So i add new view from the librairy window, but i don't see the battery in the top, is that normal ? and how to add it please ??
Assuming you mean the status bar along the top you can try this code to make sure it is shown:
[[UIApplication sharedApplication] setStatusBarHidden:NO];
or:
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
if you want to animate it coming down.
Cheers.