[objc/swift] iOS 페이스북 로그인(Feat.PhotoUrl 얻기)

천현철·2021년 3월 25일
0

iOS

목록 보기
37/51

FBSDKLoginKit '~> 11.0'

objc:

@import FBSDKCoreKit;
@import FBSDKLoginKit;

- (void)facebookAuth:(UIViewController*)uiview
{    
    FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
    [loginManager logInWithPermissions:@[ @"public_profile", @"email" ]
                    fromViewController:uiview
                               handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
    {
        if (error) {
            NSLog(@"%@", error.localizedDescription);
        } else if (result.isCancelled){
            NSLog(@"FBLogin cancelled");
        }else{
            if ([FBSDKAccessToken currentAccessToken])
            {
                [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
                                                   parameters:@{@"fields": @"id, name, picture.type(normal), email"}]
                 startWithCompletion:^(id<FBSDKGraphRequestConnecting>  _Nullable connection,
                                       id  _Nullable result,
                                       NSError * _Nullable error)
                 {
                    if (!error) {
                        
                        NSDictionary *dict = result;
                        NSString* displayName = [dict valueForKey:@"name"];
                        NSString* email = [dict valueForKey:@"email"];
                        
                        for(NSString *key in dict.allKeys)
                        {
                            if([key isEqualToString:@"picture"])
                            {
                                NSDictionary *dictPic = [dict valueForKey:@"picture"];
                                NSDictionary *dictData = [dictPic valueForKey:@"data"];
                                NSString* photoURL = [dictData valueForKey:@"url"];                                                                
                            }
                        }
                    }
                }];
            }
        }
    }];
}

swift 5.0:

func facebookLogin(viewController: UIViewController)
{
    Settings.appID = ""
    Settings.displayName = "Dev"

    let loginManager = LoginManager()
    loginManager.logIn(permissions: ["email"],
                        from: viewController) { result, error in
        guard error == nil else {
            print("Error! \(String(describing: error))")
            return
        }
        guard let accessToken = AccessToken.current else {
            print("accessToken is null")
            return
        }
        
        print(accessToken.userID)
        
        GraphRequest(graphPath: "me",
                        parameters: ["fields": "id, name, picture.type(normal), email"]).start(
                        completion: { (connection, result, error) -> Void in
                            guard error == nil else {
                                print("Error! \(String(describing: error))")
                                return
                            }
                            
                            let parsedData = result as! Dictionary<String, AnyObject>?
                            if let email = parsedData?["email"] {
                                print("Email: \(email as! String)")
                            }
                            
                            if let dictData: [String: Any] = result as? [String: Any] {
                                DispatchQueue.main.async {
                                    print(dictData["email"]!)
                                    print(dictData["name"]!)
                                    
                                    if let picData: [String: Any] = dictData["picture"] as? [String: Any] {
                                        if let data: [String: Any] = picData["data"] as? [String: Any] {
                                            print(data["url"]!)
                                        }
                                    }
                                }
                            }
                        })
    }
}

스택오버플로우
https://stackoverflow.com/questions/29323244/facebook-ios-sdk-4-0how-to-get-user-email-address-from-fbsdkprofile
https://stackoverflow.com/questions/59866790/converting-graph-api-from-swift-3-to-swift-5-for-facebook-sdk

페이스북
https://developers.facebook.com/docs/ios/graph?locale=ko_KR

profile
기도하지말고 행동하라

0개의 댓글