On which ViewController should I implement FacebookSDK's removeObserver? - ios

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

Related

How to redirect to a particular page in a Webview when when a push notification is received

I have a UIWebView in my ViewController. I need to redirect it to a particular page when a push notification is received, The method to receive push notification is in AppDelegate.
So how can i access that UIWebView which is in ViewController from AppDelegate?
You can use nsnotification observers
when ever a push notification received you can post a notification like below
[[NSNotificationCenter defaultCenter] postNotificationName:#"PushNotificationReceived" object:nil];
and in the controller class you can observer the notification and do what ever you want.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadWebview:) name:#"PushNotificationReceived" object:nil];
this is one of the way.

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/

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.

Reachability notification not getting called. Using tonymillion/Reachability

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.

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