iOS Facebook:使用故事板获取用户数据
Posted
技术标签:
【中文标题】iOS Facebook:使用故事板获取用户数据【英文标题】:iOS Facebook: Fetching user data using storyboard 【发布时间】:2013-11-08 13:49:55 【问题描述】:我是第一次尝试 facebook 集成。我对可以从不同教程站点获得的许多代码感到困惑。 我已使用以下代码从 facebook 数据中获取用户信息,但我无法使用情节提要获取数据。有人可以建议这段代码有什么问题吗?
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
return wasHandled;
此处的以下代码段没有被执行。那么还有其他方法可以将控件放入此代码中吗?
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
regisrationdetails.fbid = user.id;
regisrationdetails.firstname = user.first_name;
休息一下,我尝试了不同的类型来调用它,但它没有发生。
- (void)loginView:(FBLoginView *)loginView
handleError:(NSError *)error
NSString *alertMessage, *alertTitle;
if (error.fberrorShouldNotifyUser)
alertTitle = @"Facebook Error";
alertMessage = error.fberrorUserMessage;
else if (error.fberrorCategory == FBErrorCategoryAuthenticationReopenSession)
alertTitle = @"Session Error";
alertMessage = @"Your current session is no longer valid. Please log in again.";
else if (error.fberrorCategory == FBErrorCategoryUserCancelled)
NSLog(@"user cancelled login");
else
alertTitle = @"Unknown Error";
alertMessage = @"Error. Please try again later.";
NSLog(@"Unexpected error:%@", error);
if (alertMessage)
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
- (void)viewDidLoad
[FBLoginView class];
FBLoginView *loginView = [[FBLoginView alloc] init];
loginView.readPermissions= @[@"email", @"user_likes"];
loginView.publishPermissions = @[@"publish_actions"];
loginView.defaultAudience = FBSessionDefaultAudienceFriends;
FBSession *session = [[FBSession alloc] init];
[FBSession setActiveSession:session];
[session openWithBehavior:FBSessionLoginBehaviorWithNoFallbackToWebView
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error)
];
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
if (!error)
regisrationdetails.firstname = [result objectForKey:@"name"];
];
NSLog(@"%@", regisrationdetails.firstname);
[FBRequestConnection
startForMeWithCompletionHandler:^(FBRequestConnection *connection,
id<FBGraphUser> user,
NSError *error)
if (!error)
regisrationdetails.firstname = user.name;
regisrationdetails.address1 = user.location[@"name"];
regisrationdetails.city = user[@"city"];
];
- (IBAction)CreateAccount:(id)sender
regisrationdetails.firstname=_FirstName.text;
regisrationdetails.lastname=_LastName.text;
regisrationdetails.email=_EmailAddress.text;
regisrationdetails.address1=_Address1.text;
regisrationdetails.address2=_Address2.text;
regisrationdetails.city=_City.text;
regisrationdetails.state=_State.text;
regisrationdetails.zip=_Zip.text;
NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@first_name=%@&last_name=%@&email=%@&address_1=%@&address_2=%@&city=%@&state=%@&zip=%@&&fb_id=%@&action=fbsignin",MainURL, regisrationdetails.firstname,regisrationdetails.lastname,regisrationdetails.email,regisrationdetails.address1,regisrationdetails.address2,regisrationdetails.city,regisrationdetails.state,regisrationdetails.zip,regisrationdetails.fbid ]];
NSError *errors;
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errors];
status = json[@"status"];
error = json[@"error"];
user = json[@"user"];
if ([status isEqualToString:@"success"])
UIAlertView *success=[[UIAlertView alloc]initWithTitle:@"Success" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[success show];
else if ([status isEqualToString:@"failure"])
messages=[[NSString alloc]init];
for (int i=0; i<[error count]; i++)
messages=[messages stringByAppendingString:[error objectAtIndex:i]];
messages=[messages stringByAppendingString:@"\n"];
UIAlertView *failure=[[UIAlertView alloc]initWithTitle:@"Failure" message:messages delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
[failure show];
是否有一个简单的示例代码可用于使用情节提要获取 facebook 用户信息??
【问题讨论】:
【参考方案1】:确保你已经实现了 facebook 委托方法,
fbLoginview.delegate = self;
并实现该方法获取用户信息
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
// here we use helper properties of FBGraphUser to dot-through to first_name and
// id properties of the json response from the server; alternatively we could use
NSLog(@"user :%@",user);
【讨论】:
视图出现了,我可以登录 FB 帐户。但它不要求任何许可或检索数据。以上是关于iOS Facebook:使用故事板获取用户数据的主要内容,如果未能解决你的问题,请参考以下文章
在 ios xcode 4.3(故事板)中弹出 facebook feed 对话框(用于墙贴)
Facebook iOS sdk xcode 4.3/storyboards
获取 ios 故事板 webView 应用程序以在安全区域呈现