我正在将 facebook 的 iOS SDK 实现到我的应用程序中。然而,有两个函数应该注册和取消注册通知:
来自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];
和
Unregister for the notifications by adding the following code to the end of the didReceiveMemoryWarning the method:
[[NSNotificationCenter defaultCenter] removeObserver:self];
由于我有相当多的 View Controller ,并且它们都应该使用 facebook 的 API,所以我想我应该在 applicationDidFinishLoadingWithOptions 中实现注册/取消注册方法(对于 register
用于通知)
但我不确定是否以及如何实现unregister
的removeObserver命令,因为applicationDidReceiveMemoryWarning对于AppDelegate不可用。
- DidReceiveMemoryWarning 是否访问了应用程序的所有 viewController?
- 仅在我的一个 View Controller 中取消注册就足够了吗?
最佳答案
应用程序委托(delegate)确实收到内存警告:
如果没有,另一种选择是使用通知中心:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(whatever:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
总而言之,在我看来,删除内存警告上的观察者是不合适的。您将在什么时候恢复它?但是,嘿,如果这就是 Facebook 推荐的……
关于ios - 我应该在哪个ViewController 上实现FacebookSDK 的removeObserver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13273818/