2017-03-03 58 views
0

我試圖在iOS 9在迅速獲得3接觸不靈

func getInnerContacts()-> Observable<[CNContact]>{ 
     return Observable<[CNContact]>.create({ (observer) -> Disposable in 
      var contacts = [CNContact]() 
      let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey] as [Any] 
      self.contactStore.requestAccess(for: .contacts, completionHandler: { (granted, error) -> Void in 
       if granted { 
        let predicate = CNContact.predicateForContactsInContainer(withIdentifier: self.contactStore.defaultContainerIdentifier()) 
        do { 
         contacts = try self.contactStore.unifiedContacts(matching: predicate, keysToFetch: keysToFetch as! [CNKeyDescriptor]) 

         contacts = newContacts.filter({ (contact) -> Bool in 
          return !contact.phoneNumbers.isEmpty && contact.givenName != "" || !contact.phoneNumbers.isEmpty && contact.familyName != "" 
         }) 

         observer.onNext(contacts) 
         observer.onCompleted() 

        }catch { 
         observer.onError(error) 
        } 
       } 
      }) 
      return Disposables.create() 
     }) 
    } 

接觸(SWIFT 3),但它不能正常工作,而且,我看不到的權限警惕。 執行我先檢查了iOS版本,如在此之前:

if #available(iOS 10 , *){ 

}else if #available(iOS 9, *){    

} 

然後我加入許可標記,Privacy - Contacts Usage Description到Info.plist文件。這適用於IOS 10細,任何想法我怎麼能在IOS實現這個9

回答

1

它會在viewDidLoad中很好地工作在斯威夫特3.

var store = CNContactStore() 
var contacts = [CNContact]() 

試試這個():

let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey, CNContactOrganizationNameKey, CNContactImageDataKey] 

    let request = CNContactFetchRequest(keysToFetch: keysToFetch as [CNKeyDescriptor]) 

    do { 
     try self.store.enumerateContacts(with: request) { contact, stop in 

      self.contacts.append(contact) 
     } 

     DispatchQueue.main.async(execute: { 
     for contact in self.contacts { 
      print("contact:\(contact)") 
      let firstName=String(format:"%@",contact.givenName) 
      // print("first:\(firstName)") 
      self.givenNameArray.append(firstName) 
      let lastName=String(format:"%@",contact.familyName) 
      // print("last:\(lastName)") 
      self.familyNameArray.append(lastName) 
      let comapny=String(format:"%@",contact.organizationName) 
      // print("company:\(comapny)") 
      self.organizationNameArray.append(comapny) 
      //get all phone numbers 
      // for ContctNumVar: CNLabeledValue in contact.phoneNumbers 
      // { 
      // let MobNumVar = (ContctNumVar.value).value(forKey: "digits") as? String 
      // print("ph no:\(MobNumVar!)") 

      // get one phone number 
      let MobNumVar = (contact.phoneNumbers[0].value).value(forKey: "digits") as! String 
      // print("mob no:\(MobNumVar)") 
      self.phonenosArray.append(MobNumVar) 
      // get all emails 
      // for ContctNumVar1: CNLabeledValue in contact.emailAddresses 
      // { 
      // print("email \(ContctNumVar1.value(forKey: "value") as! String)") 
      // } 
      // get one email 
      let email = (contact.emailAddresses[0]).value(forKey: "value") as! String 
      // print("email:\(email)") 
      self.emailsArray.append(email) 

      let imagedat=contact.imageData 
      // print("image data:\(imagedat)") 
      let image=UIImage(data: imagedat!) 
      // print("image:\(image)") 
      self.imagesArray.append(image!) 
      } 
     }) 


    } catch { 
     print(error) 
    } 
+0

它在其他情況下工作,但不在我的工作。在iOS 9或更高版本上運行的應用程序的首次啓動期間沒有許可警報,但它適用於iOS 10 –