gpt4 book ai didi

ios - 在 iOS 上从 Facebook 获取用户数据

转载 作者:行者123 更新时间:2023-11-28 20:07:55 28 4
gpt4 key购买 nike

我是 iOS 新手 我只想获取性别、城市、电子邮件和出生日期等信息

但是从我在下面发布的代码中,我得到了一个想法,比如如何在 Facebook 上发布数据,

现在我想从 Facebook 获取用户详细信息

.h

#import <UIKit/UIKit.h>
#import <Social/Social.h>

@interface ViewController : UIViewController
- (IBAction)PostFB:(id)sender;
- (IBAction)PostTW:(id)sender;

@end

.m

//
// ViewController.m
// FaceBookFirstApp3
//
// Created by hits1 on 27/01/14.
// Copyright (c) 2014 hits1. All rights reserved.
//

#import "ViewController.h"
#import <Social/Social.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)PostFB:(id)sender {

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

[controller setInitialText:@"First post from my iPhone app"];

[self presentViewController:controller animated:YES completion:Nil];
}
}

- (IBAction)PostTW:(id)sender {

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@"Great fun to learn iOS programming at appcoda.com!"];
[tweetSheet addURL:[NSURL URLWithString:@"http://www.appcoda.com"]];
[tweetSheet addImage:[UIImage imageNamed:@"socialsharing-facebook-image.jpg"]];
[self presentViewController:tweetSheet animated:YES completion:nil];
}



}
@end

请帮我从 Gmail 或 Facebook 中获取至少一个字段(城市(或)性别(或)电话号码)

最佳答案

您可以使用以下代码获取用户的性别、城市、电子邮件和出生日期

在.h文件中添加,

#import <Accounts/Accounts.h>
@property (nonatomic, retain) ACAccountStore *accountStore;
@property (nonatomic, retain) ACAccount *facebookAccount;
-(void)get;
-(void)attemptRenewCredentials;
- (void) getuserdetails;

在 .m 文件中

- (void) getuserdetails
{

self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSString *key = @"your app id";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];


[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
self.facebookAccount = [accounts lastObject];
NSLog(@"facebook account =%@",self.facebookAccount);
[self get];
} else {
dispatch_async(dispatch_get_main_queue(), ^{

// NSLog(@"%@",e.description);
if([e code]== ACErrorAccountNotFound)
{
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Account not found"
message:@"Please setup your Facebook account in Settings App" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alt show];
}
else
{
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Access Denied"
message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alt show];
}

});
NSLog(@"error getting permission %@",e);

}
}];
}

-(void)get
{

NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me"];

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = self.facebookAccount;

[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {

if(!error)
{
list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSLog(@"Dictionary contains: %@", list );
NSLog(@"EmailID %@",[list objectForKey:@"email"]);

NSLog(@"Birthday %@",[list objectForKey:@"birthday"]);
NSLog(@"Gender %@",[list objectForKey:@"gender"]);

NSLog(@"City %@",[[list objectForKey:@"location"] objectForKey:@"name"]);


if([list objectForKey:@"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
});

}
else{
//handle error gracefully
NSLog(@"error from get%@",error);
//attempt to revalidate credentials
}

}];


}

-(void)attemptRenewCredentials{
[self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
if(!error)
{
switch (renewResult) {
case ACAccountCredentialRenewResultRenewed:
NSLog(@"Good to go");

[self get];
break;
case ACAccountCredentialRenewResultRejected:
{
NSLog(@"User declined permission");
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Access Denied"
message:@"You declined permission" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alt show];
break;
}
case ACAccountCredentialRenewResultFailed:
{
NSLog(@"non-user-initiated cancel, you may attempt to retry");
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"Access Denied"
message:@"non-user-initiated cancel, you may attempt to retry" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
[alt show];
break;
}
default:
break;
}

}
else{
//handle error gracefully
NSLog(@"error from renew credentials%@",error);
}
}];


}

-(void)accountChanged:(NSNotification *)notif//no user info associated with this notif
{
[self attemptRenewCredentials];
}

关于ios - 在 iOS 上从 Facebook 获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397997/

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