7 Replies
      Latest reply: Dec 11, 2016 3:31 PM by eskimo RSS
      WatchDev2015 Level 1 Level 1 (0 points)

        Hello all:

         

        I developed an app for Apple Watch 1 that tracks tennis shot stats on the Watch, writes those stats to an FMDB database, and then the user used to be able to check those stats on the phone. That was in watch OS1, and the watch wrote to a DB and then the phone read from that same DB. With the changes in OS2/3 the code used to determine the paths to the DB, which runs on both the Watch and Phone and used to return the same value, now produces different paths. I changed the code to use a Shared Instance in the phone app for the DB path and file creation, but the Path is still different when the code is called from the watch vs. the phone.

         

        Can anyone offer any assistance and/or make a suggestion for sharing data? I need the watch app to track the data and then write it out to the DB on the phone, and then at some later point in time when the user runs the app on the phone, the data needs to be accessible.

         

        Thanks for any and all input.

        • Re: Save data from Watch to Phone?
          eskimo Apple Staff Apple Staff (6,480 points)

          In watchOS 2 and later your iPhone app and WatchKit extension are running on different bits of hardware (the iPhone app runs on the phone, the WatchKit extension runs on the watch).  There’s no longer any possibility of shared file system access between the two.  If you want to put data from the watch to the phone, you’ll need to use a networking API.  Most folk do this sort of thing using the Watch Connectivity framework but there are other options (for example, you could store the data in CloudKit and have both the watch and the phone access it from there).

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Save data from Watch to Phone?
              WatchDev2015 Level 1 Level 1 (0 points)

              Thank you for your answer! I understand what you are syaing, however the code I developed runs on the iPhone, and is only called from the Watchkit Extension. So I have a file in the iPhone app called WriteData that performs the write to the database, and i set it up as a Shared Instance:

               

              static let sharedInstance = WriteData()

               

              and then in the Watchkit Extension, there is a file that tracks the data on the Watch, and then to write the data I call

               

              WriteData.sharedInstance.updateDatabase()

               

              but for some reason when the updateDatabase() is called directly from the iPhone, it creates a different path than if it is called from the WatchKit extension, despite the code running on the iPhone in both cases...Strange.

                • Re: Save data from Watch to Phone?
                  eskimo Apple Staff Apple Staff (6,480 points)

                  for some reason when the updateDatabase() is called directly from the iPhone, it creates a different path than if it is called from the WatchKit extension, despite the code running on the iPhone in both cases

                  We’re talking watchOS 2 or later, right?  If so, the WatchKit extension is not running on the phone, it’s running on the watch.

                  To summarise:

                                                                                                                             

                  CodewatchOS 1watchOS 2…
                  iOS appphonephone
                  WatchKit extensionphonewatch
                  WatchKit appwatchwatch

                  Notice the change in the second row; the WatchKit extension has moved from the phone to the watch and thus no longer has the ability to share data directly with your iOS app.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: Save data from Watch to Phone?
                      WatchDev2015 Level 1 Level 1 (0 points)

                      Yes Watch Os2.The confusion is because the database code is running ONLY on the iPhone, not the Watch Extension App. So from the extension app I simply call the code on the Phone:

                       

                      I have a file in the iPhone app called WriteData that performs the write to the database, and i set it up as a Shared Instance:

                       

                      static let sharedInstance = WriteData()

                       

                      and then in the extension app i use the following line tp update data:

                       

                      WriteData.sharedInstance.updateDatabase()

                        • Re: Save data from Watch to Phone?
                          eskimo Apple Staff Apple Staff (6,480 points)

                          So from the extension app I simply call the code on the Phone …

                          How do you do that?  What API do you use to do the networking between the watch and the phone?  Watch Connectivity?  Or something else?

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"

                            • Re: Save data from Watch to Phone?
                              WatchDev2015 Level 1 Level 1 (0 points)

                              Hi:

                               

                              In the iPhone app, I have a file called "WriteData.swift", with Target Membership for both the iPhone App and watch Extension, and in that file I declare it as a static shared instance:


                              static let sharedInstance = WriteData()


                              then in the Watchkit Extension App, there is a file called "ScoreInterfaceController.swift", and from there I call: 

                               

                              WriteData.sharedInstance.updateDatabase()

                               

                              so when the updateDatabase() code is called from teh iPhone, it builds a path to teh DB file, but when it is called from teh file in the Extension app (but AFAIK the code itself still runs on teh iPhone), it builds a different path.

                               

                              I hope this is making sense.

                               

                              Thanks!

                               

                              John.

                                • Re: Save data from Watch to Phone?
                                  eskimo Apple Staff Apple Staff (6,480 points)

                                  … when it is called from teh file in the Extension app (but AFAIK the code itself still runs on teh iPhone) …

                                  This last bit is where you’re confused.  In watchOS 2 and later the WatchKit extension runs on the watch.

                                  Share and Enjoy

                                  Quinn “The Eskimo!”
                                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                  let myEmail = "eskimo" + "1" + "@apple.com"