Add data to healthkit from Apple Watch and iPhone











up vote
1
down vote

favorite












I'm working on an water-app at the moment that can send information to healtkit from the iPhone and the Apple Watch. I have successful setup the healtkit authorization and i can send data to healtkit.



When i'm sending data from the Apple Watch to healtkit it works fine, al my data entries are vissible in the graph. The strange thing is when i add data from my iPhone that the graph will be overwritten with the iPhone data. The watch entries are still in "show data"



When then i'm adding from my Apple Watch it won't update the graph but the entries are also in "show data".



I'm using a sharedfiel (HealtKitManager.swift) file for the healthkit authorization and the "AddWater" function.



import Foundation
import HealthKit

class HealtKitManager: NSObject {

static let sharedInstance = HealtKitManager()

private override init() {}

let healthStore = HKHealthStore()

func authorizeHealthKit(_ completion: @escaping((_ success: Bool, _ error: Error?) -> Void)){

let waterType = HKObjectType.quantityType(forIdentifier: .dietaryWater)!
let allTypes = Set([waterType])

healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
print("Authorization (success)")
completion(success, error)
}
}

func addWater(mL : Double){

let quantityType = HKQuantityType.quantityType(forIdentifier: .dietaryWater)
let quantityUnit = HKUnit(from: "mL")
let quantityAmount = HKQuantity(unit: quantityUnit, doubleValue: mL)

let now = Date()

let sample = HKQuantitySample(type: quantityType!, quantity: quantityAmount, start: now, end: now)

let correlationType = HKObjectType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.food)

let waterCorrelationForWaterAmount = HKCorrelation(type: correlationType!, start: now, end: now, objects: [sample])

self.healthStore.save(waterCorrelationForWaterAmount, withCompletion: { (succes, error) in

if (error != nil) {
NSLog("error")
}
})

}

}


in my ViewController and my InterfaceController i've connected buttons to write a "default" value to healtkit:



@IBAction func addGlassAction(_ sender: Any) {

healtKitManager.addWater(mL: 100)

}


and



@IBAction func add50ml() {
healtKitManager.addWater(mL: 50)
}


Why is the iPhone app "the boss"?



