在iOS Swift中,要获取Facebook用户的封面照片网址,您需要使用Facebook SDK
LoginManager
类来实现这一点。以下是一个简单的示例:import FBSDKLoginKit
let loginManager = LoginManager()
loginManager.logIn(permissions: ["public_profile", "user_photos"], from: self) { loginResult in
switch loginResult {
case .success(let grantedPermissions, _, let accessToken):
if grantedPermissions.contains("user_photos") {
self.getFacebookCoverPhotoURL(accessToken: accessToken)
}
case .cancelled:
print("User cancelled login.")
case .failed(let error):
print("Login failed: \(error.localizedDescription).")
}
}
FBSDKGraphRequest
类来发起这个请求。以下是一个示例:import FBSDKCoreKit
func getFacebookCoverPhotoURL(accessToken: AccessToken) {
let graphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "cover"], tokenString: accessToken.tokenString, version: nil, httpMethod: .get)
graphRequest.start { (connection, result, error) in
if let error = error {
print("Error fetching cover photo: \(error.localizedDescription).")
return
}
guard let result = result as? [String: Any], let cover = result["cover"] as? [String: Any] else {
print("Error parsing cover photo data.")
return
}
if let coverPhotoURLString = cover["source"] as? String {
let coverPhotoURL = URL(string: coverPhotoURLString)!
print("Cover photo URL: \(coverPhotoURL)")
} else {
print("Error retrieving cover photo URL.")
}
}
}
领取专属 10元无门槛券
手把手带您无忧上云