简体   繁体   English

如何使同一个Facebook对象可用于我的iOS Facebook SDK项目中的所有代码?

[英]How do I make the same Facebook object available to all my code in my iOS Facebook SDK project?

I've tried passing in the Facebook object which I named facebook as an argument into classes that rely on it. 我试图通过我在其中一个名为Facebook的对象facebook作为一个参数到依赖于它的类。 However, is there a way to use it in a sort of global way so that way I don't have to keep on passing it around between objects? 但是,是否有一种以全局方式使用它的方法,这样我就不必继续在对象之间传递它了?

Well, I see several ways: 好吧,我看到了几种方法:

  • Create a public static method in a class that contains facebook object: 在包含facebook对象的类中创建一个公共静态方法:

     + (id)getSharedFacebookObject { return facebook; } 
  • The second way is to use app delegate: 第二种方法是使用应用程序委托:

     MyAppDelegate *appDel = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; id Facebook = appDel.facebook; 

Your best bet would be to use the singleton pattern. 最好的选择是使用单例模式。

http://en.wikipedia.org/wiki/Singleton_pattern http://en.wikipedia.org/wiki/Singleton_pattern

Create a new class that has the facebook object as an instance variable and set the new class as the facebook delegate. 创建一个具有facebook对象作为实例变量的新类,并将新类设置为facebook委托。 You can then get the sharedInstance from any class and it will return the same object. 然后,您可以从任何类获取sharedInstance,它将返回相同的对象。

This may not actually be the correct solution to your problem. 这实际上可能不是您问题的正确解决方案。 It may require you to add another layer of communication between the singleton and the particular view controller you are using. 可能需要您在单例和您正在使用的特定视图控制器之间添加另一层通信。 If you are only performing simple functions then this may be fine. 如果仅执行简单功能,则可能会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM