在Facebook iOS上发布图像

时间:2012-02-25 06:05:25

标签: ios facebook

我正在制作和应用程序,你修改图像,然后你可以上传到Twitter,Facebook等...我的问题是与Facebook。 fbDidLogin从未调用过。我按照分配的教程来搜索分配并尝试分配解决方案而不做任何事情。

因为我不知道用户是否会将照片导出到Facebook,所以只有在我必须离开代码时才登录。我希望有人可以帮助我。

我需要将我创建的图像发布到用户墙

//<FBSessionDelegate, FBDialogDelegate, FBRequestDelegate>

- (void)publishFacebook:(UIImage *)img {

    [self loginFacebook];
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        img, @"picture", @"Testing...", @"caption",
                                        nil];
    [_facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];
    NSLog(@"Image posted");
}

- (void)loginFacebook {

    if (_facebook == nil) {
        _facebook = [[Facebook alloc] initWithAppId:kAppId 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 arrayWithObjects:@"email", @"offline_access", @"publish_stream", nil];
        [_facebook authorize:permissions];
        [defaults synchronize];
    }
}

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[_facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[_facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

}

在我的app delegate

// Pre 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [[_viewController facebook] handleOpenURL:url]; 
}

// For 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [_viewController.facebook handleOpenURL:url]; 
}

它转到Facebook应用程序,它请求所有权限等..并且IT在控制台“图像发布”中说,但如果我留在Facebook应用程序而不接受或拒绝,只需留在那里它执行上传图像。 ..

如果在Facebook应用程序中我接受它返回我的普通应用程序的所有权限。应用程序委托方法返回正常值,Facebook的网页具有非常长的字符串。

我该怎么办?

由于

1 个答案:

答案 0 :(得分:1)

试试这段代码......

    -(void)buttonPressed:(id)sender{ //start the facebook action by clicking any button
    facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" 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]) {
        [facebook authorize:nil];
    }else{
    [self postWall];
    }
    // Pre 4.2 support
    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        return [facebook handleOpenURL:url]; 
    }
// after 4.2 support
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [facebook handleOpenURL:url]; 
}
    - (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:@"jpg"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"Test message", @"message", imageData, @"source", nil];
[facebook requestWithGraphPath:@"me/photos" andParams:params andHttpMethod:@"POST" andDelegate:self];

        NSLog(@"Image posted");
    }
-(void)fbDidNotLogin:(BOOL)cancelled{
    if (cancelled) {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Could not Login" message:@"Facebook Cannot login for your application" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alertView show];
    }
}

签出你是否把委托给FBsessionDelegate,FBRequestDelegate在你的控制器File.Try在你的应用程序本身运行你的Facebook而不是在SAFARI.To这样做检查此链接。 Facebook Implementation within app (without opening in safari or native app)

<强>编辑: 使用postWall方法更改了代码并添加了fbDidNotLoginMethod。这是正确的。