gpt4 book ai didi

ios - Facebook SDK 和 iOS

转载 作者:行者123 更新时间:2023-11-29 03:44:36 25 4
gpt4 key购买 nike

我正在尝试使用 Facebook SDK 发布状态。部分用户已使用 Facebook 登录。所以我有这个代码:

 if (FBSession.activeSession.isOpen) {


NSLog(@"Already Open%@",[[FBSession activeSession] accessTokenData].accessToken);

// NSString *tok = [[FBSession activeSession] accessTokenData].accessToken;

NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];

[[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session,NSError *error) {
if(!error){
NSLog(@"Publish Permission Granted");

}
else
{
NSLog(@"Publish to get Read Permission");
} }];



//Remove indicator
[_activityView removeFromSuperview];


} else {


// OPEN Session!
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {

// show error to user.

} else if (FB_ISSESSIONOPENWITHSTATE(status)) {

// no error, so we proceed with requesting user details of current facebook session.

NSLog(@"----%@",[session accessTokenData].accessToken);

//NSString *tok = [session accessTokenData].accessToken;

NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];

[[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session,NSError *error) {
if(!error){
NSLog(@"Publish Permission Granted");
}
else
{
NSLog(@"Publish to get Read Permission");
} }];


[_activityView removeFromSuperview];

// [self promptUserWithAccountName]; // a custom method - see below:
}
}];
}

因此,让我们重点关注第一部分,假设用户有打开的 session (第二部分只是打开一个新 session ,以防没有可用的 session )。授予发布权限后,如何发布带有网址和图片的状态? Facebook 的例子根本没有帮助。我找到了一些其他示例,但大多数都已经过时了。

我成功地发布了一篇简单的帖子:

FBRequest *postRequest = [FBRequest requestForPostStatusUpdate:@"hi" ];

[postRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {


// TODO: Check for success / failure here

}];

获得发布许可后。我想要类似的 url 描述 imgurl 和标题。

最佳答案

这就是我在 ibaction 方法中的做法:

分享图片:

UIImage *img = myImage; 
FBLoginView *loginview = [[FBLoginView alloc] init];
loginview.delegate = self;

[self performPublishAction:^{

[FBRequestConnection startForUploadPhoto:img
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
[self showAlert:@"Photo Post" result:result error:error];
}];

}];

分享网址:

NSURL *urlToShare = [NSURL URLWithString:@"http://developers.facebook.com/ios"];

FBAppCall *appCall = [FBDialogs presentShareDialogWithLink:urlToShare
name:@"Hello Facebook"
caption:nil
description:@"The 'Hello Facebook' sample application showcases simple Facebook integration."
picture:nil
clientState:nil
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if (error) {
NSLog(@"Error: %@", error.description);
} else {
NSLog(@"Success!");
}
}];

同时添加这些:

- (void) performPublishAction:(void (^)(void)) action
{
if([[FBSession activeSession]isOpen])
{
if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
// if we don't already have the permission, then we request it now
[FBSession.activeSession requestNewPublishPermissions:@[@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
action();
}
//For this example, ignore errors (such as if user cancels).
}];
} else {
action();
}
}
else
{
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {

}else{
NSLog(@"Session error");
[self fbResync];
[NSThread sleepForTimeInterval:0.5]; //half a second
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error){

}];
}
}];
}
}

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{

}

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
{
self.loggedInUser = user;
}

- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
self.loggedInUser = nil;
}

-(void)fbResync
{
ACAccountStore *accountStore;
ACAccountType *accountTypeFB;
if ((accountStore = [[ACAccountStore alloc] init]) && (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){

NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
id account;
if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])){

[accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
//we don't actually need to inspect renewResult or error.
if (error){

}
}];
}
}
}

希望这会有所帮助...查看 Facebook SDK 示例中的 HelloFacebookSample。

试试这个:

[self performPublishAction:^{
NSString *message = [NSString stringWithFormat:@"Updating status for %@ at %@", self.loggedInUser.first_name, [NSDate date]];

[FBRequestConnection startForPostStatusUpdate:message
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

[self showAlert:message result:result error:error];
self.buttonPostStatus.enabled = YES;
}];

self.buttonPostStatus.enabled = NO;
}];

关于ios - Facebook SDK 和 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com