0

我正在尝试将图像发布到 Facebook。昨天我得到它,但今天我再次测试它并不起作用:S。我找不到问题。我修改了一些东西,但没有修改 Facebook 与之交互的方法。我希望有一个人可以帮助我。

我在 viewController 中得到了所有内容,因为它仅在用户请求时登录。

我调试了它,只有“publishFacebook”被调用,因为我调用它。它去 Facebook 并请求权限,然后你返回,没有任何反应。

URL 方案是正确的。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb235694579858240</string>
        </array>
    </dict>
</array>
</plist>

视图控制器.h

导入“FBConnect.h”

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UIActionSheetDelegate, UIGestureRecognizerDelegate, MFMailComposeViewControllerDelegate, FBSessionDelegate, FBDialogDelegate, FBRequestDelegate>

@property (nonatomic, retain) Facebook *facebook;
-(void)postWall;

视图控制器.m

-(void) publishFacebook:(UIImage *)img { 
    
    //start the facebook action by clicking any button
    _publishedImage = img;
    _facebook = [[Facebook alloc] initWithAppId:@"235694579858240" andDelegate:self];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
        _facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        _facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }
    
    if (![_facebook isSessionValid]) {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_photos",
                                @"user_likes", 
                                @"read_stream",
                                nil];
        _facebook.sessionDelegate = self;
        [_facebook authorize:permissions];
    }else{
        [self postWall];
    }
}
    
    
    - (void)fbDidLogin {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[_facebook accessToken] forKey:@"FBAccessTokenKey"];
        [defaults setObject:[_facebook expirationDate] forKey:@"FBExpirationDateKey"];
        [defaults synchronize];
        [self postWall];
    }
    
    - (void)postWall{
        
        //NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
        NSData *imageData = UIImagePNGRepresentation(_publishedImage);
        //[NSData dataWithContentsOfFile:filePath]
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       @"Check out my awesome image!. I make it with Trolling for iOS", @"message", imageData, @"source", nil];
        [_facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
        
        NSLog(@"Image posted");
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Image Posted" message:@"Your image was successfully posted of facebook" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alertView show];
    }
    
    -(void)fbDidNotLogin:(BOOL)cancelled{
        if (cancelled) {
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Could not Login" message:@"Facebook Cannot login please try again" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
            [alertView show];
        }
    }


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [_facebook handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [_facebook handleOpenURL:url]; 
} 

谢谢

4

1 回答 1

1

您需要仔细遵循 Facebook 的教程,一切都应该正常工作:developers.facebook.com/docs/mobile/ios/build

您需要的唯一调整是作为 UIButton IBAction 调用的结果触发登录过程。其余的流程完全相同。这是一个非常微妙的过程,如果你错过了一些东西,很难调试。

于 2012-04-22T06:53:56.790 回答