问题答案
RT,viewDidLoad中写了加载通讯录的部分,其中若是第一次运行应用,会请求授权,但是授权的对话框缺看不到
 
// ViewController.swift
 
import UIKit
import AddressBook
import AddressBookUI
 
class ViewController: UIViewController {
   override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var testSwiftContacts:Array = getSysContacts()
        if testSwiftContacts.isEmpty {
            println("no contact")
        }
        for contact in testSwiftContacts {
            println(contact["FirstName"]!+"·"+contact["LastName"]!)
        }
    }
}
 
//  SwiftContactTool.swift
 
import Foundation
import AddressBook
import AddressBookUI
 
func getSysContacts() -> [[String:String]] {
    var error:Unmanaged<CFError>?
    var addressBook: ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()
 
    let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus()
 
    if sysAddressBookStatus == .Denied || sysAddressBookStatus == .NotDetermined {
        // Need to ask for authorization
        var authorizedSingal:dispatch_semaphore_t = dispatch_semaphore_create(0)
        var askAuthorization:ABAddressBookRequestAccessCompletionHandler = { success, error in
            if success {
                ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray
                dispatch_semaphore_signal(authorizedSingal)
            }
        }
        ABAddressBookRequestAccessWithCompletion(addressBook, askAuthorization)
        dispatch_semaphore_wait(authorizedSingal, DISPATCH_TIME_FOREVER)
    }
 
    return analyzeSysContacts( ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray )
}
 
func analyzeSysContacts(sysContacts:NSArray) -> [[String:String]] {
    var allContacts:Array = [[String:String]]()
    for contact in sysContacts {
        var currentContact:Dictionary = [String:String]()
        // 姓
        currentContact["FirstName"] = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as String? ?? ""
        // 名
        currentContact["LastName"] = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as String? ?? ""
        // 昵称
        currentContact["Nikename"] = ABRecordCopyValue(contact, kABPersonNicknameProperty)?.takeRetainedValue() as String? ?? ""
        // 公司(组织)
        currentContact["Organization"] = ABRecordCopyValue(contact, kABPersonOrganizationProperty)?.takeRetainedValue() as String? ?? ""
        // 职位
        currentContact["JobTitle"] = ABRecordCopyValue(contact, kABPersonJobTitleProperty)?.takeRetainedValue() as String? ?? ""
        // 部门
        currentContact["Department"] = ABRecordCopyValue(contact, kABPersonDepartmentProperty)?.takeRetainedValue() as String? ?? ""
        //备注
        currentContact["Note"] = ABRecordCopyValue(contact, kABPersonNoteProperty)?.takeRetainedValue() as String? ?? ""
        allContacts.append(currentContact)
    }
    return allContacts
}
下图是应用中断后可见到的对话框
应该如何解决呢?
 
标签: swift
答案
这个授权框应该和push通知那个授权框一样的。
由于授权框由系统来调用,系统只有在你第一次真正用到该框架的APIs才会调用授权框。这样的话会由造成延误的可能。所以我觉得你在AppDelegate就调用一次相关的API来获取授权框。
对了,这个与你所使用的语言没有关系噢。无论objc or swift
举一反三