Reachability notification not getting called. Using tonymillion/Reachability - ios

I'm using this library to get notified when the connection status on the device changes. I don't understand WHY this is not working! I'm doing exactly what I'm reading in the documentation in github!
When the app starts, the notification gets posted but when I open the settings, turn on/off airplane mode and go back to the app (without closing it) the notifiation does not get called.
I have also subscribed to get the UIApplicationWillEnterForegroundNotification notifications... both methods DO EXISTS in the app... am I missing something?!?
This is in my viewDidLoad:
self.reach = [Reachability reachabilityWithHostname:kServerURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChangedInApp:)
name:kReachabilityChangedNotification
object:nil];
[self.reach startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(initializeDateRelatedStuff:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
This is in my dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Thanks!!

The thing is that your application is most likely not a background application and thus suspended once you press the home button to activate airplane mode. Since your app is not running when the reachability changes, it won't get any notification. You will get the notification, however, when network reachability changes while your app is in foreground.
So you probably want to check the reachability when your app enters the foreground in addition to what you have already implemented.

Related

In Objective-C how to detect when iPhone and iPad are plugged in?

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.

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.

On which ViewController should I implement FacebookSDK's removeObserver?

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...

How to judge whether it is a new day in an iOS project?

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.

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