Search code examples
iosobjective-ciphonearrayswatchkit

use array from iphone in watchkit


I have a simple app on my iPhone where I can enter some names and the app than picks a random name and displays an alertview.

All the names I enter are displayed in a table view and are storen in an array.

On the apple watch, I want to add a "play"-button, when it is tapped, I want to display a name.

Do I need to pick a name on the iphone and send the name to the apple watch, or do i need to send the array to the apple watch and pick a random name on the watch itself?

I don't have much experience in objective-c so any tutorials, or some kind of list would be nice!

Thanks in advance!


Solution

  • First off, to clarify: Your Apple Watch app technically runs on your iPhone, using your WatchKit Extension. No matter what you pick, it'll still be executed on the iPhone and then sent automatically to the Watch.

    Now, to answer your question: You can do either. You can use the openParentApplication method on WKInterfaceController to wake up the main iPhone app and either (1) get the entire array, or (2) find a random name, and send that back to the Extension.

    Or you could store the data in a Shared App Group, that way it'll be accessible to both your iPhone and Apple Watch App.

    I'd suggest the latter if you'll be sharing a lot of info between the two. If not, I'll probably get the Array from the iPhone using openParentApplication and find the random name on the WatchKit Extension. You have a limited amount of time to do what you want to do when opening the Parent App, not to mention that this could take a while, so if you'll be getting multiple names that could add a delay.