Skip to content

Commit

Permalink
w
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschechter committed Jun 27, 2012
1 parent 0557d98 commit 25f9e3f
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 550 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

@interface FPAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) FBSession *session;
@property (strong, nonatomic) FPViewController *viewController;
@property (strong, nonatomic) FPViewController *rootViewController;
@property (strong, nonatomic) UIWindow *window;

@end
28 changes: 15 additions & 13 deletions samples/FriendPickerSample/FriendPickerSample/FPAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
@implementation FPAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize session = _session;
@synthesize rootViewController = _rootViewController;

// FBSample logic
// In the login workflow, the Facebook native application, or Safari will transition back to
Expand All @@ -31,7 +30,7 @@ - (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [self.session handleOpenURL:url];
return [FBSession.activeSession handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Expand All @@ -41,28 +40,31 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// http://stackoverflow.com/questions/1725881/unknown-class-myclass-in-interface-builder-file-error-at-runtime
[FBFriendPickerViewController class];

// FBSample logic
// Here we allocate our FBSession object, and assign it to the property referenced by the rest of the
// application
self.session = [[FBSession alloc] init];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[FPViewController alloc] initWithNibName:@"FPViewController_iPhone" bundle:nil];
self.rootViewController = [[FPViewController alloc] initWithNibName:@"FPViewController_iPhone" bundle:nil];
} else {
self.viewController = [[FPViewController alloc] initWithNibName:@"FPViewController_iPad" bundle:nil];
self.rootViewController = [[FPViewController alloc] initWithNibName:@"FPViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
self.rootViewController.navigationItem.title = @"Friend Picker";

// Set up a UINavigationController as the basis of this app, with the nib generated viewController
// as the initial view.
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:self.rootViewController];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

return YES;
}

// FBSample logic
// It is important to close any FBSession object that is no longer useful
- (void)applicationWillTerminate:(UIApplication *)application {
// Close the session token before quitting
[self.session close];
[FBSession.activeSession close];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

#import <UIKit/UIKit.h>
#import <FBiOSSDK/FacebookSDK.h>

@interface FPViewController : FBFriendPickerViewController
@interface FPViewController : UIViewController

- (IBAction)pickFriendsButtonClick:(id)sender;

@end
100 changes: 47 additions & 53 deletions samples/FriendPickerSample/FriendPickerSample/FPViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,26 @@
// We need to handle some of the UX events related to friend selection, and so we declare
// that we implement the FBFriendPickerDelegate here; the delegate lets us filter the view
// as well as handle selection events
@interface FPViewController () <FBFriendPickerDelegate>
@interface FPViewController ()

@property (strong, nonatomic) IBOutlet UITextView *selectedFriendsView;
@property (strong, nonatomic) IBOutlet UISegmentedControl *sortBySegmentedControl;
@property (strong, nonatomic) IBOutlet UISegmentedControl *displayBySegmentedControl;
@property (retain, nonatomic) FBFriendPickerViewController *friendPickerController;

- (void)sessionChanged;
- (IBAction)sortBySegmentedControlValueChanged:(id)sender;
- (IBAction)displayBySegmentedControlValueChanged:(id)sender;
- (void)doneButtonWasPressed:(id)sender;
- (void)cancelButtonWasPressed:(id)sender;
- (void)fillTextBoxAndDismiss:(NSString *)text;

@end

@implementation FPViewController

@synthesize selectedFriendsView = _friendResultText;
@synthesize sortBySegmentedControl = _sortBySegmentedControl;
@synthesize displayBySegmentedControl = _displayBySegmentedControl;
@synthesize friendPickerController = _friendPickerController;

- (void)viewDidLoad {
[super viewDidLoad];

// FBSample logic
// We are inheriting FBFriendPickerViewController, and so in order to handle events such
// as selection change, we set our base class' delegate property to self
self.delegate = self;

self.sortBySegmentedControl.selectedSegmentIndex = 0;
self.sortOrdering = FBFriendSortByFirstName;
self.displayBySegmentedControl.selectedSegmentIndex = 0;
self.displayOrdering = FBFriendDisplayByFirstName;



// FBSample logic
// We call our session-related workhorse here to update the state of the view and session
Expand All @@ -63,21 +52,12 @@ - (void)viewDidLoad {
// FBSample logic
// This method is responsible for keeping UX and session state in sync
- (void)sessionChanged {
// get the app delegate
FPAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

// if the session is open, then load the data for our view controller
if (appDelegate.session.isOpen) {
// setting the session property on our base class in order to
// enable fetching data by the base view controller
self.session = appDelegate.session;

// the view controllers in the SDK have a loadData method, which is used to update
// the view to reflect changes to properties and other setting which may require
// a refetch from the server
[self loadData];
} else {
if (!FBSession.activeSession.isOpen) {

// if the session is closed, then we open it here, and establish a handler for state changes
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
[FBSession.activeSession openWithCompletionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
switch (state) {
Expand All @@ -102,41 +82,55 @@ - (void)sessionChanged {
}
}

// FBSample logic
// In the following methods, we make changes to the base view controller state, and so we need to call
// load data in order to refetch from the server
- (IBAction)sortBySegmentedControlValueChanged:(id)sender {
self.sortOrdering = ([sender selectedSegmentIndex] == 0) ? FBFriendSortByFirstName : FBFriendSortByLastName;
[self loadData]; // refetch from the server
}

- (IBAction)displayBySegmentedControlValueChanged:(id)sender {
// another property that when changes requires a refetch from the server
// Note: the reason that setting the properties doesn't implicitly refetch is that we wanted to leave
// network I/O in the hands of the application within reason. For example, suppose that two or three properties
// were being set -- it would be ideal for the application to wait until all three are set before issueing a
// loadData call which will result in a network round-trip
self.displayOrdering = ([sender selectedSegmentIndex] == 0) ? FBFriendDisplayByFirstName : FBFriendDisplayByLastName;
[self loadData];
}
- (IBAction)pickFriendsButtonClick:(id)sender {

// Create friend picker, and get data loaded into it.
FBFriendPickerViewController *friendPicker = [[FBFriendPickerViewController alloc] init];
self.friendPickerController = friendPicker;

#pragma mark - FBFriendPickerDelegate implementation
[friendPicker loadData];

// Create navigation controller related UI for the friend picker.
friendPicker.navigationItem.title = @"Pick Friends";
friendPicker.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(doneButtonWasPressed:)];
friendPicker.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(cancelButtonWasPressed:)];

// Make current.
[self.navigationController pushViewController:friendPicker animated:YES];
}

// FBSample logic
// A delegate method for the view controller, which is called when the selection changes
- (void)friendPickerViewControllerSelectionDidChange:(FBFriendPickerViewController *)friendPicker {
- (void)doneButtonWasPressed:(id)sender {
NSMutableString *text = [[NSMutableString alloc] init];

// we pick up the users from the selection, and create a string that we use to update the text view
// at the bottom of the display; note that self.selection is a property inherited from our base class
for (id<FBGraphUser> user in self.selection) {
for (id<FBGraphUser> user in self.friendPickerController.selection) {
if ([text length]) {
[text appendString:@", "];
}
[text appendString:user.name];
}

[self fillTextBoxAndDismiss:text];
}

- (void)cancelButtonWasPressed:(id)sender {
[self fillTextBoxAndDismiss:@"<Cancelled>"];
}

- (void)fillTextBoxAndDismiss:(NSString *)text {
self.selectedFriendsView.text = text;
[self.navigationController popViewControllerAnimated:YES];
}


@end
Loading

0 comments on commit 25f9e3f

Please sign in to comment.