ios - 在 Facebook iOS 上发布图像

标签 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];

}

在我的应用程序委托(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 应用程序中我接受所有权限,它会返回到我的正常应用程序。应用程序委托(delegate)方法返回 Facebook 网页上带有很长字符串的正常值。

我能做什么?

谢谢

最佳答案

试试这个代码...

    -(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];
    }
}

检查一下您是否将委托(delegate)放入 Controller 文件中的 FBsessionDelegate、FBRequestDelegate。尝试在您的应用程序本身而不是 SAFARI 中运行您的 facebook。为此,请检查此链接。 Facebook Implementation within app (without opening in safari or native app)

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

关于ios - 在 Facebook iOS 上发布图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9441759/

相关文章:

iphone - 在自定义 UITableViewCell 中访问 UITextField

javascript - 如何防止钛合金 View 中的事件冒泡?

ios - 用于编辑和非编辑文本字段的不同弹出窗口

django - pyfacebook @facebook.require_login() 装饰器导致不断的 auth_token 刷新

ios - UIViewController 动画

objective-c - 没有显式声明的属性 ivars 会生成哪些名称?

facebook - 无法将页面标签添加到 Facebook 页面 - 没有提示添加页面

php - Facebook 登录和 MySQL

Android Facebook SDK 发布 key 哈希不起作用

java - 带有 Android Facebook SDK 的 key 工具