最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[基本解决]swift实现读取手机联系人

Swift crifan 2393浏览 0评论

swift read phone contact list

How to retrieve address book contacts with Swift? – Stack Overflow

Accessing iOS Address Book with Swift: array count of zero – Stack Overflow

ios – How to fetch custom label phone numbers in address book swift – Stack Overflow

Contacts – iOS 9 Swift Programming Cookbook [Book]

13. Address Book – iOS 8 Swift Programming Cookbook [Book]

Swift: Get contact emails from AddressBook · GitHub

Requesting Access to the Address Book – Swift Language – iOS 8 | Ashish Kakkad

SocialbitGmbH/SwiftAddressBook · GitHub

->

https://github.com/SocialbitGmbH/SwiftAddressBook/archive/master.zip

结果下载下来代码,使用,结果返回false:

        swiftAddressBook?.requestAccessWithCompletion { (addressBook :Bool, _ :CFError?) -> Void in
            print("addressBook=\(addressBook)") //false
            if addressBook { 

看起来应该是:

权限问题,但是不知道如何解决。

然后去看介绍:

How To · SocialbitGmbH/SwiftAddressBook Wiki · GitHub

结果还是不行:

        let status = SwiftAddressBook.authorizationStatus()
        print("status=\(status)") //ABAuthorizationStatus
        swiftAddressBook?.requestAccessWithCompletion { (addressBook: Bool, error: CFError?) -> Void in
            print("addressBook=\(addressBook), error=\(error)")
            //addressBook=false, error=nil
            if addressBook {

参考:

Swift: Get contact emails from AddressBook · GitHub

去试试:

        let status = SwiftAddressBook.authorizationStatus()
        print("status=\(status)") //ABAuthorizationStatus
        print("status.rawValue=\(status.rawValue)") //0
        print("status.hashValue=\(status.hashValue)") //0
        swiftAddressBook?.requestAccessWithCompletion { (addressBook: Bool, error: CFError?) -> Void in
            print("addressBook=\(addressBook), error=\(error)")
            //addressBook=false, error=nil
            if addressBook {

结果是0,

此处的0,好像应该是:

@available(iOS, introduced=2.0, deprecated=9.0, message="use CNAuthorizationStatus")
public enum ABAuthorizationStatus : CFIndex {
   
    case NotDetermined // deprecated, use CNAuthorizationStatusNotDetermined
    case Restricted // deprecated, use CNAuthorizationStatusRestricted
    case Denied // deprecated, use CNAuthorizationStatusDenied
    case Authorized // deprecated, use CNAuthorizationStatusAuthorized
}
@available(iOS, introduced=2.0, deprecated=9.0, message="use [CNContactStore authorizationStatusForEntityType:]")
public func ABAddressBookGetAuthorizationStatus() -> ABAuthorizationStatus

中的:NotDetermined

搜:

swift ABAddressBookGetAuthorizationStatus NotDetermined

ABAddressBook Reference

Address Book Framework Reference for iOS

Swift – 添加、修改、删除通讯录联系人

How To Import iOS Address Book | ishaq.pk

有点点明白了:

此处的:

ABAddressBookGetAuthorizationStatus()

就是类似于安卓中,弹出一个对话框,让用户选择是否允许某个权限的

但是此处iOS模拟器中,好像没有反应,就直接返回了,没有弹出类似的对话框。

iPhone & iPad Application Development Help World: Save contacts Address Book Tutorial in iOS sdk

难道是:

此处的iOS模拟器中,没有创建过联系人的地址簿?

从而导致此处的错误?

有人说是:

需要在:

viewDidLoad

中执行这个判断,去试试。

结果错误依旧。。

难道是由于此处的iOS版本是9.0之后的9.2,而导致了ABAddressBookGetAuthorizationStatus函数过期了,所以没法用了?

不过后来确定,总之新版iOS 9.0之后是用了新的接口:

[规避解决]swift中通过新接口CNContactStore去获取地址簿联系人列表

难道是因为:

IOS调用系统通讯录 – 珲少的个人空间 – 开源中国社区

中提到的:

没有包含对应的库?

AddressBook.framework

AddressBookUI.framework

转载请注明:在路上 » [基本解决]swift实现读取手机联系人

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.190 seconds, using 22.10MB memory