Facebook图谱API(Graph API)是Facebook提供的主要编程接口,允许开发者读取和写入Facebook社交图谱中的数据。"赞"功能是Facebook社交互动的基础元素之一,通过API可以实现对页面、帖子、评论等内容的点赞操作。
首先需要:
import FacebookCore
import FacebookLogin
let loginManager = LoginManager()
loginManager.logIn(permissions: [.publicProfile, .userLikes], from: self) { result, error in
if let error = error {
print("Error: \(error.localizedDescription)")
} else if let result = result, result.isCancelled {
print("Login cancelled")
} else {
print("Logged in")
}
}
func likePost(postId: String) {
let graphRequest = GraphRequest(
graphPath: "/\(postId)/likes",
parameters: [:],
httpMethod: .post
)
graphRequest.start { _, result, error in
if let error = error {
print("Error liking post: \(error.localizedDescription)")
} else {
print("Successfully liked post")
}
}
}
func unlikePost(postId: String) {
let graphRequest = GraphRequest(
graphPath: "/\(postId)/likes",
parameters: [:],
httpMethod: .delete
)
graphRequest.start { _, result, error in
if let error = error {
print("Error unliking post: \(error.localizedDescription)")
} else {
print("Successfully unliked post")
}
}
}
func checkIfLiked(postId: String, completion: @escaping (Bool) -> Void) {
let graphRequest = GraphRequest(
graphPath: "/me/likes/\(postId)",
parameters: [:],
httpMethod: .get
)
graphRequest.start { _, result, error in
if let error = error {
print("Error checking like: \(error.localizedDescription)")
completion(false)
} else if let result = result as? [String: Any], !result.isEmpty {
completion(true)
} else {
completion(false)
}
}
}
问题:收到"Permissions error"或"User hasn't authorized the application to do this"错误。
原因:缺少必要的权限或用户未授权。
解决:
user_likes
权限问题:API调用返回版本不支持的错误。
解决:
graphPath: "/v12.0/\(postId)/likes"
问题:收到"Invalid object ID"错误。
解决:
问题:收到"API calls too frequent"错误。
解决:
通过合理使用Facebook图谱API的点赞功能,可以显著提升iOS应用的社交互动性和用户参与度。
没有搜到相关的文章