我想通了!
我首先遵循了这个教程:http://www.appcoda.com/ios-programming-facebook-login-sdk/
然后我根据需要对其进行了修改。
下面是我的应用程序代码
在 AppDelegate.m 中
#import <FacebookSDK/FacebookSDK.h>
...
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[FBLoginView class];
[FBProfilePictureView class];
}
...
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication];
}
在 CreateAccountConnectSocialMediaViewController.h 中
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
@interface CreateAccountConnectSocialMediaViewController : UIViewController <FBLoginViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *socialMediaView;
- (IBAction)nextButton:(UIButton *)sender;
- (IBAction)openProfileButton:(UIButton *)sender;
@end
在 CreateAccountConnectSocialMediaViewController.m 中
@interface CreateAccountConnectSocialMediaViewController ()
@property FBLoginView *fbLoginButton;
@property NSString *fbUserID;
@end
@implementation CreateAccountConnectSocialMediaViewController
- (void)viewDidLoad {
[super viewDidLoad];
//Create CUSTOM FB Button
self.fbLoginButton = [[FBLoginView alloc] init];
for (id loginObject in self.fbLoginButton.subviews)
{
if ([loginObject isKindOfClass:[UIButton class]])
{
UIButton * loginButton = loginObject;
UIImage *loginImage = [UIImage imageNamed:@"FB_Hover.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
}
if ([loginObject isKindOfClass:[UILabel class]])
{
UILabel *loginLabel = loginObject;
loginLabel.text = @"";
loginLabel.frame = CGRectMake(0, 0, 0, 0);
}
}
self.fbLoginButton.delegate = self;
self.fbLoginButton.readPermissions = @[@"public_profile", @"email"];
self.fbLoginButton.frame = CGRectMake(0, 0, 46, 46);
[self.socialMediaView addSubview:self.fbLoginButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Change the ICON to denote the user is logged in
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
for (id loginObject in self.fbLoginButton.subviews)
{
if ([loginObject isKindOfClass:[UIButton class]])
{
UIButton *loginButton = loginObject;
UIImage *loginImage = [UIImage imageNamed:@"FB_Static.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
}
if ([loginObject isKindOfClass:[UILabel class]])
{
UILabel *loginLabel = loginObject;
loginLabel.text = @"";
loginLabel.frame = CGRectMake(0, 0, 0, 0);
}
}
}
//Grab the users Apped Scoped ID, to use in going to his/her page later
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
self.fbUserID = user.objectID;
}
Change the ICON to denote the user is NOT logged in
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
for (id loginObject in self.fbLoginButton.subviews)
{
if ([loginObject isKindOfClass:[UIButton class]])
{
UIButton * loginButton = loginObject;
UIImage *loginImage = [UIImage imageNamed:@"FB_Hover.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
}
if ([loginObject isKindOfClass:[UILabel class]])
{
UILabel * loginLabel = loginObject;
loginLabel.text = @"";
loginLabel.frame = CGRectMake(0, 0, 0, 0);
}
}
}
-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
NSLog(@"%@", [error localizedDescription]);
}
- (IBAction)nextButton:(UIButton *)sender {
//Go to next CreateAccount View Controller
[self performSegueWithIdentifier:@"accountCreatedSegue1" sender:self];
}
//Open the user's profile in the native FB App or
//the browser if the native FB app doesn't exist
- (IBAction)openProfileButton:(UIButton *)sender {
NSString *urlString = [NSString stringWithFormat:@"fb://profile?app_scoped_user_id=%@", self.fbUserID];
NSLog(@"urlString = %@", urlString);
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}
@end