Iphone 通过-openWithBehavior:completionHandler获得Facebook应用程序的发布权限,用于iOS
在我的应用程序中,我需要用户登录Facebook,在我的表视图中获取好友列表并在提要上发布,但我不想将用户重定向到任何地方。所以我使用了-openWithBehavior:completionHandler:。。。这是我的密码Iphone 通过-openWithBehavior:completionHandler获得Facebook应用程序的发布权限,用于iOS,iphone,ios,facebook,facebook-graph-api,facebook-fql,Iphone,Ios,Facebook,Facebook Graph Api,Facebook Fql,在我的应用程序中,我需要用户登录Facebook,在我的表视图中获取好友列表并在提要上发布,但我不想将用户重定向到任何地方。所以我使用了-openWithBehavior:completionHandler:。。。这是我的密码 -(IBAction)loginAction:(id)sender { [self deleteCookies]; // get the app delegate so that we can access the session property DLAppDelega
-(IBAction)loginAction:(id)sender {
[self deleteCookies];
// get the app delegate so that we can access the session property
DLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
// this button's job is to flip-flop the session from open to closed
if (appDelegate.session.isOpen) {
// if a user logs out explicitly, we delete any cached token information, and next
// time they run the applicaiton they will be presented with log in UX again; most
// users will simply close the app or switch away, without logging out; this will
// cause the implicit cached-token login to occur on next launch of the application
[appDelegate.session closeAndClearTokenInformation];
} else {
if (appDelegate.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
appDelegate.session = [[FBSession alloc] init];
[self updateView];
}
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
switch (status) {
case FBSessionStateOpen:
// call the legacy session delegate
//Now the session is open do corresponding UI changes
{
FBCacheDescriptor *cacheDescriptor = [FBFriendPickerViewController cacheDescriptor];
[cacheDescriptor prefetchAndCacheForSession:session];
[FBSession openActiveSessionWithAllowLoginUI:NO];
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:@"publish_stream",@"publish_actions", nil] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:NO completionHandler:nil];
}
break;
case FBSessionStateClosedLoginFailed:
{ // prefer to keep decls near to their use
// unpack the error code and reason in order to compute cancel bool
// call the legacy session delegate if needed
//[[delegate facebook] fbDialogNotLogin:userDidCancel];
}
break;
// presently extension, log-out and invalidation are being implemented in the Facebook class
default:
break; // so we do nothing in response to those state transitions
}
[self updateView];
}];
}
}
用户已成功登录,我可以使用FQL检索好友列表。问题是在发布到提要时。我知道我需要获得发布权限才能这样做。但是当我使用下面的代码发布
- (IBAction)postAction:(id)sender {
DLAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (appDelegate.session.isOpen) {
[FBSession openActiveSessionWithAllowLoginUI:NO];
NSMutableDictionary *postParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"https://developers.facebook.com/ios", @"link",
@"https://developers.facebook.com/attachment/iossdk_logo.png", @"picture",
@"Facebook SDK for iOS", @"name",
@"Build great social apps and get more installs.", @"caption",
@"The Facebook SDK for iOS makes it easier and faster to develop Facebook integrated iOS apps.", @"description",
nil];
if ([_postText.text length]>0) {
[postParams setObject:[_postText text] forKey:@"message"];
}
if (([FBSession.activeSession.permissions
indexOfObject:@"publish_actions"] == NSNotFound) ||
([FBSession.activeSession.permissions
indexOfObject:@"publish_stream"] == NSNotFound)) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObjects:@"publish_stream",@"publish_actions",nil]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self publishStory:postParams];
}
}];
} else {
// If permissions present, publish the story
[self publishStory:postParams];
}
}
}
-(void)publishStory:(NSDictionary *)postParams {
[FBRequestConnection startWithGraphPath:
@"me/feed" parameters:postParams HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (!error) {
//Tell the user that it worked.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Shared:"
message:[NSString stringWithFormat:@"Sucessfully posted to your wall."]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
alertView.tag = 101;
[alertView show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error:"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
NSLog(@"%@",error);
}
}
];
}
此代码将用户重定向到Safari或Facebook应用程序。我不想发生这种事
当然,我需要在登录时获得发布权限。问题是如何设置?您必须设置FBSessionLoginBehavior,要更改它,唯一的方法是使用:
[session openWithBehavior:FBSessionLoginBehaviorWithNoFallbackToWebView
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// Respond to session state changes,
// ex: updating the view
}];
我看到您使用了FBSessionLoginBehaviorForcingWebView
,因此要获得所需内容,您必须从该枚举中进行选择:
typedef enum {
/*! Attempt Facebook Login, ask user for credentials if necessary */
FBSessionLoginBehaviorWithFallbackToWebView = 0,
/*! Attempt Facebook Login, no direct request for credentials will be made */
FBSessionLoginBehaviorWithNoFallbackToWebView = 1,
/*! Only attempt WebView Login; ask user for credentials */
FBSessionLoginBehaviorForcingWebView = 2,
/*! Attempt Facebook Login, prefering system account and falling back to fast app switch if necessary */
FBSessionLoginBehaviorUseSystemAccountIfPresent = 3,
} FBSessionLoginBehavior;
现在要解决这个“登录时我确实需要获得发布权限。问题是如何获得?”问题,您可以-(id)initWithPermissions:(NSArray*)权限代码>您的会话
:
NSArray *permissions = @[@"publish_stream", @"publish_actions"];
appDelegate.session = [[FBSession alloc] initWithPermissions:permissions];