iOS - setIdleTimerDisabled:NO Not Working - iphone

I have an app that utilizes the video camera so the screen cannot dim. Inhibiting the screen from dimming works fine like so:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
However when the app is closed and enters the background, setting the IdleTimer back to NO is not working. The screen stays on forever on the home screen. This is how I am trying to accomplish this.
- (void)applicationWillResignActive:(UIApplication *)application
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
Is there a better place to add this line of code?

The same problem was occuring with me. Actually it does work but only works when your device is not connected to xCode. Try disconnecting the device and then test this functionality.

Why don't you try using your code in
applicationWillEnterForeground
delegegate method.
Hope that work's for you.

Swift version:
public func applicationWillResignActive(application: UIApplication) {
UIApplication.sharedApplication().idleTimerDisabled = false
}
public func applicationDidBecomeActive(application: UIApplication) {
UIApplication.sharedApplication().idleTimerDisabled = true
}

Related

Status Bar Still Showing

I am getting REALLY frustrated!!
I have tried every living possibility to get rid of the UIStatusBar at the top of my app...
I have tried:
Setting Status Bar to "None" in IB
Running [[UIApplication sharedApplication] setStatusBarHidden:YES]; on application launch AND in each scene.
Going to the .plist and changing the value for Status Bar Hidden at Startup: YES
Setting that same value on the home page for the target
Setting - (BOOL)prefersStatusBarHidden
{
return YES;
} in the app delegate
Literally none of this works... It still shows up on all of my views, and it is SUPER frustrating
Thanks again :)
Side note: I'm in xcode 5, developer beta iOS 7 beta 6, but this also happens on my old ios6 and xcode 4 apps
Please try this
//viewDidload
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self prefersStatusBarHidden];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
// Add this Method
- (BOOL)prefersStatusBarHidden
{
return YES;
}
This code has been taken from this link
What I usually do is add two key-value properties to the Info.plist file.
The properties source code is:
You need to add a method to the view controller, and not to the app delegate as you write.
- (BOOL)prefersStatusBarHidden
{
return YES;
}
As something occured to me!!
for anyone else ,,
Make sure you are modifying the info.plist in the right *TARGET* :/
plus the accepted answer.

How to create a custom alarm which can be set for random days in a week without using local notifications?

