Creating Your First Apple Watch Application

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading...

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

create first apple watch app_01

2.    Name the app as “Stopwatch”. In this project we don’t need notification so just deselect the option “Include Notification Scene

 create first apple watch app_02

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.

 create first apple watch app_03

“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.

create first apple watch app_04

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

create first apple watch app_05

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.

create first apple watch app_06

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.

Recommended Posts
Showing 24 comments
  • fallout 76 hacks
    Reply

    I dugg some of you post as I thought they were very beneficial invaluable

  • Resume101
    Reply

    For professional resume writing, head to Resume101 and examine the prices and packages they offer.

  • Reply

    Thanks for the nice post

  • 에볼루션카지노
    Reply

    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

  • among us
    Reply

    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

  • word hurdle
    Reply

    Thank you very much for the helpful post.

  • casino online
    Reply

    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.

  • casinosite
    Reply

    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.

  • cuphead
    Reply

    The information is very special, I will have to follow you.

  • slotsite
    Reply

    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!

  • smithlogan
    Reply

    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.

  • smithlogan
    Reply

    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.

  • smithlogan
    Reply

    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.

  • flagle
    Reply

    I enjoy using this digital wristwatch. It’s amazing how cool it appears to be.

  • stumble guys
    Reply

    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.

  • stumble guys
    Reply

    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.

  • stumble guys
    Reply

    I’ve listed my blog’s URL below. Do come back once.

  • stumble guys
    Reply

    Do come back once.

  • snake game
    Reply

    YOur sharing is really useful for me. That’s great!

  • backrooms game
    Reply

    awesome blog!

  • Reply

    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.

  • Jolly
    Reply

    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.

  • app development company
    Reply

    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.

  • casinocommunity
    Reply

    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?

Leave a Comment

Contact Us

We look forward to your messages. Please drop us a note for any enquiries and we'll get back to you, asap.

Not readable? Change text. captcha txt
Summary of Recent Singapore PDPA Decisionslaptops and phone

SCAM ALERT

Do not send any money to the scammers

RobustTechHouse would like to alert members of the public that there are persons impersonating RobustTechHouse to offer jobs.
RobustTechHouse only recruits from legitimate job portals and does not require any payment to secure a job.

Members of the public contacted by the scammers may wish to:
(a) Make a police report, particularly if you have suffered any monetary loss.
(b) Report the scammers' contact numbers to ScamShield https://www.scamshield.org.sg/report