Application does not run in background simply does not work - ios

I have read over many stack overflow questions where people ask to terminate their app oppose to let it run in the background.
The main answer I found was to set the application does not run in background BOOL to YES in my info.plist
I have done this and cleaned my project but still my application runs in the background when the user presses the home button. This solution simply does not work.
What can I do to make my application quit when a user presses the home button.
My app is currently running on iOS 6.
Any help is appreciated :)

This answer is for your first comment, not the original question. Have your iPod view controller register for the UIApplicationDidEnterBackgroundNotification notification. The implementation should stop the music. This is a much better user experience than choosing to have your app terminate on suspend.
// Put this in a good place like viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(backgrounding) name:UIApplicationDidEnterBackgroundNotification object:nil];
// Handle the notification
- (void)backgrounding {
// app is leaving the foreground, stop the music
}
// In your dealloc method add:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

Related

UIUserNotificationSettings Alert Causes App to Resign Activity

I currently have an application that requires Local Notifications, so naturally I must ask the user if he or she would like to 'Allow Notifications'. Here is how I am doing this:
AppDelegate.m
// Local Notifications
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
At the initial loading of the app, I have a AVAudioPlayer that begins playing. I have it set up so that when the user leaves the app (the application enters the background) the music fades out and pauses.
The problem is that during the first time the app is launched, when the notification pops up asking for the user to allow notifications, it appears that the app is fooled into thinking it is about to resign activity. I can't seem to figure out how to either avoid JUST the notification alert from triggering this, or at worst "bring the app back", and becoming active once the alert is dismissed with one of the 2 notification options.
Here is what I am calling in my ViewController to notify the app when it changes state:
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pauseAudio)
name:UIApplicationWillResignActiveNotification object:nil];
...
}
If someone could tell me how to ignore the notification alert so that it does not think it will be resigning activity, OR how to bring back the application after it does "resign". I currently have these other notification handles in attempt to bring it back, but they are not called after the alert "messes everything up":
(in the same ViewController within viewDidLoad)
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(animateLoginScreen)
name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumeAudio)
name:UIApplicationDidBecomeActiveNotification object:nil];
I appreciate any help on this one. Thanks!
Whenever a "system dialog", such as the notification permission dialog, is shown your application does resign active because it is iOS, not your app, that is responsible for processing the permissions request.
Your app isn't 'fooled', it really is resigning active for the period of time that the dialog is displayed. I suspect that you should pause your audio on your application entering the background, not merely on it resigning active. UIApplicationDidBecomeActiveNotification should be posted if you are getting a willResignActive though. Do you get a call to the corresponding AppDelegate methods?

Method in viewWillAppear and viewDidLoad didn't loaded

I have created a method to check the status of a server in my viewcontroller, I need to check this, everytime I will open the app.
I call [self checkStatus]; from viewWillAppear and viewDidLoad, but when I open the app, by clicking home button, and I try to open the app again (clicking the app icon in applications) this method is not called. I have a NSLog to view when it is launched or not.
I'm frustrated. Thanks for your help.
You can react to app changes using NotificationCenter:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doSomething:) name:UIApplicationDidBecomeActiveNotification object:nil];
BTW: don't forget to removeObserver when you don't need it!
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
You can also use UIApplicationWillEnterForegroundNotification etc, depending what do you need.
You should read about app lifecycle on Apple Developer pages :). Read:
AppleDeveloperLink Especially section: "Execution States for Apps" to know more about app lifecycle.
StackOverflowLink to know more about view lifecycle.
iOS is not calling those methods again, but the delegate methods in the AppDelegate. You have to propagate the message to your controllers then.
I hope this will help you: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/

Run Loop to set color to button ios?

I've got sort of a problem with my ios application. It handles the Apple Push Notification (APN) and everything BUT, my app handle it in it appDelegate.m
The problem is I want to make a button to change color if the user have a notification, so I search a way to do it and I found "Run Loop", is this possible to have a Run Loop in a secondary thread to check infinitely if there is a new notification? (with a wait of 5/10 seconds between each verification) I don't really know how to declare it neither to make it works alone. (I'm pretty new with xCode and used the AFNetworking asynchronous methods for the rest of the app so I'm not really good with threading and co.)
It could be really great if someone is able to give me a way to implement a simple run loop in my app.
Thank you!
EDIT : I have a white button for User profile, and I want it to change to Red when the user have a notification, the button is already declared and works in a viewController, but I don't manage to link it to the appDelegate, that's why I'm asking to do a RunLoop who could handle a BOOL sent by the appDelegate, that my view do only in the viewDidLoad (that don't really work when app is in background etc...)
I don't really see why you need a Run loop?
You could use NSNotificationCenter. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
AppDelegate.m
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"didRecieveNotification" object:nil userInfo:userInfo];
}
}
And add your ViewController where the button lives as a observer:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"didRecieveNotification" object:nil];
}
-(void)handleNotification:(NSNotification *)notification
{
YourButton.backgroundColor = [UIColor redColor];
}

How hide or close my app when i reveive a call?

I have a little bug. I'm developing an iOS App.
If i receive a call, my app stays open and the screen for my entering call appears on my app. I would like to close my app if i have a call.
How can i fix that?
Thanks,
J.
The green, in-call status bar is not a bug but a feature. You don't need to close the app when the call comes.
Instead, make sure your views are resized properly when the in-call status bar appears.
As Per Apple Human Interface guidelines
Never quit an iOS app programmatically because people tend to interpret this as a crash.
However, if external circumstances prevent your app 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 app 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 app. It
puts users in control, letting them decide whether they want to take corrective action and
continue using your app or press the Home button and open a different app
If only some of your app's features are unavailable, display either a screen or an alert
when people use the feature. Display the alert only when people try to access the feature
that isn’t functioning. `
But again you handle your app accordingly when call comes by using the following notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(incomingCall:) name:CTCallStateIncoming object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(callEnded:) name:CTCallStateDisconnected object:nil];
Srinivasan N's answer has the incorrect observer, you'll want to add this observer which will account for all scenarios: phone calls, Personal Hotspot, GPS/navigation, etc.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(adjustViews:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
}
- (void)adjustViews:(NSNotification *)notification
{
NSValue *rectValue = [[notification userInfo] valueForKey:UIApplicationStatusBarFrameUserInfoKey];
CGRect newFrame;
[rectValue getValue:&newFrame];
NSLog(#"Changed frame to: Width: %f, Height: %f", newFrame.size.width, newFrame.size.height);
// Adjust your views here
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Is it possible that NSNotificationCenter doesn't work on certain devices?

Am using NSNotificationCenter in my iPhone App to post a notification
// I add an observer in didFinishLanchingWithOptions() in the AppDelegate
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(getData:) name:kNotif_GetData object:nil];
....
....
// then in another method, I post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:kNotif_GetData object:nil];
....
One user says that this functionality (which is executed by getData()) is not working. so that might mean that the notification hasn't gotten fired. Any idea why this might happen? When I test it with different devices, it works perfect. The user uses iPhone5 and iOS7. Is there any more setting or any explicit user setting which is needed or which could turn this on/off?
No, notification centre is reliable.
"Not working" is not enough information to diagnose the problem. Get more information from the user or add more logging so that you know what is actually going on.

Resources