Iphone - UISwitch Turned Off On Exit - iphone

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

Related

Status Bar still appearing in iOS 7 despite correct PList settings

I've just upgraded to XCode 5 and iOS 7. I've read all the suggestions I can find, but still getting the status bar appearing over the top of my apps.
I've tried setting View controller-based status bar appearance to NO in my plist:
I've tried adding:
- (void)viewDidLoad
{
// …
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)]) {
// iOS 7
[self prefersStatusBarHidden];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
} else {
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
// …
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
to my root view controller.
I don't know what else to try. Am I missing something obvious?
UPDATE
I've found that the status bar is only present on a couple of targets, while other targets running the same code don't have the status bar. I've checked all their plists... The only difference with the ones showing the status bar are that they display an advert bar at the top of the screen... I wonder if this could have something to do with it?
Use - (BOOL)prefersStatusBarHidden
{
return YES;
} in all of your view controllers. Good Luck!
In application .plist add this key:
UIViewControllerBasedStatusBarAppearance and set it "NO"
I ended up deleting the plist file and copying one from another target that did work, and then changing the necessary values. The lists were identical, however this appeared to fix it. I had already tried clean building, reseting the simulator etc, so I guess it was a bug in the plist/xcode.
Try to add the next code in your root view controller:
- (BOOL)prefersStatusBarHidden { return YES; }
Less hacky solution is to insert
[application setStatusBarHidden:YES]
in AppDelegate.m in method
- (BOOL) application(UIApplication *)application didFinishLaunchingWithOptions:(NSDirectory *)launchOptions
like Dipen Panchasara
his: [UIApplication sharedApplication] delivers exact the application given in the methodcall.
Because changing a method to allways return YES feels not right

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 tell if leaving iOS app entered foreground from fast-app switching or manually?

Is there a way to tell if an iOS app enters the foreground from fast-app switching or manually? I need to know by the time applicationWillEnterForeground is called, so some specific code can be executed (or not executed) depending on the condition in which the app entered the foreground.
EDIT:
It turned out that this was more of a design issue for me. I moved my code to applicationDidBecomeActive. I also added a BOOL property to the appDelegate called fastAppSwitching (probably the wrong name for it). I set this to YES in application:handleOpenURL and application:openURL:sourceApplication:annotation. Then I added the following code to application:didFinishLaunchingWithOptions:
if (launchOptions) {
self.fastAppSwitching = YES;
}
else {
self.fastAppSwitching = NO;
}
In applicationDidBecomeActive, I used the following code:
if (fastAppSwitching == YES) {
self.fastAppSwitching = NO; //stop, don't go any further
}
else {
...
}
EDIT2: MaxGabriel makes a good point below: "Just a warning to others taking the solution described here, applicationDidBecomeActive: is called when the user e.g. ignores a phone call or text message, unlike applicationWillEnterForeground". This is actually also true for in-app purchases and Facebook in-app authorization (new in iOS 6). So, with some further testing, this is the current solution:
Add a new Bool called passedThroughWillEnterForeground.
In applicationWillResignActive:
self.passedThroughWillEnterForeground = NO;
In applicationDidEnterBackground:
self.passedThroughWillEnterForeground = NO;
In applicationWillEnterForeground:
self.passedThroughWillEnterForeground = YES;
In applicationDidBecomeActive:
if (passedThroughWillEnterForeground) {
//we are NOT returning from 6.0 (in-app) authorization dialog or in-app purchase dialog, etc
//do nothing with this BOOL - just reset it
self.passedThroughWillEnterForeground = NO;
}
else {
//we ARE returning from 6.0 (in-app) authorization dialog or in-app purchase dialog - IE
//This is the same as fast-app switching in our book, so let's keep it simple and use this to set that
self.fastAppSwitching = YES;
}
if (fastAppSwitching == YES) {
self.fastAppSwitching = NO;
}
else {
...
}
EDIT3: I think we also need a bool to tell if app was launched from terminated.
If your application is launched by another application, the
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
sourceApplicarion:(NSString *)bundleID
annotation:(id)info;
method is called on your app delegate. You can use this method to e. g. set a Boolean switch to true that indicates whether the app was launched by another program.
The peoblem is that this method is called after applicationWillEnterForeground:, so you can't tell in that method whether your app was launched manually or automatically.
However, I suspect that if you need this to be detected in a particular method, you may have a design problem and you should probably reorganize your code.
In the case where your app is opened from another app, application:openURL:sourceApplication:annotation will be called on your app delegate.

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 :-)

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