gpt4 book ai didi

iOS Facebook : Fetching user data using storyboard

转载 作者:行者123 更新时间:2023-11-29 03:30:29 24 4
gpt4 key购买 nike

我是第一次尝试 facebook 集成。我对可以从不同的教程站点获得的许多代码感到困惑。我使用以下代码从 facebook 数据中获取用户信息,但我无法使用 Storyboard获取数据。有人可以建议这段代码有什么问题吗??

 - (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];
}
}

是否有简单的示例代码可用于使用 Storyboard获取 facebook 用户信息?

最佳答案

确保你已经实现了 facebook 委托(delegate)方法,

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);
}

关于iOS Facebook : Fetching user data using storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19860641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com