
Contents
Creating Your First Apple Watch Application
Pham Van Hoang, hoangk55cd@gmail.com, is the author of this article and he contributes to RobustTechHouse Blog
Introduction
At WWDC 2015, watchOS2 for apple watch was made public and it came with a lot of new features and capabilities, including a new ability to write native apps that run right on the watch.
In this article we will show you how to create your first watchOS application named “Stopwatch” using Objective-C. Hooray!
Let’s Get Started
1. Go to Xcode and create new application. Select watchOS -> Application
2. Name the app as “Stopwatch”. In this project we don’t need notification so just deselect the option “Include Notification Scene”
3. Create user interface.
In this Stopwatch app, we will have a label to display our time; a start button to trigger (or stop) the timing; a lap button that allows us to record our last lap time; and a label to display it. To do this, head over to the “Interface.storyboard” in your Watch Kit App and add attributes as below.
“The projects you create for Apple Watch consist of two separate bundles: a Watch app and a WatchKit extension. The Watch app bundle contains the storyboards and resource files associated with all of your app’s user interfaces. The WatchKit extension bundle contains the extension delegate and the controllers for managing those interfaces and for responding to user interactions. While these bundles are distributed inside an iOS app, they are then installed on the user’s Apple Watch and run locally on the watch” – Apple docs
To have buttons that sit side-by-side of each other, we need to add them into a Group and configure them as below.
4. Outlets and Actions
The next step we have to take is to link these elements within our app. Go to “InterfaceController.h” class and create outlets and actions as below:
#import <WatchKit/WatchKit.h> #import <Foundation/Foundation.h> @interface InterfaceController : WKInterfaceController { NSTimer *timer; // we use timer to call function update UI every seconds NSDate *startTime; // time that we press start stop watch NSString *currentTimeString; // save current time BOOL isStarted; // check whether the timer is currently running } @property (strong, nonatomic) IBOutlet WKInterfaceLabel *currentTimeLabel; @property (strong, nonatomic) IBOutlet WKInterfaceLabel *lapTimeLabel; @property (strong, nonatomic) IBOutlet WKInterfaceButton *startButton; @property (strong, nonatomic) IBOutlet WKInterfaceButton *lapButton; - (IBAction)lapTimeButtonPress; - (IBAction)startTimeButtonPress; @end
5. Implement the function.
Now head over to file “InterfaceController.m” to implement codes that actually update time in our watch application.
First we want to add function that update timer every time we call and reset when user press button stop.
- (void) updateTimer { NSDate *dateNow = [NSDate date]; NSTimeInterval interval = [dateNow timeIntervalSinceDate:startTime]; NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:interval]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"HH:mm:ss"]; [dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; currentTimeString = [dateFormat stringFromDate:timeDate]; // save current time in stop watch [self.currentTimeLabel setText:currentTimeString]; // update UI } - (void) resetTimer{ // reset timer when user click stop [timer invalidate]; [self.currentTimeLabel setText:@"00:00:00"]; [self.lapTimeLabel setHidden:YES]; }
Next, we need to trigger “update timer” function which update every second using NSTimer. We also need to check whether the timer is currently running or not, so we can disable/enable “lap” button and change the button start title to “stop/start”.
- (IBAction)startTimeButtonPress { isStarted = !isStarted; if (isStarted == YES) { [self.lapButton setEnabled:YES]; [self.startButton setTitle:@"Stop"]; startTime = [NSDate date]; timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; } else { [self.lapButton setEnabled:NO]; [self.startButton setTitle:@"Start"]; [self resetTimer]; } }
Lastly, we just need to update “lap” label each time user presses on the “lap” button.
- (IBAction)lapTimeButtonPress { [self.lapTimeLabel setHidden:NO]; self.lapTimeLabel.text = currentTimeString; }
We have now completed a basic stopwatch application. Let’s run the project and enjoy your new apple watch app.
You can find the full example here. Hope you will find this post useful. If you have any questions, please leave the comments below. Thanks for reading.
Reference
Apple App Programming Guide for watchOS
Brought to you by the RobustTechHouse team (Singapore based app development company). If you like our articles, please also check out our Facebook page.
I dugg some of you post as I thought they were very beneficial invaluable
For professional resume writing, head to Resume101 and examine the prices and packages they offer.
Thanks for the nice post
Je ne suis pas vraiment un lecteur Internet pour être honnête mais vos blogs vraiment sympa, continue comme ça ! 에볼루션카지노 Je vais aller de l’avant et ajouter votre site à vos favoris pour revenir à l’avenir. advgamble.com
The information about the watch model you pointed to is very interesting. I really like the Apple watch model. It is always my ideal choice and many others!
coloring pages
Thank you very much for the helpful post.
Of course, your article is good enough, casino online but I thought it would be much better to see professional photos and videos together. There are articles and photos on these topics on my homepage, so please visit and share your opinions.
I was looking for another article by chance and found your article casinosite I am writing on this topic, so I think it will help a lot. I leave my blog address below. Please visit once.
The information is very special, I will have to follow you.
It’s really great. Thank you for providing a quality article. There is something you might be interested in. Do you know slotsite ? If you have more questions, please come to my site and check it out!
It’s absolutely wonderful. I appreciate you publishing such a good article and that online assignment is very professional and effective platform and that do your online course they have provide a very expert services for all students.
It’s absolutely wonderful. I appreciate you publishing such a good article and that the online assignment is very professional and effective platform and that do your online course they have provide a very expert services for all students.
It’s absolutely wonderful. I appreciate you publishing such a good article and that the online assignment is very professional and effective platform and that do your online course they have provide a very expert services for all students.
I enjoy using this digital wristwatch. It’s amazing how cool it appears to be.
By coincidence, I came upon your article while seeking for another one. Since I am writing about it, I believe it will be really beneficial. I’ve listed my blog’s URL below. Do come back once.
Since I am writing about it, I believe it will be really beneficial. I’ve listed my blog’s URL below. Do come back once.
I’ve listed my blog’s URL below. Do come back once.
Do come back once.
YOur sharing is really useful for me. That’s great!
awesome blog!
Such an amazing article I must say that you have an amazing writing skills and hope you will continue your good work at the same pace. thank you once again for sharing such impressive work with us.
I appreciate you sharing this useful information. Your articles are a fantastic resource for learning about the creation of
apple application with mobile app development company in Dubai . such a fantastic blog. It’s a good platform for connecting with your audiences as well.
Creating a new is not a piece of cake it requires a lot of creativity and expertise and for the app I would always recommend TekRevol as they have the best and most professional app developers.
Hello ! I am the one who writes posts on these topics casinocommunity I would like to write an article based on your article. When can I ask for a review?