我第一次尝试 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 用户信息?