ios - HealthKit Auth in Swift -
i've been building basic app collect fitness , other data health kit in swift, however, i'm having issue getting authorization occur
class healthmanager { let healthkitstore:hkhealthstore = hkhealthstore() func authorizehealthkit(completion: ((success:bool, error:nserror!) -> void)!) { // 1. set types want read hk store let healthkittypestoread = set(arrayliteral:[ hkobjecttype.characteristictypeforidentifier(hkcharacteristictypeidentifierdateofbirth), hkobjecttype.characteristictypeforidentifier(hkcharacteristictypeidentifierbloodtype), hkobjecttype.characteristictypeforidentifier(hkcharacteristictypeidentifierbiologicalsex), hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifierbodymass), hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifierheight), hkobjecttype.workouttype() ]) // 2. set types want write hk store let healthkittypestowrite = set(arrayliteral:[ hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifierbodymassindex), hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifieractiveenergyburned), hkobjecttype.quantitytypeforidentifier(hkquantitytypeidentifierdistancewalkingrunning), hkquantitytype.workouttype() ]) // 3. if store not available (for instance, ipad) return error , don't go on. if hkhealthstore.ishealthdataavailable() { let error = nserror(domain: "me.kebabman.healthkit", code: 2, userinfo: [nslocalizeddescriptionkey:"healthkit not available in device"]) if( completion != nil ) { completion(success:false, error:error) } return; } // 4. request healthkit authorization healthkitstore.requestauthorizationtosharetypes(healthkittypestowrite, readtypes: healthkittypestoread) { (success, error) -> void in if( completion != nil ) { completion(success:success,error:error) } } } }
from class (my view controller) i'm calling authorisehealthkit func - no matter change i'm getting healthkit not available on device (using ios 8.3 simulator)
from reading through forums should working can't figure out why.
thanks
your if statement wrong
this section of code if hkhealthstore.ishealthdataavailable() { return error ... }
should
if !hkhealthstore.ishealthdataavailable() { return error ... }
or
if hkhealthstore.ishealthdataavailable() { request authorization ... } else { return error... }
Comments
Post a Comment