2012-02-25 81 views
0

我正在製作和應用程序,你修改圖像,然後你可以上傳到Twitter,Facebook等...我的問題是與Facebook。還有fbDidLogin從不叫。我遵循這個教程的分配,並搜索分配,並嘗試解決方案的分配和任何東西。在Facebook上發佈圖像iOS

因爲我不知道用戶是否會將照片導出到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]; 

} 

在我的應用程序委託

// 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的網頁與一個非常長的字符串。

我該怎麼辦?

感謝

+0

@Emesto我真的不明白你想說什麼..但我會盡力幫助你的代碼。在此之前,你可以看看我的SO問題... http:// stackoverflow。 com/q/9238700/1083859 – 2012-02-25 07:03:00

+0

@RA嗨男人,我簽出帖子,它發佈的圖像,但與網址,這是不同的,因爲我有一個UIImage,我想上傳和發佈在Facebook – Edig 2012-02-25 19:02:17

回答

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要做到這一點檢查這個鏈接。 Facebook Implementation within app (without opening in safari or native app)

EDITED: 與postWall方法改變的代碼,並加入fbDidNotLoginMethod.This是正確的。

+0

我會檢查它,因爲我需要它登錄並在同一功能內部做所有事情我測試它並告訴你謝謝 – Edig 2012-02-25 19:03:30

+0

對不起太多時間,但它工作:) – Edig 2012-04-14 03:21:39