WatchKit Tutorial: How To Build Apple Watch App To Obtain Current Location

Apple Watchkit was first released on 18th November, 2014 along with the Xcode 6.2 beta. For now, there are three types of Apple watch experience you can build: Watchkit apps, actionable notifications, and Glances.

Today, companies are hurrying to build Apple watch app to get ahead of the pack and send signal to market as well as customers that the company is an innovator.

According to a report, Companies like MapMyFitness, Runtastic, and Nike have already arrived on the platform, but have not extended Apple watch support to more than 25% of their portfolios.

However, there are still many popular apps and games developed their Watch app to increase reliability for their users. And today, in this iOS Watchkit tutorial, we’ll learn how to build Apple watch app and obtain current location of device.

Let’s Get Started

Before creating this demo, You must be ready with the system having iOS 10.0 SDK and Xcode 8.0.

WatchOS 2.0 is the minimum iOS version supported by this demo.

Most used Watch app methods:

awakeWithContext: When initializing the page, awakeWithContext method will be called. This is the first method to be called, and no UI is displayed yet.awakeWithContext will be called on ALL PAGES as soon as the watchOS app launches.

willActivate: Β This method is called when watch view controller is about to be visible to user,
You should update label values, actions and anything else related to view elements.

here.willActivate will be called on ALL PAGES as soon as the watchOS app launches but this method will be called again as soon as you view the controller.

πŸ‘‰ Now, Open your Xcode and click on “Create New Xcode Project”.

πŸ‘‰ Select “Single View Application” and click on next.

Create New Xcode Project

πŸ‘‰ Give you product Name and select language Swift in below screen press next button.

set a product Name

πŸ‘‰ Now you base Project is ready Next step is to click on File-> New> Target as shown in the below screen.

set the target

πŸ‘‰ This will open template choose screen where you need to select WatchOS -> WatchKit App-> Next

choose template screen

πŸ‘‰ Next, it will open one popup and ask you to activate the watch scheme. Select the Activate option.

activate the watch scheme

πŸ‘‰ you have successfully created Watch kit extension this will create a group of file in your
File inspector as below Image.

Watch kit extension

πŸ‘‰ Here, the InterfaceController.swift file is similar to ViewController file in ios application and ExtentionDelegate.swift file is similar to AppDelegate.swift in ios.

πŸ‘‰ Now click on Interface. Storyboard from File Inspector it will open Storyboard like below screen.

open Storyboard

πŸ‘‰ Now drag a map from the object library to your watch storyboard, and add one button give name β€œGet Current Location” and two other Label one for latitude and other for longitude as shown in the below screenshot.

Get Current location

πŸ‘‰ Now open Assistance editor and select map then click ctrl key and drag into InterfaceController file make an Outlet of map.

Similarly for both label and action for Button.

Here, the design work is finished in Storyboard.

Add Following keys into you Info.plist file of you iphone application and as well as under CurrentLocation Extension info.plist file :

Privacy – Location When In Use Usage Description : I need location

Privacy – Location Always Usage Description : I need location

πŸ‘‰ Now run the app. This will show your design on watch simulator but it won’t show location pin while click on button.

location pin hidden on simulator screen

πŸ‘‰ Now, next step to get user current location.

πŸ‘‰ Add WatchConnector.swift file in your project and select watchDemo as well as CurrentLocation Extension as show in following screenshot (link of WatchConnecter file).

select watchDemo and Current Location Extension

πŸ‘‰ Now you need to add coreLocation framework in your project.

Select WatchDemo from file inspector->Targets->BuildPhase->Link Binary With Libraries->clikc on (+) add CoreLocation.framework

πŸ‘‰ Now got to your AppDelegate.swift file import following framework.

import CoreLocation
import MapKit

πŸ‘‰ Then create variable for location like this:

let locationManager:CLLocationManager = CLLocationManager()
var currentLocation = CLLocation()

πŸ‘‰ Add following code in your AppDelegate method.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
return true

πŸ‘‰ Inherit CLLocationManagerDelegate in AppDelegate.swift file and add it’s delegate method like below.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count == 0
self.currentLocation = locations.first!
let message = ["lat":self.currentLocation.coordinate.latitude,"long":self.currentLocation.coordinate.longitude]
WatchConnector.shared.sendMessage(message, withIdentifier: "sendCurrentLocation") { (error) in
print("error in send message to watch\(error.localizedDescription)")

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Fail to load location")

πŸ‘‰ Now go to your InterfaceController.swift file import coreLocation framework.

import CoreLocation

πŸ‘‰ create variable like below.

var currentLocation = CLLocation()
var lat:Double = 0.0
var long:Double = 0.0

πŸ‘‰ Add following code in your override func willActivate() method.

override func willActivate() {
// This method is called when watch view controller is about to be visible to user
WatchConnector.shared.listenToMessageBlock({ (message) in = message["lat"] as! Double
self.long = message["long"] as! Double

self.currentLocation = CLLocation(latitude: as! CLLocationDegrees, longitude: self.long as! CLLocationDegrees)

let mylocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(self.currentLocation.coordinate.latitude, self.currentLocation.coordinate.longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegion(center: mylocation, span: span)
self.mapView.addAnnotation(mylocation, with: .red)
}, withIdentifier: "sendCurrentLocation")

πŸ‘‰ Now, next step is to get latitude, longitude when click on get current location button.

@IBAction func btnGetCurrentLocationClicked() {

πŸ‘‰ Now run the app in watch it will run your iPhone app in iPhone simulator as well
πŸ‘‰ After run app on watch open WatchDemo on iPhone Simulator and see there is pin shown on your watch app.

open WatchDemo on iPhone Simulator

And done!

This iOS Watchkit tutorial has covered the basics of integrating Maps into Watch app and use it to get the current user location. But, the Apple Watch kit isn’t limited to this, you can also send actionable notifications or add Glances by developing a Watch app for your iPhone application.

Now, if you’re not aware of how to build Apple watch app, simply get in touch with a well-established iOS app development company and having experience of creating iOS watch app to get your own Apple Watch app developed.

In case, if you still have any query or confusion regarding the cost of making your own WatchKit, how long does it take to make an app, then you can get in touch with us through our contact us form. One of our sales representatives would like to assist you. The consultation is absolutely free of cost.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.