我对目标 C 很陌生。我遵循了将 Facebook 集成到 iOS 应用程序的教程,没有做任何其他事情。在我添加了注销按钮的代码后,我运行了它,它给了我一个错误:
[self.viewController.view addSubview:logoutButton];
错误是“无法识别的选择器发送到实例 0x6b6c550”。我知道这可能是一个愚蠢的错误,但如果有人能指出我错在哪里,我将不胜感激!
static NSString* kAppId = @"340105106048288";
// Method that gets called when the sign out button is clicked
- (void)logoutButtonClicked {
[facebook logout];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logoutButton.frame = CGRectMake(40, 40, 200, 40);
[logoutButton setTitle:@"Log Out" forState:UIControlStateNormal];
[logoutButton addTarget:self action:@selector(logoutButtonClicked)
forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:logoutButton];
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"];
}
// This part, the authorize method will bring you to the authorization page
if (![facebook isSessionValid])
[facebook authorize:nil];
return YES;
}
// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [facebook handleOpenURL:url];
}
// Save the user credential, specifically the access token and the expiration date to the user defaults
- (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 removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
}