ios - HKObserverQuery Fails with Authorization Not Determined -


when trying set observer query healthkit, lot of users i'm getting error of authorization not determined.

after researching bit saw error should happen when trying write (share) data healthkit. according apple hk documentation - when trying read data user did not grant permissions shouldn't data @ (without errors) if there no new data.

here's code use set observer queries:

class func setobserver(healthkitmanager manager: healthkitmanager, fortype type: hksampletype?, withpredicateafterfetching predicate: nspredicate?) {     guard type != nil && manager.healthstore != nil else {          return     }      let query = hkobserverquery.init(sampletype: type!, predicate: nil) { (query, completionhandler, error) in          guard error == nil else {              simpleevent(name: "hkobserverquery failed", param: "type:\(type!.identifier), error: \(error!.localizeddescription)").fire()              return         }          let anchoredquery = hkanchoredobjectquery.init(type: type!, predicate: predicate, anchor: manager.gethealthdataanchor(fortype: type!), limit: hkobjectquerynolimit, resultshandler: { (query, results, deletedobjects, newanchor, error) in              guard error == nil else {                 simpleevent(name: "hkanchoredquery failed", param: "type:\(type!.identifier), error: \(error!.localizeddescription)").fire()                  return             }       \\ code saves hk data              completionhandler()         })          manager.healthstore!.execute(anchoredquery)     }      manager.healthstore!.execute(query)     manager.observerqueries.append(query) } 

the error caught on hkobserverquery failed event.

as mentioned permission request should not affect data reading here's code anyway:

func requestauthorizationtosharetypesandreadtypes(withsuccess successblock: @escaping () -> void, failure failureblock: @escaping () -> void) {      self.healthstore?.requestauthorization(toshare: writedatatypes as! set<hksampletype>, read: readdatatypes as! set<hkobjecttype>, completion: { (success, error) in          if !success || error != nil {             simpleevent.init(name: "hk-requestauthorization", param: error!.localizeddescription)             nslog("you didn't allow healthkit access these read/write data types. error was: %@.", error as! nserror);             failureblock()             return;         }          self.userhavebeenpromptwithpermissionsscreenatleastonce = true          self.enablebackgrounddelivery()          successblock()     }) } 

and here's part enable background delivery

func enablebackgrounddelivery() {     type in self.readdatatypes! {         if let type = type as? hksampletype {             self.enablebackgrounddelivery(forsampletype: type)         }     } }  func enablebackgrounddelivery(forsampletype type:hksampletype) {     self.healthstore?.enablebackgrounddelivery(for: type, frequency: hkupdatefrequency.immediate, withcompletion: { (success, error) in         if error != nil {             print(string.init(format: "hk-backgounderror: error:%@, user: %@, type: %@", error!.localizeddescription, nrmanager.shared().username, type.identifier))         }      }) } 

authorization required read data healthkit. if receiving error indicating authorization not determined, indicates user has not been prompted authorize app either read or write samples of type querying for.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -