gpt4 book ai didi

ios - 尝试访问 iOS 10 中的联系人框架时崩溃

转载 作者:行者123 更新时间:2023-12-01 15:54:04 25 4
gpt4 key购买 nike

我正在尝试访问联系人框架,以便将新联系人保存到用户地址簿中。我有以下代码...

import Contacts
import ContactsUI

在文件的头部并包含一个方法...

func requestForAccess(completion: (granted: Bool) -> ()) {

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_BACKGROUND.rawValue), 0), {
let authStatus = CNContactStore.authorizationStatusForEntityType(.Contacts)

switch authStatus {
case .Authorized:
completion(granted: true)
case .Denied, .NotDetermined:

// CRASH HERE before the completion block is called, after calling
// requestAccessForEntityType

self.contactStore.requestAccessForEntityType(.Contacts, completionHandler: { (access, accessError) -> () in
if access {
completion(granted: access)
} else {
if authStatus == .Denied {
dispatch_async(dispatch_get_main_queue(), { () -> () in
let msg = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
//self.alertService.showAlert(msg)
})
}
}
})
default:
completion(granted: false)
}
})
}

在标记的行,有一个 SIABRT 崩溃。关闭堆栈并运行 po $arg1 会抛出以下内容......

error: Couldn't materialize: couldn't read the value of register x0
Errored out in Execute, couldn't PrepareToExecuteJITExpression

我在根 dictInfo.plist 中有必要的行

<key>NSContactsUsageDescription</key>
<string>We need to access Contacts to import entries direct from the app.</string>

我还在目标属性(常规)的“链接框架和库”中包含了 Contacts.frameworkContactsUI.framework

最佳答案

我已经为联系人描述添加了 plist 条目并稍微修改了您的代码以便在我的 swift 3.0 项目中使用,以下代码在我的 iOS 10.0.2 上运行起来就像一个魅力

   override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated);
self.requestForAccess { (gotAccess) in
print(gotAccess)
}
}

func requestForAccess(_ completion: @escaping (_ granted: Bool) -> ()) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
let authStatus = CNContactStore.authorizationStatus(for: .contacts)

switch authStatus {
case .authorized:
completion(true)
case .denied, .notDetermined:

self.contactStore.requestAccess(for: .contacts, completionHandler: { (access, accessError) -> () in
if access {
completion(access)
} else {
if authStatus == .denied {
DispatchQueue.main.async {
let msg = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
print(msg)
}
}
}
})
default:
completion(false)
}
}
}

关于ios - 尝试访问 iOS 10 中的联系人框架时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40219461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com