Guest User

Untitled

a guest
Jun 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #import "SessionViewController.h"
  2. #import "FBConnect.h"
  3. #import "FBFeedDialog.h"
  4.  
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////
  6. // This application will not work until you enter your Facebook application's API key here:
  7.  
  8. static NSString* kApiKey = @"XXXXXXXXXXXXXXXXXX";
  9.  
  10. // Enter either your API secret or a callback URL (as described in documentation):
  11. static NSString* kApiSecret = @"XXXXXXXXXXXXXXXXXX"; // @"<YOUR SECRET KEY>";
  12.  
  13. ///////////////////////////////////////////////////////////////////////////////////////////////////
  14.  
  15. @implementation SessionViewController
  16.  
  17. @synthesize label = _label;
  18. @synthesize anImage;
  19.  
  20. - (void)done:(id)sender{
  21.  
  22. [self dismissModalViewControllerAnimated:YES];
  23.  
  24.  
  25. }
  26.  
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // NSObject
  29.  
  30. - (id)init {
  31. if (self = [super init]) {
  32. _session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
  33. }
  34. return self;
  35. }
  36.  
  37.  
  38. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  39. if (self = [super initWithNibName:@"SessionViewController" bundle:nibBundleOrNil]) {
  40. _session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
  41.  
  42. }
  43. return self;
  44. }
  45.  
  46. - (void)dealloc {
  47. [_session release];
  48. [anImage release];
  49. [super dealloc];
  50. }
  51.  
  52. ///////////////////////////////////////////////////////////////////////////////////////////////////
  53. // UIViewController
  54.  
  55. - (void)viewDidLoad {
  56. [_session resume];
  57. _loginButton.style = FBLoginButtonStyleWide;
  58. }
  59.  
  60. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  61. return NO;
  62. }
  63.  
  64. ///////////////////////////////////////////////////////////////////////////////////////////////////
  65. // FBDialogDelegate
  66.  
  67. - (void)dialog:(FBDialog*)dialog didFailWithError:(NSError*)error {
  68. _label.text = [NSString stringWithFormat:@"Error(%d) %@", error.code,
  69. error.localizedDescription];
  70. }
  71.  
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////
  73. // FBSessionDelegate
  74.  
  75. - (void)session:(FBSession*)session didLogin:(FBUID)uid {
  76. _permissionButton.hidden = NO;
  77. _feedButton.hidden = NO;
  78.  
  79. NSString* fql = [NSString stringWithFormat:
  80. @"select uid,name from user where uid == %lld", session.uid];
  81.  
  82. NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
  83. [[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
  84. }
  85.  
  86. - (void)sessionDidLogout:(FBSession*)session {
  87. _label.text = @"";
  88. _permissionButton.hidden = YES;
  89. _feedButton.hidden = YES;
  90. }
  91.  
  92. ///////////////////////////////////////////////////////////////////////////////////////////////////
  93. // FBRequestDelegate
  94.  
  95. - (void)request:(FBRequest*)request didLoad:(id)result {
  96.  
  97. if([result isKindOfClass:[NSArray class]]){
  98. NSArray* users = result;
  99. NSDictionary* user = [users objectAtIndex:0];
  100. NSString* name = [user objectForKey:@"name"];
  101. _label.text = [NSString stringWithFormat:@"Logged in as %@", name];
  102. }
  103.  
  104. }
  105.  
  106. - (void)request:(FBRequest*)request didFailWithError:(NSError*)error {
  107. _label.text = [NSString stringWithFormat:@"Error(%d) %@", error.code,
  108. error.localizedDescription];
  109. }
  110.  
  111. ///////////////////////////////////////////////////////////////////////////////////////////////////
  112.  
  113. - (IBAction)askPermissionForPhotoUpload:(id)target {
  114. FBPermissionDialog* dialog = [[[FBPermissionDialog alloc] init] autorelease];
  115. dialog.delegate = self;
  116. dialog.permission = @"photo_upload";
  117. [dialog show];
  118. }
  119. - (IBAction)publishPhoto:(id)target{
  120.  
  121. NSMutableDictionary *args = [[[NSMutableDictionary alloc] init] autorelease];
  122. [args setObject:self.anImage forKey:@"image"];
  123. FBRequest *uploadPhotoRequest = [FBRequest requestWithDelegate:self];
  124. [uploadPhotoRequest call:@"photos.upload" params:args];
  125. }
  126.  
  127.  
  128. - (void)askPermission:(id)target {
  129. FBPermissionDialog* dialog = [[[FBPermissionDialog alloc] init] autorelease];
  130. dialog.delegate = self;
  131. dialog.permission = @"status_update";
  132. [dialog show];
  133. }
  134.  
  135. - (void)publishFeed:(id)target {
  136. FBFeedDialog* dialog = [[[FBFeedDialog alloc] init] autorelease];
  137. dialog.delegate = self;
  138. dialog.templateBundleId = 9999999;
  139. dialog.templateData = @"{"key1": "value1"}";
  140. [dialog show];
  141. }
  142.  
  143. @end
Add Comment
Please, Sign In to add comment