2012-04-15 78 views
0

我正在嘗試發佈圖像到Facebook。昨天我得到了它,但今天我再次測試它,它不起作用:S。我找不到問題。我修改了一些東西,但沒有與Facebook交互的方法。我希望有一個人可以幫助我。發佈UIImage Facebook問題

我在我的viewController中獲得了一切,因爲它只在用戶請求時纔會登錄。

我調試它,只有「發佈Facebook」被調用,因爲我稱它。它到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> 

ViewController.h

進口 「FBConnect.h」

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

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

ViewController.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]; 
} 

感謝

+2

appDelegate怎麼樣?是否正確接線以接收Facebook從背景回來時返回的任何內容? – Stavash 2012-04-15 08:16:07

+0

Nop我的appDelegate沒有任何東西,但爲什麼我測試和工作,然後再次測試,並沒有工作?或者我需要放在我的appDelegate上? - (BOOL)應用程序:(UIApplication *)應用程序handleOpenURL:(NSURL *)url {?我嘗試和沒有什麼 – Edig 2012-04-15 08:26:56

+2

你需要認真對待Facebook的教程,一切都應該工作:https://developers.facebook.com/docs/mobile/ios/build/如果這沒有幫助,請張貼什麼不工作,所以我們可以幫幫我。祝你好運 – Stavash 2012-04-15 08:30:35

回答

1

您需要仔細按照Facebook的導師ial和一切應該工作:developers.facebook.com/docs/mobile/ios/build

您需要的唯一調整是觸發登錄過程作爲UIButton IBAction調用的結果。其餘的流程完全相同。這是一個非常微妙的過程,如果你錯過了某些東西,那麼很難調試。

+0

謝謝我在我的代表中實施此功能以發佈。 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(publishFacebook :) name:@「facebook」object:nil]; – Edig 2012-04-22 07:15:37