Capturing Facebook values

Hi all - I'm new to Xcode but not programming - I've done VB.Net before and have been a BI Developer using SQL Server for years.
I've managed to implement Facebook integration into my app. I'm just wondering what best practices are generally used.

If you need to refer to the Facebook Id, name etc, would you capture these at the point of logging in, and write them into a local database? (I know there are some local databases in IOS). Or do you need to make a GraphRequest call each time you want to get a value?

Secondly, assuming this is a reasonable method, I went down the route of creating a class and having function within it to return values. My IsLoggedIn function performs as expected. However, fetchUserProfile returns an empty string, presumably because the Facebook Id that is captured into retUser is out of scope when it comes to returning the value:
Code Block import UIKit
import FBSDKLoginKit
import FBSDKCoreKit
class FaceBookRetrieve: NSObject {
    var tokenstring=AccessToken.current?.tokenString
    
    func IsLoggedIn()  -> Bool
    {
        var LoggedIn=false
        if let token=AccessToken.current,!token.isExpired {
            LoggedIn=true
          
        }
        return LoggedIn
    }
    
    
    func fetchUserProfile()->String {
    
        var retUser:String = ""
              if FaceBookRetrieve().IsLoggedIn()==true {
                  //user is logged in
        
                  if let accesstoken=AccessToken.current,!accesstoken.isExpired{
                  let token:String? = accesstoken.tokenString
                  let request=FBSDKLoginKit.GraphRequest(graphPath: "me",
                                                         parameters: ["field":"email,name" ],
                                                         tokenString: token,
                                                         version: nil,
                                                         httpMethod: .get)
                  request.start(completionHandler: {connection,result,error in
                      if let userdata = result as? [String:AnyObject] {
                                                                retUser = (userdata["id"] as? String)!
                                                                      }
              
                                                    }
                                )                                               }
                  
                      
                  
                  
                                                        }
        
        return retUser
    }
}

Many thanks for reading.


Replies

I have also asked this question in another forum here:
https://forums.swift.org/t/capturing-facebook-values-in-swift-xcode/38684

Surely someone is able to help me with this? I cannot find anything whatsoever to assist me out there - this must be something that is done in thousands of apps?!