あなたの Facebook セッションは有効ですか? if ステートメントで尋ねる:
if (facebook isSessionValid) {
NSLog (@"Working fine..."); // it might also be better to place the post method in here and put an else statement to open a alert where it says error or something like that
}
そうでない場合....次のような方法を確認してください
handleOpenURL と fbDidLogin
そこにログインして、呼び出されたかどうかを確認してください....そうでない場合は、非常によく似た問題がありました...ガイドします
だから私の問題は、App Delegate で Facebook 変数を宣言する必要があったことでした...これを見てください..これはあなたがすべきことです:
- (void)viewDidLoad
{
fbAppDelegate = (NerdfeedAppDelegate *)[[UIApplication sharedApplication] delegate];
[[fbAppDelegate facebook] authorize:nil];// or wherever you wanna put it...
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(IBAction)post {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%@ has invited you (%@) to enjoy an offer at %@ on %@", [nameArray objectAtIndex:0], [facebookNameArray objectAtIndex:i], spotString, timeChosenString], @"name",
domain, @"link",
[NSString stringWithFormat:@"%@ (%@)", offerString, peopleString], @"caption",
@"Going out? Use JoynIn to grab amazing offers from local bars, clubs, and restaurants. All you need to bring is friends!", @"description",
titleChosen.text, @"message",
nil];
[fbAppDelegate.facebook requestWithGraphPath:[NSString stringWithFormat:@"%@/feed/",[facebookUIDArray objectAtIndex:i]] andParams:params andHttpMethod:@"POST" andDelegate:self];
}
アプリ Delegate.h:
@interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate, FBDialogDelegate, FBLoginDialogDelegate, FBRequestDelegate>
{
Facebook *facebook;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) Facebook *facebook;
App Delegate.m:
static NSString *kAppID;
- (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];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
kAppID = [[NSString alloc] initWithString:@"//app id"];
// ....
facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:(id)self.viewController];
[facebook setSessionDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
return YES;
}
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
-(void)fbDidLogout {
// Remove saved authorization information if it exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
}