Search code examples
swiftfacebook-graph-apiswift3fbsdkfbsdkloginkit

Swift FBSDK Get user country


I am currently implementing Facebook login for my app, and I am trying to fetch the following info from the profile:

first name, last name, gender, and country.

However, I can only seem to fetch everything except the user country. I have tried to set my parameters to my FBSDKGraphRequest like this:

let parameters = ["fields": "email, gender, user_location, id, first_name, last_name,  picture.type(large)"]

as well as my readPermissions to:

loginButton.readPermissions = ["public_profile", "email", "user_location", "user_friends", "user_education_history", "user_birthday"]

What am I doing wrong here? How can I obtain the country of the user?

Help is greatly appreciated!


Solution

  • Your readPermissions user_location is correct but the parameter name for country/location is not user_location.
    It is location.

    let parameters = ["fields": "id,location,name,first_name,last_name,picture.type(large),email,birthday,gender,bio,relationship_status"]
    

    Make sure you have added a place in your Facebook account and made that public in which your are logging in and you will be done. Try with the same facebook account from which you have created the facebook app.

    You can get your location with a warning message(as shown screenshot later below) for testing purposes. But before submitting your app to appstore, you need to go through the Login Review from facebook as user_location is not a pre-Approved permission.

    For More Details go to Graph API Explorer

    You can go to the Graph API Explorer and try out with your app. Generate a token by clicking on Get Token and Get User Access Token. On clicking it will open the permission checkboxes. Keep user_location checked.

    Graph Api Explorer Page

    enter image description here

    Next just click on Submit with the following Get Parameters. Make sure that the location parameter is there and you can see a JSON like this as shown.
    There is a location dictionary with name key which consist of your country and city name.

    {
      "id": "338058996547981",
      "name": "Rajan Maheshwari",
      "birthday": "04/29/1990",
      "location": {
        "id": "106517799384578",
        "name": "New Delhi, India"
      }
    }
    

    enter image description here

    NOTE: - Also you have to make sure that you have entered a place in your Facebook profile and made that public.

    enter image description here

    When you will run the app with the permissions, you will get a Facebook authentication with a warning screen like this.

    enter image description here

    It will ask for a Login Review as some of the permissions need review here. The basic permissions provided free by facebook which don't need any review are:-

    enter image description here

    but for testing purposes you can get the location without any review. But before you will submit the iOS app to appstore, you need to pass the login review of Facebook in order to avoid that warning message and to fetch location of the user.