一直在寻找指导,但我在这里和网上发现的大部分内容都是"如何从iOS应用程序发布到Facebook"。我的问题不同。
我有一个拥有多个用户的应用。
如果user1在我的iOS应用程序上查看user2的个人资料,我想在user2的个人资料上有一个按钮(当用户1按下时)会打开user1的Facebook App显示用户2的Facebook Wall" back"或者"完成"按钮将user1带回我的应用程序。
我认为这很容易,但我无法找到相关信息。
感谢您的帮助:)
答案 0 :(得分:0)
我明白了!
我首先遵循本教程: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