Method in viewWillAppear and viewDidLoad didn't loaded - ios

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/

Related

Using NSNotificationCenter to call method in main app from extensions widget?

I have a widget that I would like to call back to my main app so as to make a call to the server to update data. I looked into delegation, but registering the widget's view controller as a delegate didn't seem very practical. So I moved on to trying to use NSNotificationCenter. I have set it up, but the selector is not being called. In my main iOS app I have this in the viewDidLoad method.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loadNewData:)
name:kUpdateData
object:nil];
And at the bottom of that file I have this:
/**
* Updates the table when the today widget is called for updated info
**/
- (void)loadNewData:(NSNotification *) notification
{
[self loadTableData];
}
That's in my main app. Now, in my notification center widget/extension, I make this call:
[[NSNotificationCenter defaultCenter]
postNotificationName:kUpdateData
object:nil];
The postNotificationName being passed in, `kUpdateData', is a constant that is resolved to #"updateData". I can see in the debugger that the postNotificationName method is being called, but the main app is not responding to it (regardless of it is in the foreground or the background). What am I doing wrong?
As a side note, the only reason I am doing this is to remove the need for repetitive code and re-implementing things I have already made.
As far as i know extension cant access or call main app methods... what can do is either do the server execution in extension or set a value in shared NSUserDefault so when your app is brought to foreground you can check this value and connect with server accordinly.

Setting NSNotifications observer in a cordova plug in

I have a cordova plugin that depends on a an accessory being connected to my iOS device. The accessory (a magnetic swiper) on notification that something is being swiped. However I not sure what I should be setting the observer as. In the example code they provide it they set it as self but that in a ViewController My plug in is not in the app controller. Any ideas?
this is the notification code that the sample code had( again this was in the ViewController:
[[NSNotificationCenter defaultCenter] addObserver: self
selector:#selector(trackDataReady:)
name: #"trackDataReadyNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(devConnStatusChange)
name:#"devConnectionNotification"
object:nil];
As long as the object (self) is still allocated it will receive the messages when they are posted to the NSNotificationCenter defaultCenter. It does not have to be a view controller. Remember though when the object is deallocated removed the observers otherwise you will get application crashes.

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.

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

Using two delegates

I am new to iPhone programming. I want to call a function which belongs to one of my views in MyAppDelegate. I want to do this because I cannot find any ApplicationDidEnterBackground() event in view.
So, basically I want to call view's function when the application is minimized. I did try using delegate. However I cannot find how to use two delegates as MyAppDelegate already has one. Please help.
The general design pattern is, if you have multiple interested parties in an event, don't use delegates but notifications. In this case, you should register a UIApplicationDidEnterBackgroundNotification in your -init method of every class where you wanna handle the event:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
Then write a -(void)didEnterBackground method with your code. In your -dealloc method, make sure to unregister the notification:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
Also, be aware that this code requires iOS 4.x or higher. If you want to maintain iOS 3 compatibility you've to check the availability first:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(applicationState)]) {
// register UIApplicationDidEnterBackgroundNotification
}

Resources