這是一個圖像上傳到Facebook塗鴉牆的演練。我從舊的應用程序中複製出來,當前的Facebook API工作方式有點不同。我認爲你可以看到我使用共享Facebook對象的主要觀點,您可以使用它來執行身份驗證以及對API的請求。我拿出了一些東西讓它更容易理解。蘋果實際上希望你檢查一個現有的互聯網連接。我希望它有幫助。
@synthesize facebook;
//login with facebook
- (IBAction) facebookButtonPressed {
if (!facebook || ![facebook isSessionValid]) {
self.facebook = [[[Facebook alloc] init] autorelease];
NSArray *perms = [NSArray arrayWithObjects: @"read_stream", @"create_note", nil];
[facebook authorize:FACEBOOK_API_KEY permissions:perms delegate:self];
}
else {
[self fbDidLogin];
}
}
//upload image once you're logged in
-(void) fbDidLogin {
[self fbUploadImage];
}
- (void) fbUploadImage
{
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
resultImage, @"picture",
nil];
[facebook requestWithMethodName: @"photos.upload"
andParams: params
andHttpMethod: @"POST"
andDelegate: self];
self.currentAlertView = [[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Facebook", @"")
message:NSLocalizedString(@"Uploading to Facebook.", @"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles: nil] autorelease];
[self.currentAlertView show];
}
//facebook error
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Error", @"")
message:NSLocalizedString(@"Facebook error message", @"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[myAlert show];
[myAlert release];
}
//facebook success
- (void)request:(FBRequest*)request didLoad:(id)result
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Facebook", @"")
message:NSLocalizedString(@"Uploaded to Facebook message", @"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[myAlert show];
[myAlert release];
}
您是否讓用戶登錄?如果是這樣,你能顯示一些代碼嗎?您應該使用相同的「Facebook」對象來處理用於登錄用戶的圖形請求。 – huesforalice
- (void)fbDidLogin {//將訪問令牌和有效期限存儲爲用戶默認值 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:facebook.accessToken forKey:@「FBAccessTokenKey」]; [defaults setObject:facebook.expirationDate forKey:@「FBExpirationDateKey」]; [defaults synchronize]; } –
我想我剛剛得到了一個這個過程大混亂,你可以嘗試幫助我正確地處理這個過程嗎? –