Search code examples
objective-cwatchkitapple-watchwatchos-2

How to show Alert on Apple Watch


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.


Solution

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