質問

I have used the SLRequest for getting profile info and i followed the Getting user profile picture in iOS6? to get the profile image. But it returning 50x50 image dimension. I need large size of profile picture. Can anyone give me some idea to do this using SLRequest?

役に立ちましたか?

解決

use the below URL to get the large image:

https://graph.facebook.com/fb_user_id/picture?type=large

For cover image, use the below:

http://graph.facebook.com/fb_user_id?fields=cover

this will return:

{
  "cover": {
    "id": "XXX", 
    "source": "cover_image_url", 
    "offset_y": 66
  }, 
  "id": "XXXXXX"
}

他のヒント

You can get the large image of profile picture as

[NSURL URLWithString:@"https://graph.facebook.com/me/picture?type=large"];

The type parameter supports

enum{square,small,normal,large}

Here is the answer for swift 3.

  1. you have to get the access to the facebook account of the user in his or her device, this using accountStore API:

      func accessFacebookAccount(onCompletion: @escaping(_ tokenCredential: 
      String, 
       _ facebookAccount: ACAccount) -> Void, onError: @escaping (_ 
     errorMessage:  String?) -> Void) {
    
    
            let accountStore = ACAccountStore()
            let facebookAccountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
    
            let options = [
            "ACFacebookAppIdKey" : "529210493918001",
            "ACFacebookPermissionsKey" : ["email","public_profile"],
            "ACFacebookAudienceKey" : ACFacebookAudienceOnlyMe] as [String : Any]
    
        accountStore.requestAccessToAccounts(with: facebookAccountType, options: options) { (granted, error) in
            if granted {
    
    
                let fbAccount = accountStore.accounts(with: facebookAccountType).last as? ACAccount
    
                let credential = fbAccount?.credential
    
    
                let token = credential?.oauthToken
                onCompletion(token!, fbAccount!)
    
            } else {
                let error = "Access to Facebook was not granted."
                onError(error)
    
            }
        }
    }
    
  2. Once you get the response, and the user granted the access to the account. You can make use of the ACAccount object and access the facebook info via a SLRequest:

    func getFacebookInfoViaAccounts (_ fbAccount: ACAccount) {
    
    
    let myURL = URL(string: "https://graph.facebook.com/me")
    
    let request = SLRequest.init(forServiceType: SLServiceTypeFacebook, requestMethod: .GET, url: myURL, parameters: ["fields":"email, first_name, last_name,birthday, gender"])
    
    request?.account = fbAccount
    
    request?.perform(handler: { (dataResponse, urlResponse, error) in
    
        if error == nil {
    
            let json = try? JSONSerialization.jsonObject(with: dataResponse!, options: [ JSONSerialization.ReadingOptions.allowFragments])
    
            if let dict = json as? [String: Any] {
    
                print("My data \(dict)")
            }
        }
    
    })
    
    let myPhotoURl = URL(string: "https://graph.facebook.com/me/picture")
    
    let requestPhoto = SLRequest.init(forServiceType: SLServiceTypeFacebook, requestMethod: .GET, url: myPhotoURl, parameters: ["type":"large"])
    
    requestPhoto?.account = fbAccount
    
    requestPhoto?.perform(handler: { (dataResponse, urlResponse, error) in
    
        if error == nil {
    
            print("URL DE LA PHOTO \(urlResponse?.value(forKey: "URL"))")
        }
    
    })
    
    }
    

Above is the code for getting the image of the users profile and the info, like email, gender and birthday.

This is the way apps like tinder get your data and dont request the user to log in to the facebook account, if the user has his facebook account linked to his or her iphone.

I hope this helps somebody, happy coding :).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top