iPhone Game Development - Chris Craft [84]
+ (FBSession*)sessionForApplication:(NSString*)key secret:(NSString*)secret
delegate:(id FBSession* session = [[[FBSession alloc] initWithKey:key secret:secret getSessionProxy:nil] autorelease]; [session.delegates addObject:delegate]; return session; } If you look on the second line you will see the FBSessionDelegate class. Right-click on FBSessionDelegate and select Jump to Definition. This opens the FBSession.h file and locates the definition of the FBSessionDelegate delegate: @protocol FBSessionDelegate /** * Called when a user has successfully logged in and begun a session. */ - (void)session:(FBSession*)session didLogin:(FBUID)uid; @optional /** * Called when a session is about to log out. */ - (void)session:(FBSession*)session willLogout:(FBUID)uid; /** * Called when a session has logged out. */ - (void)sessionDidLogout:(FBSession*)session; @end A protocol in Objective-C is functionally equivalent to an interface in most other modern programming languages. More specifically, a protocol is a list of methods that any class that wishes to adopt the protocol must implement. You can also look at it as a contract that must be met and that the compiler will enforce before allowing the application to compile. Protocols can have both required and optional methods. By default, methods are required in a protocol. If you read through the FBSessionDelegate listing, you will first see the didLogin method, followed by the willLogout and sessionDidLogout methods. Notice the @optional keyword; all methods following this keyword are optional. So the didLogin method is required, and the willLogout and sessionDidLogout methods are optional. Tip Become a fan of the “iPhone Game Programming Book” on Facebook at www.facebook.com. We will be sure to provide updates and announcements here. If you were to try and build the application now, the compiler would generate the following warning: Warning: class ‘MainViewController' does not implement the ‘FBSessionDelegate' protocol This is because when you wrote the following line of code you assigned the FBSessionDelegate to self at the end of the line: session = [FBSession sessionForApplication: @” Now the compiler is holding you to your end of the bargain. You have learned that the FBSessionDelegate protocol has three methods, two of which are optional. You should add the missing required protocol method to the MainViewController class. We will leave implementing the optional methods as an exercise for the reader. There is more to implementing a protocol than simply adding the required methods. You also have to mark the containing class with the name of the protocol. Think about it this way: Just because your class has a didLogin method doesn't mean you really intended for it to be used by the FBSessionDelegate. Compilers are not designed to trust coincidences. Here is how to mark your MainViewController class so that the compiler will know you wanted to implement the FBSessionDelegate. First open the MainViewController.h file and find the following section of code inside it: #import @interface MainViewController : UIViewController { } To label the MainViewController as implementing the FBSessionDelegate protocol, you need to add the name of the protocol to the inside of a pair of angle brackets right before the curly braces: #import #import “FBConnect/FBConnect.h” @interface MainViewController : UIViewController } Also notice how you will need to add