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
}
Related
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/
I found a couple of tutorials online about detecting battery charge status changes, but what I need to detect is the second the iphone/ipad is plugged in. Not plugged in and then a charge update is sent out.
We need to prevent them from using our app if the battery is below 25% charge but if its plugged in then we need to allow them.
I used both of these, but neither notification gets fired when plugging in or unplugging.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// tell the device that - my application is going to monitor Device Battery levels
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
// schedule a timer to update the battery details
[NSTimer scheduledTimerWithTimeInterval:600.0
target:self
selector:#selector(updateBatteryDetails)
userInfo:nil
repeats:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(pluggedDetected)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(pluggedDetected)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
}
One problem with your code is that when you register for the notifications, you're passing in a selector that doesn't take any parameters. As the docs for NSNotificationCenter tell us:
The method specified by notificationSelector must have one and only
one argument (an instance of NSNotification).
Fixing that caused your code to work for me. Your method should look like this:
- (void)pluggedDetected:(NSNotification*)notification
{
//...
}
and your call to add the observer should look like:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(pluggedDetected:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
Note that the colon is an essential part of the selector name.
Given that you're using notifications to detect changes, I don't think you also need a timer to update the battery information -- just do that whenever a notification triggers your method.
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.
I am implementing facebook's SDK for iOS into my app. There are two functions however that are supposed to register and unregister for the notifications:
From Facebook's login to facebook with ios:
in the viewDidLoad method, register for the session change notification you defined in the app delegate by adding this code to the end of the method:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(sessionStateChanged:)
name:FBSessionStateChangedNotification
object:nil];
and
Unregister for the notifications by adding the following code to the end of the didReceiveMemoryWarning the method:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Since I have quite a few view controllers and all of them should be using facebook's API, I thought I should implement the register/unregister methods in the applicationDidFinishLoadingWithOptions (for register for notifications)
but I am not sure if and how I should implement the unregister's removeObserver command, because applicationDidReceiveMemoryWarning is not available for the AppDelegate.
Is DidReceiveMemoryWarning visiting all the viewControllers of the App?
Would it be sufficient to unregister in just one of my viewControllers ?
The application delegate does receive memory warnings:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
If it didn't, another option would be to use the notification center:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(whatever:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
That all said, it seems to me that removing the observer on a memory warning is inappropriate. At what point will your reinstate it? But hey, if that's what Facebook recommends...
I have a UIView ,I want it to display in a new day.For example,today I launch my app,and that UIView comes to my view,but after this,I can't see that UIView until tomorrow.
So I want to know how to judge whether it is a new day to display the UIView?
You need to register for the UIApplicationSignificantTimeChangeNotification notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(timeChange) name:UIApplicationSignificantTimeChangeNotification object:nil];
Then in the timeChange method (or whatever you call it) update your view.
Don't forget to unregister:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationSignificantTimeChangeNotification object:nil];
This notification is sent at midnight, if the user changes the time on their device, or the timezone changes. If your app is in the background when the change happens, it will get notified when the app returns to the foreground.