ios - 我应该在哪个ViewController 上实现FacebookSDK 的removeObserver?

标签 ios xcode facebook

我正在将 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)确实收到内存警告:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

如果没有,另一种选择是使用通知中心:

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/

相关文章:

ios - 如何使用 Json (Swift 2) 将错误处理添加到工作 TableView Controller

xcode - 如何快速本地化以编程方式添加的自动布局约束

ios - 从 Xamarin.Form 向 Facebook 提交 iOS 构建

java - 如何通过 Android 的帐户管理器类检索 Twitter 和 facebook 身份验证和 token

android - 在 facebook 集成 android 中获取意外的 CallbackManager

ios - 从数组中搜索并返回匹配字符串元素的有效方法

ios - 如何实现iOS5/6的iOS 7 Tab栏图标设计

ios - 强制标签显示其文本

ios - 如何创建自定义 Xcode 预处理器宏

ios - NSInvalidArgumentException -[NSNull 长度] : unrecognized selector sent to instance