Facebook Graph API是Facebook提供的开发者接口,允许开发者访问Facebook平台上的数据。要获取用户的雇主信息,需要使用用户的工作经历数据。
首先需要:
在Podfile中添加:
pod 'FBSDKCoreKit'
pod 'FBSDKLoginKit'
然后运行 pod install
在AppDelegate中添加:
import FBSDKCoreKit
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
ApplicationDelegate.shared.application(app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation])
}
import FBSDKLoginKit
func fetchEmployerName() {
let loginManager = LoginManager()
loginManager.logIn(permissions: ["user_work_history"], from: self) { result, error in
if let error = error {
print("Error: \(error.localizedDescription)")
return
}
guard let result = result, !result.isCancelled else {
print("User cancelled login")
return
}
// 获取工作经历数据
let request = GraphRequest(graphPath: "me", parameters: ["fields": "work"])
request.start { _, result, error in
if let error = error {
print("Error: \(error.localizedDescription)")
return
}
if let result = result as? [String: Any],
let work = result["work"] as? [[String: Any]] {
for job in work {
if let employer = job["employer"] as? [String: Any],
let name = employer["name"] as? String {
print("Employer Name: \(name)")
// 这里可以处理获取到的雇主名称
}
}
}
}
}
}
user_work_history
权限,且需要经过Facebook审核才能在正式应用中使用。如果只需要显示而不需要存储雇主信息,可以考虑使用Facebook的分享对话框或使用Facebook登录仅获取基本资料。
常见错误及解决方法:
通过上述方法,你可以在iOS应用中安全地获取用户的雇主姓名信息。
没有搜到相关的沙龙