I have studied some of the following questions, that are::
1.) How to set alarm for selected days in iphone?
2.) iPhone alarm using repeated local notifications
3.) How to set the alarm in iPhone and save in local notification?
but they all are using local notifications. I am facing problem with local notification as I have to use three buttons on an Alarm View which are: Snooze, Ignore and Okay. I have to perform custom actions on each button click.
Please help me with this stuff.
Suggestions accepted.
Thanks in advance.
Kuldeep.
In you app delegate...
- (void) presentWidget: (NSString*)theDisplayedText {
BOOL widgetIspresent = [WidgetVC widgetIsCurrentlyPresented];
if (!widgetIspresent) {
WidgetVC *widgetVC = [[WidgetVC alloc] initWithNibName:#"WidgetVC" userInfoString:theDisplayedText bundle:nil];
widgetVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
widgetVC.userInfoStr = theDisplayedText;
[mainScreenManager presentViewController:widgetVC animated:YES completion:nil];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification didFinishLaunchingWithOptions %#",localNotif);
NSString *theDisplaytext = [localNotif.userInfo valueForKey:#"someKey"];
//here we have to handle whatever notification was pressed - that might also be an old aready passed one
//this all is checked when the widget opens - it will show if the notification is OK or too old
[self presentWidget: theDisplaytext ];
} else {
//so we started the app normally, not via local notification
[self presentWidget];
}
return YES;
}
// Handle the notificaton when the app is running
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotif {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification didReceiveLocalNotification %#",localNotif);
NSString *theDisplaytext = [localNotif.userInfo valueForKey:#"someKey"];
[self presentWidget: theDisplaytext];
}
You need a way to start the UIViewController for the widget, I just created a mainScreenManager helper.
So on the widgetVC you have your "Snooze" and "OK" while the "Ignore" is just the ignoring of the notification itself.
Hope this gets you on an usable track...
ps I ported my app from Android to iPhone, that's why I used the name "widgetVC" for this screen. On Android it is implemented as a widget. Hope no iPhone lover feels offended :-)

Iphone - UISwitch Turned Off On Exit

I want to set a switch to off when the app enters into background. I am trying to do it using the following code. It isn't working :( When I resume app the switch is still "on" on animation on the screen. How can I fix this?
- (void)applicationWillResignActive:(UIApplication *)application {
[registrationSwitch setOn:NO animated:YES];
}
You need to set the value in userDefaults. Add a boolean check like:
if (userDefaultBool == 1){
registrationSwitch setOn
}
else{
registration setOff
}
put that in your viewDidLoad

Prevent iOS from taking screen capture of app before going into background

You all might know that iOS takes a screen shot of your application before throwing it into the background. This is usually for a better User experience like quick animation to bring the app back and so on. I don't want my app screen shot to be stored on the device, but I want the multitasking to still exist.
I came out with a solution but I'm not sure if I'm heading in the right direction. So, when the applicationDidEnterBackground is called -- I put in an overlay image that will be captured by the OS, and once the app enters foreground, I will remove the overlay. I'm not sure if this is going to work but I'm on my way to implement this. Meanwhile, any other thoughts on this will help me figure out the optimal way of attacking this issue.
You are on the right track. This is Apple's recommended way to do this as noted in the iOS Application Programming Guide:
Remove sensitive information from views before moving to the background. When an application transitions to the background, the system takes a snapshot of the application’s main window, which it then presents briefly when transitioning your application back to the foreground. Before returning from your applicationDidEnterBackground: method, you should hide or obscure passwords and other sensitive personal information that might be captured as part of the snapshot.
Need to write the code in Application life cycle methods, here we are putting an imageView while the app animate to background :
-(void)applicationWillResignActive:(UIApplication *)application
{
imageView = [[UIImageView alloc]initWithFrame:[self.window frame]];
[imageView setImage:[UIImage imageNamed:#"Splash_Screen.png"]];
[self.window addSubview:imageView];
}
Here is the code to remove the imageView:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(imageView != nil) {
[imageView removeFromSuperview];
imageView = nil;
}
}
It is working and properly tested.
I came across the same issue, and my research has lead me to the following answers:
set a blurry screen overlay before the app goes in the background and once the app becomes active remove this overlay
if it is iOS 7 or later you can use the function
ignoreSnapshotOnNextApplicationLaunch
See in apple documentation:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/ignoreSnapshotOnNextApplicationLaunch
I hope this helps somebody.
Working methods in AppDelegate, swift 4.2:
func blurScreen(style: UIBlurEffect.Style = UIBlurEffect.Style.regular) {
screen = UIScreen.main.snapshotView(afterScreenUpdates: false)
let blurEffect = UIBlurEffect(style: style)
let blurBackground = UIVisualEffectView(effect: blurEffect)
screen?.addSubview(blurBackground)
blurBackground.frame = (screen?.frame)!
window?.addSubview(screen!)
}
func removeBlurScreen() {
screen?.removeFromSuperview()
}
Where is:
weak var screen : UIView? = nil // property of the AppDelegate
Call these methods in needed delegate methods:
func applicationWillResignActive(_ application: UIApplication) {
blurScreen()
}
func applicationDidBecomeActive(_ application: UIApplication) {
removeBlurScreen()
}
Your approach is exactly the correct and only way to do it. Place an overlay view and remove it later. It is valid to do this if your app shows sensitive data that you don't want to be cached in image format anywhere.
Apple Doc
https://developer.apple.com/library/archive/qa/qa1838/_index.html
Note: Your implementation of -applicationDidEnterBackground: should not start any animations (pass NO to any animated: parameter). The snapshot of your application's window is captured immediately upon returning from this method. Animations will not complete before the snapshot is taken.
Converted Apple code in swift 4.2 App delegate i declared
func applicationDidEnterBackground(_ application: UIApplication) {
// Your application can present a full screen modal view controller to
// cover its contents when it moves into the background. If your
// application requires a password unlock when it retuns to the
// foreground, present your lock screen or authentication view controller here.
let blankViewController = UIViewController()
blankViewController.view.backgroundColor = UIColor.black
// Pass NO for the animated parameter. Any animation will not complete
// before the snapshot is taken.
window.rootViewController?.present(blankViewController, animated: false)
}
func applicationWillEnterForeground(_ application: UIApplication) {
// This should be omitted if your application presented a lock screen
// in -applicationDidEnterBackground:
window.rootViewController?.dismiss(animated: false) false
}
Improvement in Depak Kumar post :
Make a property UIImage *snapShotOfSplash;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];
snapShotOfSplash =[UIImage imageNamed:#"splash_logo"];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
self.overlayView = [[UIImageView alloc]initWithFrame:[self.window frame]];
self.overlayView.backgroundColor = [UIColor whiteColor];
[self.overlayView setImage:snapShotOfSplash];
[self.overlayView setContentMode:UIViewContentModeCenter];
[self.window addSubview:self.overlayView];
[self.window bringSubviewToFront:self.overlayView]; }
- (void)applicationDidBecomeActive:(UIApplication *)application {
if(self.overlayView != nil) {
[self.overlayView removeFromSuperview];
self.overlayView = nil;
}
}
Implementation with some animation while going in background and reverse action
- (void)applicationWillResignActive:(UIApplication *)application
{
// fill screen with our own colour
UIView *colourView = [[UIView alloc]initWithFrame:self.window.frame];
colourView.backgroundColor = [UIColor blackColor];
colourView.tag = 1111;
colourView.alpha = 0;
[self.window addSubview:colourView];
[self.window bringSubviewToFront:colourView];
// fade in the view
[UIView animateWithDuration:0.5 animations:^{
colourView.alpha = 1;
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// grab a reference to our coloured view
UIView *colourView = [self.window viewWithTag:1111];
// fade away colour view from main view
[UIView animateWithDuration:0.5 animations:^{
colourView.alpha = 0;
} completion:^(BOOL finished) {
// remove when finished fading
[colourView removeFromSuperview];
}];
}
swift 4.0 version.
for use custom icon
first add this line at top of AppDelegate
var imageView: UIImageView?
and add this:
func applicationDidEnterBackground(_ application: UIApplication) {
imageView = UIImageView(frame: window!.frame)
imageView?.image = UIImage(named: "AppIcon")
window?.addSubview(imageView!)
}
func applicationWillEnterForeground(_ application: UIApplication) {
if imageView != nil {
imageView?.removeFromSuperview()
imageView = nil
}
}
background with black color
func applicationDidEnterBackground(_ application: UIApplication) {
let blankViewController = UIViewController()
blankViewController.view.backgroundColor = UIColor.black
window?.rootViewController?.present(blankViewController, animated: false)
}
func applicationWillEnterForeground(_ application: UIApplication) {
window?.rootViewController?.dismiss(animated: false)
}
In iOS 7 you could use the allowScreenShot to stop the ability all together.
See: Apple Developer: Configuration Profile Reference:
allowScreenShot
Boolean
Optional. If set to false, users can’t save a screenshot of the display and are prevented from capturing a screen recording; it also prevents the Classroom app from observing remote screens. Defaults to true.
Availability: Updated in iOS 9.0 to include screen recordings.

applicationWillEnterForeground never called

Hey there, I'm trying Multitasking in the Simulator (I only have a 2nd gen iPod and an iPad) and I'm still having some problems. My testing methods look like this:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"Entering %s",__FUNCTION__);
if (enteredFromBackground) {
NSLog(#"Entering from Background");
enteredFromBackground = NO;
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"Entering %s",__FUNCTION__);
enteredFromBackground = YES;
}
Unforntunately, I'm not seeing the NSLog from applicationWillEnterForeground, that's why I added the line to show me something in applicationDidBecomeActive.
All I get is the
2010-11-20 15:58:12.796 iBeat[45997:207] Entering -[AppDelegate_Shared applicationDidEnterBackground:]
2010-11-20 15:58:18.160 iBeat[45997:207] Entering -[AppDelegate_Shared applicationDidBecomeActive:]
After having this problem in iOS 13, I found out that I was waiting for applicationWillEnterForeground(_ application: UIApplication) to be called instead of sceneWillEnterForeground(_ scene: UIScene).
For more information, read this answer:
enter link description here
Finally I found my problem!
Since I have a universal Application, I have an Appdelegate_Shared, an Appdelegate_iPhone, and an Appdelegate_iPad.
I had an empty implementation of "applicationWillEnterForeground" in the two subclasses but didn't call super!
And then I wondered why the method in Appdelegate_Shared never got called o.O