NSNotificationCenter Add Observer not working in iOS - ios

I am working a two applications in which one application get invoked by other application. If one of the application has been quited (Neither not in foreground or background), [[NSNotificationCenter defaultCenter] postNotificationName: was not working for very time time. If the application running in background, then every thing working as expected. I dont what going on. Some one please give me guide lines.

Related

Why is my iOS app getting launched automatically?

I have an audio app that plays when being launched. It has CarPlay entitlements, plays from the network and naturally plays in the background.
Now, every one in a while the phone launchs the app in the background without notice. Music simply starts to play randomly. I can't determine the cause or the situation in which this reproduces. It might be related to Bluetooth connections, phone calls, CarPlay interactions, network changes or other reasons.
I do know the following things for sure:
The app is launched from scratch as I have examined remote logs and seen the app is being launched.
Pausing the music from control center will kill the app, and it will be relaunching after 2-3 minutes again (and again)
There are not too many clues about your issue.
But I would try to find some possible causes base on my experiences.
First of all, check the notifications/listeners that you use in the app.
Something would be like bellow:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
for this one, once the network status changed, you would get the notification, and you might do something after that(play the music).
Second, check out if there is Timer in the app or not, it would cause problem if
the timer was set improperly and you do something by the timer.

download files when user press back or close the app and restart the app in iOS

I am very new to iOS and I want to download the database from the server in iOS App. It works perfectly first time when i run the app from Xcode in simulator.
After download is finished,I pressed command+shift+H to open the home screen, as there is no home/back button in the simulator. Again I opened the app but it doesn't download the database this time.
I have kept the download code in viewWillAppear and viewDidLoad both and its downloading the db twice at the same time.
My purpose is to download the file when user resumes/restart the app. I have done same thing in android, where onResume/onPause/onRestart methods are available. I want to achieve the same thing ion iOS also.
One doubt......
How to press back button in iOS simulator?
I think back button starts the app from start (At least in android) not home button. How can I test this in iOS simulator.
If you really need to download your data every time the view is shown (I really don't recommend this), remove the download code from viewDidLoad and keep the one from viewWillAppear.
command+shift+H is the right way to press the home button on the simulator.
You have to handle application states. refer below document from apple -
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html
handle those from appDelegate or
You can handle those by adding the observers like below
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(backgroundMethod)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(foregroundMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];

Is applicationDidBecomeActive called in every app?

I'm making a jailbreak tweak for my iOS device and have run into a problem regarding application bundleIDs and multitasking.
I currently have it set up so that when an application icon is tapped, it grabs the application ID and stores it for later. I needed to get this for multitasking so I tried applicationDidBecomeActive with no luck as it was never called when I thought it would be. Is there any way to tell when an application loads from multitasking and/or app switching?
You can register notification in your viewDidLoad:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(selectorYouWant)
name:UIApplicationDidBecomeActiveNotification
object:nil];
Here are all the UIApplicationDelegate methods that may be called. Upon first launch application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: will be called when initially loading the app when it is launching for the first time/is no longer in memory. As for becoming active while still in memory I would recommend you use applicationWillEnterForeground:.

Xcode - How to deal with notifications posted into NSNotificationCenter while application was in the background as soon as app comes to foreground

I am currently dealing with Smartcard reader connected to a IOS device as an external accessory. When application goes to the background and remains there for a few (let's say 10-15 seconds) the reader is disconnected by iOS in order not to drain battery. This sends notification to NSNotificationCenter that the reader (EAAccessory) has been disconnected. When app comes to the foreground, it usually takes some time till the reader is connected back.
I am able to handle with these notifications using following methods:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(EAaccessoryConnect) name:EAAccessoryDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(EAaccessoryDisConnect) name:EAAccessoryDidDisconnectNotification object:nil];
The problem is, sometimes when application is in the background for a longer time (longer than 20 minutes), more than one EAAccessoryDidDisconnectNotification is obviously posted to the notification centre as well as EAAccessoryDidConnectNotification.
When I receive EAAccessoryDidDisconnectNotification I have to adequately handle it and allow some time till the EAAccessoryDidConnectNotification is received. However when another EAAccessoryDidDisconnectNotification comes, it messes up with my settings and the user is logged out from the application.
My question therefore is - is it possible to check which notifications have been sent to the NSNotificationCenter while app was in the background just after the app comes to the foreground, and remove multiple notification of the same kind - leaving only one notification of a kind. Or is there any other solution you suggest me to implement to deal with this?

Application does not run in background simply does not work

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];

Resources