更新到XCode 8,Swift 3和iOS 10后,我在请求访问Facebook帐户的权限时开始出现以下错误:
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'访问选项不是 允许此帐户类型。 options参数必须为nil。'
我在SO上发现了三个这样的错误:
前两个没有答案;最后一个建议检查accountType(withAccountTypeIdentifier ...)
返回的值是不是nil
,我已经在做了。
如其他地方所述,此错误与documentation不一致,{{3}}表示Facebook需要选项词典。
这是一个小例子,它可以重现问题并大量显示错误,并检查我认为可以进行错误检查的所有内容:
import UIKit
import Social
import Accounts
class DVSocialTestViewController: UIViewController {
let localURL = URL(string: "fb://")
let serviceType = SLServiceTypeFacebook
let accountID = ACAccountTypeIdentifierFacebook
var accountType: ACAccountType? {
return ACAccountStore().accountType(withAccountTypeIdentifier: self.accountID)
}
var accounts: [Any]? {
guard let ac = self.accountType else { return nil }
return ACAccountStore().accounts(with: ac)
}
static let appID = "MyAppID"
let basicOptions =
[ACFacebookAppIdKey: DVSocialTestViewController.appID,
ACFacebookPermissionsKey: ["basic_info"],
ACFacebookAudienceKey: ACFacebookAudienceFriends] as [AnyHashable: Any]
let publishOptions =
[ACFacebookAppIdKey:DVSocialTestViewController.appID,
ACFacebookPermissionsKey:["publish_actions"],
ACFacebookAudienceKey:ACFacebookAudienceFriends] as [AnyHashable: Any]
var applicationIsInstalled: Bool {
guard let url = self.localURL else {
return false
}
return UIApplication.shared.canOpenURL(url)
}
var serviceIsAvailable: Bool {
return SLComposeViewController.isAvailable(forServiceType: self.serviceType)
}
var userIsLoggedIn: Bool {
guard let ac = self.accounts, ac.count > 0 else {
NSLog("\(self.accounts)")
return false
}
return true
}
func requestPermissionsForAccountType(_ type: ACAccountType) {
ACAccountStore().requestAccessToAccounts(with: type, options: self.basicOptions) {
(success: Bool, error: Error?) -> Void in
if !success {
NSLog("\(error)")
return
}
ACAccountStore().requestAccessToAccounts(with: type, options: self.publishOptions) {
(success: Bool, error: Error?) -> Void in
if !success {
NSLog("\(error)")
return
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
guard self.applicationIsInstalled else {
NSLog("FB Application is not installed.")
return
}
guard self.serviceIsAvailable else {
NSLog("FB Service is unavailable.")
return
}
guard self.userIsLoggedIn else {
NSLog("User is not logged into FB.")
return
}
guard let AT = self.accountType else {
NSLog("No accounts.")
return
}
self.requestPermissionsForAccountType(AT)
}
}
有没有人碰到这个?关于我缺少什么的任何建议?
修改
我觉得我的问题与建议的副本不同,原因很少。