2017-04-22 86 views
1

我面臨兩個主要問題第一個是: 1.我正在嘗試檢測來電,撥出電話,撥號通話爲此我使用此代碼:CXCallObserver無法正常工作,當運行應用程序超過一個(當包含聯繫人圖像數據時)應用程序崩潰

import UIKit 
import CoreTelephony 
import CallKit 


class ViewController: UIViewController,CXCallObserverDelegate { 


    let callObserver = CXCallObserver() 

    var seconds = 0 
    var timer = Timer() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     callObserver.setDelegate(self, queue: nil) 

    } 



    override func viewWillAppear(_ animated: Bool) { 
     print("viewWillAppear \(seconds)") 


    } 


    fileprivate func runTimer(){ 

     timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTimer), userInfo: nil, repeats: true) 

    } 

    func updateTimer() { 
     seconds += 1 
     print("Seconds \(seconds)") 
    } 



    @IBAction func callButton(_ sender: UIButton) { 


     if let url = URL(string: "tel://\(12345879)"){ 

      UIApplication.shared.open(url, options: [:], completionHandler: nil) 

     } 



    } 


    func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) { 
     if call.hasEnded == true { 
      print("Disconnected") 
      seconds = 0 
      self.timer.invalidate() 

     } 
     if call.isOutgoing == true && call.hasConnected == false { 
      print("Dialing call") 
      self.runTimer() 

     } 
     if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false { 
      print("Incoming") 
     } 

     if call.hasConnected == true && call.hasEnded == false { 
      print("Connected") 
     } 
    } 



} 

它工作正常,當我撥打一個號碼顯示「撥號」,但是當我切斷電話,然後顯示「斷開連接」,然後再「撥號」的狀態。

  1. 另一個問題是,當我從設備獲取所有聯繫人信息時,它工作正常,當我沒有獲取imageData但是當我獲取聯繫人圖像時,它第一次正常工作。然後,如果我再運行一次應用程序變得緩慢。然後下一個崩潰顯示零,同時展開一個值。

我在AppDelegate中編寫了我的聯繫人數據獲取功能。它在應用程序啓動時調用。這是代碼: 一個號碼時我斷開通話,然後如果不幸它去「撥號」選項,我再次檢查了「秒」變量的值,如果:

func fetchContactList(){ 
     let loginInformation = LoginInformation() 
     var contactModelData: [ContactsModel] = [] 
     var profileImage : UIImage? 

     let store = CNContactStore() 
     store.requestAccess(for: .contacts, completionHandler: { 
      granted, error in 

      guard granted else { 
       let alert = UIAlertController(title: "Can't access contact", message: "Please go to Settings -> MyApp to enable contact permission", preferredStyle: .alert) 
       alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 
       self.window?.rootViewController?.present(alert, animated: true, completion: nil) 
       return 
      } 


      let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName),CNContactPhoneNumbersKey, CNContactEmailAddressesKey, CNContactPostalAddressesKey, CNContactImageDataKey, CNContactImageDataAvailableKey,CNContactThumbnailImageDataKey,CNContactThumbnailImageDataKey] as [Any] 
      let request = CNContactFetchRequest(keysToFetch: keysToFetch as! [CNKeyDescriptor]) 
      var cnContacts = [CNContact]() 

      do { 
       try store.enumerateContacts(with: request){ 
        (contact, cursor) -> Void in 
        cnContacts.append(contact) 
       } 
      } catch let error { 
       NSLog("Fetch contact error: \(error)") 
      } 

      for contact in cnContacts { 
       let fullName = CNContactFormatter.string(from: contact, style: .fullName) ?? "No Name" 

       var phoneNumberUnclean : String? 
       var labelofContact : String? 
       var phoneNumberClean: String? 

       for phoneNumber in contact.phoneNumbers { 
        if let number = phoneNumber.value as? CNPhoneNumber, 
         let label = phoneNumber.label { 
         let localizedLabel = CNLabeledValue<CNPhoneNumber>.localizedString(forLabel: label) 
         print("fullname \(fullName), localized \(localizedLabel), number \(number.stringValue)") 
         phoneNumberUnclean = number.stringValue 
         labelofContact = localizedLabel 
        } 



       } 


       if let imageData = contact.imageData { 
        profileImage = UIImage(data: imageData) 
        print("image \(String(describing: UIImage(data: imageData)))") 
       } else { 
        profileImage = UIImage(named: "user") 

       } 




       self.contactModelData.append(ContactsModel(contactName: fullName, contactNumber:phoneNumberUnclean!, contactLabel: labelofContact!, contactImage: profileImage!, contactNumberClean: phoneNumberUnclean!)) 





      } 
      self.loginInformation.saveContactData(allContactData: self.contactModelData) 

     }) 



    } 

回答

0

使用這個我已經解決了這兩個問題它在「撥號」中大於0,然後使線程無效。

第二個問題: 我用Dispatch.async.main後臺線程並取縮略圖

相關問題