Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NG_Matthieu · Apr 09, 2015 at 06:27 AM · unity 5ioswatch

How to update apple watch content (from extension) with data from Unity project

So a few days down the line from first question:

http://answers.unity3d.com/questions/938476/empty-project-empty-apple-watch-does-not-compile-i.html#comment-941306

Not sure if it fits here as it's mostly Xcode stuff, but still closely related to Unity to apple watch so writing here.

So progress on project is I have an Unity iOS app that has a watch kit added to it. But what is the use of adding watch to a project if you can't display anything properly?!

So for this I will share some code and write what it does and maybe someone can help me with the final step I want... Feel free to use the code, once I get this to work properly I might even make my first short tutorial about this as I spent/d long time on something that should have been fairly easy but somehow Apple managed to not do so...

The first thing I did is create a very simple script within Unity:

 public class UpdateLabel : MonoBehaviour {
 
     private int i = 0;
     public Text _label = null;
 
     void Start()
     {
         //Debug.Log ("UpdateLabel started");
     }
 
     // Update is called once per frame
     void Update () {
         i++;
         _label.text = i.ToString ();
         SetAppleWatchData ("i",i.ToString());
     }
 
     #if UNITY_IOS && !UNITY_EDITOR
     [DllImport("__Internal")]
     private static extern void SaveStringForWatch(string KeyCode, string value);
     #endif
     
     public void SetAppleWatchData(string key, string value)
     {
         #if UNITY_IOS && !UNITY_EDITOR
         SaveStringForWatch(key,value);
         #endif
     }
 }

The idea is to save a key value pair within Apple's new system of groups to send the data to the Watch. For this I needed to write a small .mm file also as shown below that saves in the proper group:

 extern "C" {
     
     void SaveStringForWatch(const char* key, const char* value) {
         
         NSString *_key = [[NSString alloc] initWithCString:key encoding:NSASCIIStringEncoding];
         NSString *_value = [[NSString alloc] initWithCString:value encoding:NSASCIIStringEncoding];
         ::printf("still printing something\n");
         
         NSUserDefaults *appleWatch = [[NSUserDefaults alloc] initWithSuiteName:@"group.watchtest"];
         [appleWatch setObject: _value forKey:_key];
         [appleWatch synchronize];
         
         NSString* _test = [appleWatch objectForKey: _key];
         
         
         
         //::printf(key+" " + value + "\n");
         NSLog(@"%@",[appleWatch dictionaryRepresentation]);
         //NSLog(@"%@",_value);
     }
 }


this took me some time to get to work properly, lot of logging and googling later I got this result that works great and is very simple... After this I moved to my Apple watch itself, before going all big and mighty was making something simple. I will get the information from the group and set it in a label.

 class InterfaceController: WKInterfaceController {
 
     @IBOutlet weak var label: WKInterfaceLabel!
     @IBOutlet weak var _timer: WKInterfaceTimer!
     
     override func awakeWithContext(context: AnyObject?) {
         super.awakeWithContext(context)
         // Configure interface objects here.
     }
 
     override func willActivate() {
         // This method is called when watch view controller is about to be visible to user
         super.willActivate()
         
         //while(true)
         //{
             ReadAppleWatchDataAndUpdateLabels()
         //}
     }
 
     override func didDeactivate() {
         // This method is called when watch view controller is no longer visible
         super.didDeactivate()
     }
 
     @IBAction func OnButtonClicked() {
         ReadAppleWatchDataAndUpdateLabels()
     }
         
     func ReadAppleWatchDataAndUpdateLabels()
     {
         let sharedDefault = NSUserDefaults(suiteName: "group.watchtest")
         if(sharedDefault?.objectForKey("i") != nil)
         {
             let sharedText = sharedDefault?.objectForKey("i") as String
             label.setText(sharedText)
         }
         else
         {
             var _text = sharedDefault?.dictionaryRepresentation()
             println(sharedDefault?.dictionaryRepresentation())
             
         }
     }
 }


from the commented part you can realise what step is missing and what I'm here to ask:

How can I have my watch kit extension poll itself to update the data on the watch kit app based on new information added to the group... Also updating itself based on information inside the group (passing timer and doing something when timer runs out, or during the timer is running,...)

I want to have a watch app a bit more then just sending data from my app and statically showing it, I want the app to feel more alive by updating itself...

I'm new to swift language and a full day of googling just gave me 0 ideas, I looked up with sprite kit, but watch kit doesn't allow that for the extension, trying to call itself with a 1 sec interval but failed miserably. Used NSTimer.scheduledTimerWithTimeInterval(Double(1), target: self, selector: Selector("ReadAppleWatchDataAndUpdateLabels"), userInfo: nil, repeats: true) for that

If anyone has an idea how to get watch kit extension to update itself based on new information received from Unity or based on timer would be much appreciated...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NG_Matthieu · Apr 10, 2015 at 08:02 PM

For those interested in the solution, took me time to find it and lot of looking around but:


 func delay(delay:Double, closure:()->()) {
     dispatch_after(
         dispatch_time(
             DISPATCH_TIME_NOW,
             Int64(delay * Double(NSEC_PER_SEC))
         ),
         dispatch_get_main_queue(), closure)
 }


 //recalls itself every minute
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(60 * Double(NSEC_PER_SEC))), nil){self.UpdateAppleWatchDataLabels()}


First function needs to be global and add the 2nd code part at end of your method and change end to the correct method name and you it will recall itself every minute (works great and not resource intensive as the NSTimer) got this from stack overflow but think it's a good thing to write it here also...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to create and save Prefabs on device. 1 Answer

How to identify each iOS device? 1 Answer

Reducing IOS size 2 Answers

Export Unity5.x project to Xcode ,Then add UIView to unity , what can I do ? 0 Answers

[Help!] Fighting with Receipt Validation (iOS) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges