上传Facebook照片和自动登录用户(Kiosk应用程序)

时间:2014-03-11 16:50:37

标签: ios xcode facebook image facebook-graph-api

我有一个iPad应用程序,专为在自助服务终端环境中使用而设计。

用户流程应为

  • 拍照
  • 从iPad相册视图中选择照片
  • 分享到Facebook和/或Twitter
  • 在图片发布后自动将用户注销

我自动注销Twitter工作正常,我的问题是Facebook部分。

我已经实现了用于内部测试的Graph API,并且希望能够以这种方式发布完整的故事,但我不认为一旦获得授权,就有办法从Facebook应用程序注销帖子已经完成。

对于后备,我可以使用Feed对话框并从那里自动注销,但据我所知,没有办法从那里上传本地图像以便与Facebook共享。

我的Facebook分享代码如下:

- (IBAction)facebookShare:(id)sender {

/// Package the image inside a dictionary
NSArray* image = @[@{@"url": self.mergeImages, @"user_generated": @"true"}];

// Create an object
id<FBGraphObject> object =
[FBGraphObject openGraphObjectForPostWithType:@"me/feed:photo"
                                        title:@"a photo"
                                        image:self.mergeImages
                                          url:nil
                                  description:nil];

// Create an action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];

// Set image on the action
[action setObject:image forKey:@"image"];

// Link the object to the action
[action setObject:object forKey:@"photo"];

// Hardcode the location based on Facebook Place ID
id<FBGraphPlace> place = (id<FBGraphPlace>)[FBGraphObject graphObject];
[place setId:@"279163865580772"]; // Singley + Mackie
[action setPlace:place];

// Check if the Facebook app is installed and we can present the share dialog
FBOpenGraphActionShareDialogParams *params = [[FBOpenGraphActionShareDialogParams alloc] init];
params.action = action;
params.actionType = @"me/feed:share";

// If the Facebook app is installed and we can present the share dialog
if([FBDialogs canPresentShareDialogWithOpenGraphActionParams:params]) {
    // Show the share dialog
    [FBDialogs presentShareDialogWithOpenGraphAction:action
                                          actionType:@"photo_overlay:share"
                                 previewPropertyName:@"photo"
                                             handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
                                                 if(error) {
                                                     // An error occurred, we need to handle the error
                                                     // See: https://developers.facebook.com/docs/ios/errors
                                                     // NSLog([NSString stringWithFormat:@"Error publishing story: %@", error.description]);
                                                 } else {
                                                     // Success
                                                     NSLog(@"result %@", results);
                                                 }
                                            }];

    // If the Facebook app is NOT installed and we can't present the share dialog
} else {
    // Put together the Feed dialog parameters
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"name",
                                   @"caption",
                                   @"description",
                                   @"link",
                                   @"picture",
                                   nil];
    // Show the feed dialog
    [FBWebDialogs presentFeedDialogModallyWithSession:nil
                                           parameters:params
                                              handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
                                                  if (error) {
                                                      // An error occurred, we need to handle the error
                                                      // See: https://developers.facebook.com/docs/ios/errors
                                                      // NSLog([NSString stringWithFormat:@"Error publishing story: %@", error.description]);
                                                  } else {
                                                      if (result == FBWebDialogResultDialogNotCompleted) {
                                                          // User cancelled.
                                                          NSLog(@"User cancelled.");
                                                      } else {
                                                          // Handle the publish feed callback
                                                          NSDictionary *urlParams = [self parseURLParams:[resultURL query]];

                                                          if (![urlParams valueForKey:@"post_id"]) {
                                                              // User cancelled.
                                                              NSLog(@"User cancelled.");

                                                          } else {
                                                              // User clicked the Share button
                                                              NSString *result = [NSString stringWithFormat: @"Posted story, id: %@", [urlParams valueForKey:@"post_id"]];
                                                              NSLog(@"result %@", result);
                                                            }
                                                      }
                                                  }

                                                  // Auto log the user out
                                                  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                                                  NSLog(@"defaults fbDidLogout  ........%@",defaults);
                                                  if ([defaults objectForKey:@"FBAccessTokenKey"])
                                                  {
                                                      [defaults removeObjectForKey:@"FBAccessTokenKey"];
                                                      [defaults removeObjectForKey:@"FBExpirationDateKey"];
                                                      [defaults synchronize];
                                                  }

                                                  NSHTTPCookie *cookie;
                                                  NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
                                                  for (cookie in [storage cookies])
                                                  {
                                                      NSString* domainName = [cookie domain];
                                                      NSRange domainRange = [domainName rangeOfString:@"facebook"];
                                                      if(domainRange.length > 0)
                                                      {
                                                          [storage deleteCookie:cookie];
                                                      }
                                                  }

                                                  [FBSession.activeSession closeAndClearTokenInformation];

                                              }];

}
}
// A function for parsing URL parameters.
- (NSDictionary*)parseURLParams:(NSString *)query {
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    for (NSString *pair in pairs) {
        NSArray *kv = [pair componentsSeparatedByString:@"="];
        NSString *val =
        [kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        params[kv[0]] = val;
    }
    return params;
}

我已经搜索了Stack Overflow以获得答案,但没有找到解决方案。

1 个答案:

答案 0 :(得分:1)

我终于弄清楚了!在这里发布答案,希望有利于处于相同情况的其他人。

首先,将以下内容添加到AppDelegate.m:

-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler:^(FBAppCall *call) {
    // Facebook SDK * App Linking *
    // For simplicity, this sample will ignore the link if the session is already
    // open but a more advanced app could support features like user switching.
    if (call.accessTokenData) {
        if ([FBSession activeSession].isOpen) {
            NSLog(@"INFO: Ignoring app link because current session is open.");
        }
        else {
            [self handleAppLink:call.accessTokenData];
        }
    }
}];
}

// Helper method to wrap logic for handling app links.
- (void)handleAppLink:(FBAccessTokenData *)appLinkToken {
    // Initialize a new blank session instance...
    FBSession *appLinkSession = [[FBSession alloc] initWithAppID:nil
                                                     permissions:nil
                                                 defaultAudience:FBSessionDefaultAudienceNone
                                                 urlSchemeSuffix:nil
                                              tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance] ];
    [FBSession setActiveSession:appLinkSession];
    // ... and open it from the App Link's Token.
    [appLinkSession openFromAccessTokenData:appLinkToken
                          completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                              // Forward any errors to the FBLoginView delegate.
                              if (error) {
                                  //[self.loginViewController loginView:nil handleError:error];
                              }
                          }];
}

无论您何时在应用中调用发布操作,请将此行添加到您的标题文件中:

@property (strong, nonatomic) FBRequestConnection *requestConnection;

以下是您的实施文件:

@synthesize requestConnection;

- (IBAction)facebookShare:(id)sender {

NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"publish_actions", @"publish_checkins", nil];

UIImage *img = self.facebookImage;
[FBSession openActiveSessionWithPublishPermissions:permissions
                                   defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES
                                 completionHandler:^(FBSession *session,FBSessionState s, NSError *error) {
                                     [FBSession setActiveSession:session];
                                     if (!error) {
                                         // Now have the permission
                                         [self processPostingImage:img WithMessage:@"Enter_your_message_here"];
                                     } else {
                                         // Facebook SDK * error handling *
                                         // if the operation is not user cancelled
                                         if (error.fberrorCategory != FBErrorCategoryUserCancelled) {
                                             [self presentAlertForError:error];
                                         }
                                     }
                                 }];

}

-(void)logout {
    [FBSession.activeSession closeAndClearTokenInformation];
    [FBSession.activeSession close];
    [FBSession setActiveSession:nil];
}

- (void)processPostingImage:(UIImage *) img WithMessage:(NSString *)message {
    FBRequestConnection *newConnection = [[FBRequestConnection alloc] init];
    FBRequestHandler handler =
    ^(FBRequestConnection *connection, id result, NSError *error) {
        // output the results of the request
        [self requestCompleted:connection forFbID:@"me" result:result error:error];
    };
    FBRequest *request=[[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:@"me/photos" parameters:[NSDictionary dictionaryWithObjectsAndKeys:UIImageJPEGRepresentation(img, 0.7),@"source",message,@"message",@"{'value':'EVERYONE'}",@"privacy", nil] HTTPMethod:@"POST"];
    [newConnection addRequest:request completionHandler:handler];
    [self.requestConnection cancel];
    self.requestConnection = newConnection;
    [newConnection start];
}

// FBSample logic
// Report any results.  Invoked once for each request we make.
- (void)requestCompleted:(FBRequestConnection *)connection
                 forFbID:fbID
                  result:(id)result
                   error:(NSError *)error
{

    // not the completion we were looking for...
    if (self.requestConnection &&
        connection != self.requestConnection)
    {
        return;
    }

    // clean this up, for posterity
    self.requestConnection = nil;

    if (error)
    {

    }
    else
    {
        [self logout];
    };
}

- (void) presentAlertForError:(NSError *)error {
    // Facebook SDK * error handling *
    // Error handling is an important part of providing a good user experience.
    // When fberrorShouldNotifyUser is YES, a fberrorUserMessage can be
    // presented as a user-ready message
    if (error.fberrorShouldNotifyUser) {
        // The SDK has a message for the user, surface it.
        [[[UIAlertView alloc] initWithTitle:@"Something Went Wrong"
                                    message:error.fberrorUserMessage
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    } else {

    }
}

注意: 要使此功能与自动注销一起使用,您还必须在设备上禁用Safari。这可以通过转到设置&gt;来完成。一般&gt;限制&gt;允许Safari&gt;关闭。一旦关闭,Facebook IBAction将在应用程序内部弹出UIWebView。点击后,当前用户可以输入他们的Facebook凭据,然后应用程序将发布图像,并将用户注销,以便下一个用户可以使用该应用程序,而无需访问以前用户的Facebook详细信息。