如何在没有Facebook sdk的情况下在iOS中登录(我支持iOS6和iOS7)。我的应用程序使用Facebook登录,用户可以在Facebook上分享帖子。此外,我需要发送Facebook的用户id和身份验证令牌到我的服务器。
早些时候我支持Xcode5,但现在我的iOS目标是使用Xcode5的>= iOS6。
为此,我使用了Facebook SDK 3.11。通过研究,我了解到我们可以使用SLComposeViewController,UIActivityViewController或SLRequest来分享帖子。这将解决我的共享问题,但如何做Facebook登录和获得身份验证令牌?
我尝试了Facebook登录的SLRequest和分享的SLComposeViewController,那么这是一个好的解决方案吗?(在这里,我想要显示Facebook本地共享对话框。所以我没有使用SLRequest来分享帖子,因为我们必须为它创建视图。)
我推荐了this link。这是前进的最佳解决方案吗?
发布于 2014-02-26 21:04:05
只有您的部署目标6 -you在-if中设置了捆绑包id,然后develelopers.facebook.com -you才在您的plist中设置了fb_app_id
在没有facebook sdk的情况下这样做,
#import <Social/Social.h>
#import <Accounts/Accounts.h>
#define FB_APP_ID @"45666675444" // ur id here
@interface OGShareViewController ()
@property (strong, nonatomic) ACAccountStore *accountStore;
@property (strong, nonatomic) ACAccount *facebookAccount;
@property (strong, nonatomic) ACAccountType *facebookAccountType;
@end
-(void)viewDidLoad
{
[self getMyDetails];
}
- (void) getMyDetails {
if (! _accountStore) {
_accountStore = [[ACAccountStore alloc] init];
}
if (! _facebookAccountType) {
_facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
}
NSDictionary *options = @{ ACFacebookAppIdKey: FB_APP_ID };
[_accountStore requestAccessToAccountsWithType: _facebookAccountType
options: options
completion: ^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [_accountStore accountsWithAccountType:_facebookAccountType];
_facebookAccount = [accounts lastObject];
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:url
parameters:nil];
request.account = _facebookAccount;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers
error:nil];
NSLog(@"id: %@", responseDictionary[@"id"]);
NSLog(@"first_name: %@", responseDictionary[@"first_name"]);
NSLog(@"last_name: %@", responseDictionary[@"last_name"]);
NSLog(@"gender: %@", responseDictionary[@"gender"]);
NSLog(@"city: %@", responseDictionary[@"location"][@"name"]);
NSLog(@"user name: %@", responseDictionary[@"username"]);
// http://graph.facebook.com/facebook/picture?type=normal
NSString *userPic = [NSString stringWithFormat:@" http://graph.facebook.com/%@/picture?type=normal", responseDictionary[@"username"]
];
NSLog(@"profile pic link: %@", userPic);
}];
} else {
[self showAlert:@"Facebook access for this app has been denied. Please edit Facebook permissions in Settings."];
}
}];
}
- (void) showAlert:(NSString*) msg {
dispatch_async(dispatch_get_main_queue(), ^(void) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"WARNING"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
});
}https://stackoverflow.com/questions/21955213
复制相似问题