Методы SDK Facebook не работают без ivar
Я использую Facebook SDK для создания базового клиентского приложения Facebook. Я следовал инструкциям Facebook в https://developers.facebook.com/docs/mobile/ios/build/. Он говорит вам, чтобы построить Facebook ivar в вашем проекте. Когда я делаю это, он работает нормально, однако, если я не использую ivar и просто имею свойство Facebook и синтезатор для него, это не работает. Разве @property и @synthesizer не должны автоматически создавать ivar без необходимости указывать это программистом.
Заголовочный файл для рабочей копии:
@interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate, FBRequestDelegate> {
Facebook* facebook;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) Facebook *facebook;
@property (strong, nonatomic) ViewController *viewController;
@end
Файл реализации для рабочей копии:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize facebook = _facebook;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] init];
//Step 2
facebook = [[Facebook alloc] initWithAppId:@"MY_APP_ID" andDelegate:self];
//Step 3
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
self.facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
//Step 4
if (![facebook isSessionValid]) {
NSArray* permissions = [[NSArray alloc] initWithObjects:@"read_stream", nil];
[facebook authorize:permissions];
}
//Add the logout button
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];
return YES;
}
- (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];
}
//Step 6
-(void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
NSLog(@"Before making newsfeed request");
[facebook requestWithGraphPath:@"me/home" andDelegate:self];
}
//Method that gets called when the logout button is pressed
-(void)logoutButtonClicked:(id)sender {
[facebook logout];
}
-(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];
}
}
-(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Response is: %@", response);
}
Заголовочный файл для не рабочей копии:
#import <UIKit/UIKit.h>
#import "FBConnect.h"
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate, FBSessionDelegate, FBRequestDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) Facebook *facebook;
@property (strong, nonatomic) ViewController *viewController;
@end
Файл реализации для не рабочей копии:
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize facebook = _facebook;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] init];
//Step 2
self.facebook = [[Facebook alloc] initWithAppId:@"MY_APP_ID" andDelegate:self];
//Step 3
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
self.facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
//Step 4
if (![self.facebook isSessionValid]) {
NSArray* permissions = [[NSArray alloc] initWithObjects:@"read_stream", nil];
[self.facebook authorize:permissions];
}
//Add the logout button
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];
return YES;
}
// Pre iOS 4.2 support
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [self.facebook handleOpenURL:url];
}
// For iOS 4.2+ support
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [self.facebook handleOpenURL:url];
}
//Step 6
-(void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[self.facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[self.facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
NSLog(@"Before making newsfeed request");
[self.facebook requestWithGraphPath:@"me/home" andDelegate:self];
}
//Method that gets called when the logout button is pressed
-(void)logoutButtonClicked:(id)sender {
[self.facebook logout];
}
-(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];
}
}
-(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Response is: %@", response);
}
Кто-нибудь может сказать мне, как не использование ivar портит ситуацию, когда @property должен создавать ivar в фоновом режиме? Пожалуйста, дайте мне знать, если вам нужны дополнительные разъяснения. Благодарю.