How to show Alert on Apple Watch How to show Alert on Apple Watch objective-c objective-c

How to show Alert on Apple Watch


With watchOS2

With watchOS2 you can use the WKAlertAction method:

+ (instancetype nonnull)actionWithTitle:(NSString * nonnull)title                                 style:(WKAlertActionStyle)style                               handler:(WKAlertActionHandler nonnull)handler

With watchOS1

If you don't mind losing the feature of an UIAlertView of seeing the content behind, you can:

1 - Create an ErrorInterfaceController (with or without an ok button)

enter image description here

2 - Set the identifier to "ErrorInterfaceController"

enter image description here

3 - Present that error with:

[self presentControllerWithName:@"ErrorInterfaceController"                         context:@{@"title" : @"yourTitle",                                  @"text"  : @"yourText"}];

4 - In your ErrorInterfaceController.m you can set your title and text with the context.

Note that your ErrorInterfaceController can have a title that is empty and the ok button can dismiss it or you can leave the way it is with a default "Done".

This is the simplest solution to present a message.

If you want something more complex you need to remember that WatchKit doesn't have a z-index and you can't add elements dynamically by code. Therefore, you need to have a solution that uses UIImages rendered in your app extension and sending them to the WatchKit.


Update for Swift 3.0 - In watchOS 3.0

let action = WKAlertAction(title: "Decline", style: WKAlertActionStyle.default) {        print("Ok")    }    presentAlert(withTitle: "Message", message: "Please select value. Swipe right to change it.", preferredStyle: WKAlertControllerStyle.alert, actions:[action])

Hope it helps !!!


For watchOS 2, here is an example:

WKAlertAction *action =            [WKAlertAction actionWithTitle:@"OK"                                     style:WKAlertActionStyleDefault                                   handler:^{                                       // do something after OK is clicked                                   }];NSString *title = @"Oops!";NSString *message = @"Here comes the error message";[self.interfaceController            presentAlertControllerWithTitle:title                                    message:message                             preferredStyle:WKAlertControllerStyleAlert                                    actions:@[ action ]];