Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 2.48 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I use Facebook instance in a ViewController?
  2. - (void)viewDidLoad
  3. {
  4.     [super viewDidLoad];
  5.     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  6.     Facebook *facebook = [appDelegate facebook];
  7. }
  8.        
  9. #import <UIKit/UIKit.h>
  10. #import "FBConnect.h"
  11.  
  12. @interface MyFacebookViewController : UIViewController <FBSessonDelegate, FBRequestDelegate>
  13. @property (nonatomic, strong) Facebook *facebook;
  14. ...
  15. @end
  16.        
  17. #import "MyFacebookViewController.h"
  18. #import "MyAppDelegate.h"
  19.  
  20. @implementation MyFacebookViewController
  21.  
  22. @synthesize facebook = _facebook;
  23.  
  24. -(Facebook *)facebook
  25. {
  26.     if(!_facebook){
  27.         _facebook = [[Facebook alloc] initWithAppId:MY_APP_ID andDelegate:self];
  28.     }
  29.     return _facebook;
  30. }
  31.  
  32. -(void)viewDidLoad
  33. {
  34.     [super viewDidLoad];
  35.     ((MyAppDelegate *)([[UIApplication sharedApplication] delegate])).facebookViewController = self;
  36.     ...
  37. }
  38.        
  39. #import <UIKit/UIKit.h>
  40. #import "MyFacebookViewController.h"
  41.  
  42. @interface MyAppDelegate : UIResponder <UIApplicationDelegate>
  43. ...
  44. @property (strong, nonatomic) MyFacebookConnectViewController *facebookViewController;
  45. @end
  46.        
  47. @implementation MyAppDelegate
  48.  
  49. @synthesize facebookViewController = _facebookViewController;
  50. ...
  51.  
  52. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  53. {
  54.     return [self.facebookViewController.facebook handleOpenURL:url];
  55. }
  56.        
  57. #import <UIKit/UIKit.h>
  58. #import "FBConnect.h"
  59.  
  60. @interface MyAppDelegate : UIResponder <UIApplicationDelegate>
  61. ...
  62. @property (strong, nonatomic) Facebook *facebook;
  63. @end
  64.        
  65. #import "MyAppDelegate.h"
  66.  
  67. @implementation MyAppDelegate
  68.  
  69. @synthesize facebook = _facebook;
  70.  
  71. -(Facebook *)facebook
  72. {
  73.     if(!_facebook){
  74.         _facebook = [Facebook alloc] initWithAppId:MY_APP_ID andDelegate:self];
  75.     }
  76.     return _facebook;
  77. }
  78.  
  79. -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  80. {
  81.     return [self.facebook handleOpenURL:url];
  82. }
  83.        
  84. #import "MyAppDelegate.h"
  85.  
  86. @interface AnyViewController : UIViewController
  87. ...
  88. @property (strong, nonatomic) Facebook *facebook;
  89. @end
  90.        
  91. #import "AnyViewController.h"
  92.  
  93. @implementation AnyViewController
  94.  
  95. @synthesize facebook = _facebook;
  96.  
  97. -(Facebook *)facebook
  98. {
  99.     if(!_facebook){
  100.         MyAppDelegate *theAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
  101.         _facebook = theAppDelegate.facebook;
  102.     }
  103.     return _facebook;
  104. }