Is there someone who can help me in te right direction?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm working on an water-app at the moment that can send information to healtkit from the iPhone and the Apple Watch. I have successful setup the healtkit authorization and i can send data to healtkit.



    When i'm sending data from the Apple Watch to healtkit it works fine, al my data entries are vissible in the graph. The strange thing is when i add data from my iPhone that the graph will be overwritten with the iPhone data. The watch entries are still in "show data"



    When then i'm adding from my Apple Watch it won't update the graph but the entries are also in "show data".



    I'm using a sharedfiel (HealtKitManager.swift) file for the healthkit authorization and the "AddWater" function.



    import Foundation
    import HealthKit

    class HealtKitManager: NSObject {

    static let sharedInstance = HealtKitManager()

    private override init() {}

    let healthStore = HKHealthStore()

    func authorizeHealthKit(_ completion: @escaping((_ success: Bool, _ error: Error?) -> Void)){

    let waterType = HKObjectType.quantityType(forIdentifier: .dietaryWater)!
    let allTypes = Set([waterType])

    healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
    print("Authorization (success)")
    completion(success, error)
    }
    }

    func addWater(mL : Double){

    let quantityType = HKQuantityType.quantityType(forIdentifier: .dietaryWater)
    let quantityUnit = HKUnit(from: "mL")
    let quantityAmount = HKQuantity(unit: quantityUnit, doubleValue: mL)

    let now = Date()

    let sample = HKQuantitySample(type: quantityType!, quantity: quantityAmount, start: now, end: now)

    let correlationType = HKObjectType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.food)

    let waterCorrelationForWaterAmount = HKCorrelation(type: correlationType!, start: now, end: now, objects: [sample])

    self.healthStore.save(waterCorrelationForWaterAmount, withCompletion: { (succes, error) in

    if (error != nil) {
    NSLog("error")
    }
    })

    }

    }


    in my ViewController and my InterfaceController i've connected buttons to write a "default" value to healtkit:



    @IBAction func addGlassAction(_ sender: Any) {

    healtKitManager.addWater(mL: 100)

    }


    and



    @IBAction func add50ml() {
    healtKitManager.addWater(mL: 50)
    }


    Why is the iPhone app "the boss"?



    Is there someone who can help me in te right direction?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm working on an water-app at the moment that can send information to healtkit from the iPhone and the Apple Watch. I have successful setup the healtkit authorization and i can send data to healtkit.



      When i'm sending data from the Apple Watch to healtkit it works fine, al my data entries are vissible in the graph. The strange thing is when i add data from my iPhone that the graph will be overwritten with the iPhone data. The watch entries are still in "show data"



      When then i'm adding from my Apple Watch it won't update the graph but the entries are also in "show data".



      I'm using a sharedfiel (HealtKitManager.swift) file for the healthkit authorization and the "AddWater" function.



      import Foundation
      import HealthKit

      class HealtKitManager: NSObject {

      static let sharedInstance = HealtKitManager()

      private override init() {}

      let healthStore = HKHealthStore()

      func authorizeHealthKit(_ completion: @escaping((_ success: Bool, _ error: Error?) -> Void)){

      let waterType = HKObjectType.quantityType(forIdentifier: .dietaryWater)!
      let allTypes = Set([waterType])

      healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
      print("Authorization (success)")
      completion(success, error)
      }
      }

      func addWater(mL : Double){

      let quantityType = HKQuantityType.quantityType(forIdentifier: .dietaryWater)
      let quantityUnit = HKUnit(from: "mL")
      let quantityAmount = HKQuantity(unit: quantityUnit, doubleValue: mL)

      let now = Date()

      let sample = HKQuantitySample(type: quantityType!, quantity: quantityAmount, start: now, end: now)

      let correlationType = HKObjectType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.food)

      let waterCorrelationForWaterAmount = HKCorrelation(type: correlationType!, start: now, end: now, objects: [sample])

      self.healthStore.save(waterCorrelationForWaterAmount, withCompletion: { (succes, error) in

      if (error != nil) {
      NSLog("error")
      }
      })

      }

      }


      in my ViewController and my InterfaceController i've connected buttons to write a "default" value to healtkit:



      @IBAction func addGlassAction(_ sender: Any) {

      healtKitManager.addWater(mL: 100)

      }


      and



      @IBAction func add50ml() {
      healtKitManager.addWater(mL: 50)
      }


      Why is the iPhone app "the boss"?



      Is there someone who can help me in te right direction?










      share|improve this question















      I'm working on an water-app at the moment that can send information to healtkit from the iPhone and the Apple Watch. I have successful setup the healtkit authorization and i can send data to healtkit.



      When i'm sending data from the Apple Watch to healtkit it works fine, al my data entries are vissible in the graph. The strange thing is when i add data from my iPhone that the graph will be overwritten with the iPhone data. The watch entries are still in "show data"



      When then i'm adding from my Apple Watch it won't update the graph but the entries are also in "show data".



      I'm using a sharedfiel (HealtKitManager.swift) file for the healthkit authorization and the "AddWater" function.



      import Foundation
      import HealthKit

      class HealtKitManager: NSObject {

      static let sharedInstance = HealtKitManager()

      private override init() {}

      let healthStore = HKHealthStore()

      func authorizeHealthKit(_ completion: @escaping((_ success: Bool, _ error: Error?) -> Void)){

      let waterType = HKObjectType.quantityType(forIdentifier: .dietaryWater)!
      let allTypes = Set([waterType])

      healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
      print("Authorization (success)")
      completion(success, error)
      }
      }

      func addWater(mL : Double){

      let quantityType = HKQuantityType.quantityType(forIdentifier: .dietaryWater)
      let quantityUnit = HKUnit(from: "mL")
      let quantityAmount = HKQuantity(unit: quantityUnit, doubleValue: mL)

      let now = Date()

      let sample = HKQuantitySample(type: quantityType!, quantity: quantityAmount, start: now, end: now)

      let correlationType = HKObjectType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.food)

      let waterCorrelationForWaterAmount = HKCorrelation(type: correlationType!, start: now, end: now, objects: [sample])

      self.healthStore.save(waterCorrelationForWaterAmount, withCompletion: { (succes, error) in

      if (error != nil) {
      NSLog("error")
      }
      })

      }

      }


      in my ViewController and my InterfaceController i've connected buttons to write a "default" value to healtkit:



      @IBAction func addGlassAction(_ sender: Any) {

      healtKitManager.addWater(mL: 100)

      }


      and



      @IBAction func add50ml() {
      healtKitManager.addWater(mL: 50)
      }


      Why is the iPhone app "the boss"?



      Is there someone who can help me in te right direction?







      ios swift apple-watch health-kit






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 14:42









      Dávid Pásztor

      20.1k72547




      20.1k72547










      asked Nov 12 at 20:16









      Moi

      265




      265





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269459%2fadd-data-to-healthkit-from-apple-watch-and-iphone%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269459%2fadd-data-to-healthkit-from-apple-watch-and-iphone%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word