16

How to show Alert on apple watch. Is there any alternate to show alerts in the Apple Watch because I checked and UIAlertView is not working on Apple Watch.

John
  • 8,468
  • 5
  • 36
  • 61
Iqbal Khan
  • 4,587
  • 8
  • 44
  • 83

5 Answers5

14

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.

pojo
  • 5,892
  • 9
  • 35
  • 47
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • plz explain in deep.where to put code ? what to do in controller to pass context? – Krutarth Patel Apr 17 '15 at 05:40
  • Completed the answer with some images. The ideia is basically define a new interface controller that is responsible to present an error and then simply present it. – Tiago Almeida Apr 17 '15 at 09:24
11

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 !!!

Harjot Singh
  • 6,767
  • 2
  • 38
  • 34
7

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 ]];
John
  • 8,468
  • 5
  • 36
  • 61
5

In watchOS 2

Objective-C

NSString *titleOfAlert = @"Something Happened Wrong";
NSString *messageOfAlert = @"Error Message Here";
[self.interfaceController presentAlertControllerWithTitle: titleOfAlert
                                                  message: messageOfAlert
                                           preferredStyle:
                                            WKAlertControllerStyleAlert
                                           actions:@[
                                              [WKAlertAction actionWithTitle: @"OK"
                                                             style: WKAlertActionStyleDefault
                                                             handler: ^{
                                                                //something after clicking OK
                                                             }
                                           ]];

Swift

let titleOfAlert = "Something Happened Wrong"
let messageOfAlert = "Error Message Here"
self.interfaceController.presentAlertControllerWithTitle(titleOfAlert, message: messageOfAlert, preferredStyle: .Alert, actions: [WKAlertAction(title: "OK", style: .Default){
    //something after clicking OK
}])

In watchOS 1

You should make a second interface controller, as Tiago says, then present the second one from the first one:

Objective-C

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

Swift

self.presentController(name: "ErrorInterfaceController", context:["title":"yourTitle" , "text":"yourText"])
Seyed Parsa Neshaei
  • 3,470
  • 18
  • 30
4

Another option is to put your alert UI in a group and show/hide it as necessary. Depending on your app's design this can work quite well. I do something similar for showing loading UI.

bgilham
  • 5,909
  • 1
  • 24
  • 39