Online Book Reader

Home Category

iPhone Game Development - Chris Craft [86]

By Root 1518 0
you did not declare the session variable:

session = [FBSession sessionForApplication: @”” secret: @”” delegate:self];

The compiler cannot implicitly tell if the session variable is an FBSession object, a string, or even an int field. Since the session variable needs to be an FBSession object, you should update your project to correct for this:

FBSession *session = [FBSession sessionForApplication: @”” secret: @”” delegate:self];

If you try to build your application now, you should be rewarded with a Build succeeded message from Xcode. However, if you run the application, it will not function as expected. You will not see the alert you added to the didLogin method. This is because the session variable scope is limited to the viewDidLoad method. It is only “alive” during this time.

By the time Facebook has enough of a chance to respond to the application's session request, Xcode has already cleaned up the session variable. You will need to use the retain method to tell Xcode to keep this variable until you are done with it. According to Xcode's API Reference, “You send an object a retain message when you want to prevent it from being deallocated without your express permission.” And that is exactly what you want to do in this case.

The last thing you need to do to receive the didLogin event is to promote a local session variable inside viewDidLoad to a property. Open the MainViewController.h file and declare a new FBSession variable named session. Your MainViewController.h file should now match the following code listing:

#import

#import “FBConnect/FBConnect.h”

@interface MainViewController : UIViewController {

FBSession* session;

}

@end

You will also need to update the MainViewController.m file. Since you have declared the session variable already in the MainViewController.h file, you do not need to declare it again in the MainViewController.m file. Update your viewDidLoad method to match the following:

session = [[FBSession sessionForApplication: @”” secret: @”” delegate:self] retain];

Since you have used retain to tell Xcode you want to keep the session variable active, you are responsible for telling Xcode when you are done with the session variable. You will want to keep the session variable around for the whole lifetime of the application, basically for as long as it is running. When the application is closed, it will call the dealloc method inside the MainViewController.m file. You can use the following code to tell Xcode to release the variable session from usage:

- (void)dealloc

{

[session release];

[super dealloc];

}

Logging in

Once you have a session for your application, you can ask the user to log in. The easiest way to do this is to add a standard login button to your application. The following line of code automatically displays the login dialog box when the user touches it:

FBLoginButton* button = [[[FBLoginButton alloc] init] autorelease];

button.style = FBLoginButtonStyleWide;

[self.view addSubview:button];

If you update your viewDidLoad method with this code, it should now look something like this:

- (void)viewDidLoad

{

session = [FBSession sessionForApplication: @”” secret: @”” delegate:self];

FBLoginButton* button = [[[FBLoginButton alloc] init] autorelease];

button.style = FBLoginButtonStyleWide;

[self.view addSubview:button];

}

The FBLoginButtonStyleWide is an enumeration setting to tell Xcode to use the wider button style when creating the button to show on the screen. You can also try FBLoginButtonStyleNormal, which is the default, to see how you like it. Now if you click Build and Go, you should see the Facebook Connect–enabled iPhone application screen shown in Figure 6.26.

One cosmetic feature that is different between your application and the Facebook Connect sample application is the background color. Let's take a moment and

Return Main Page Previous Page Next Page

®Online Book Reader