没有ivar,Facebook SDK方法不起作用

时间:2012-05-08 23:00:03

标签: objective-c ios properties facebook-ios-sdk ivars

我正在使用facebook sdk构建基本的facebook客户端应用程序。我在https://developers.facebook.com/docs/mobile/ios/build/#register中关注了facebook的教程。它告诉您在项目中构建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);
}

当@property应该在后台创建一个ivar时,请有人告诉我如何不使用ivar搞砸了吗?如果您需要更多说明,请告诉我。感谢。

0 个答案:

没有答案