Cập nhật cho Swift 3
@IBAction func fblogin(_ sender: Any) {
let loginManager = LoginManager()
UIApplication.shared.statusBarStyle = .default // remove this line if not required
loginManager.logIn([ .publicProfile,.email ], viewController: self) { loginResult in
print(loginResult)
//use picture.type(large) for large size profile picture
let request = GraphRequest(graphPath: "me", parameters: ["fields":"email,name,gender,picture"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: FacebookCore.GraphAPIVersion.defaultVersion)
request.start { (response, result) in
switch result {
case .success(let value):
print(value.dictionaryValue)
case .failed(let error):
print(error)
}
}
}
}
Đối với Objective-C
Bạn có thể gọi phương pháp này trên UIButton
nhấp chuột kiện
-(void)fblogin{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
if ([UIApplication.sharedApplication canOpenURL:[NSURL URLWithString:@"fb://"]])
{
login.loginBehavior = FBSDKLoginBehaviorSystemAccount;
}
[login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
NSLog(@"Unexpected login error: %@", error);
NSString *alertMessage = error.userInfo[FBSDKErrorLocalizedDescriptionKey] ?: @"There was a problem logging in. Please try again later.";
NSString *alertTitle = error.userInfo[FBSDKErrorLocalizedTitleKey] ?: @"Oops";
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
else
{
if(result.token) // This means if There is current access token.
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:@{@"fields": @"picture, name, email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id userinfo, NSError *error) {
if (!error) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
dispatch_async(dispatch_get_main_queue(), ^{
// you are authorised and can access user data from user info object
});
});
}
else{
NSLog(@"%@", [error localizedDescription]);
}
}];
}
NSLog(@"Login Cancel");
}
}];
}
Nguồn
2016-02-02 18:16:33
Giải pháp siêu bẩn xD: D – Abo3atef
nó phù hợp với tôi. Cảm ơn bạn –
Nope: 'lỗi nghiêm trọng: bất ngờ tìm thấy nil khi unwrapping một giá trị tùy chọn ' – MarksCode