Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 906
Iqbal Khan Avatar asked Apr 16 '15 10:04

Iqbal Khan


People also ask

Why is my Apple Watch not showing alerts?

If you don't see notifications on your Apple Watch To check your connection, swipe up on the watch face to open Control Center. If your devices are disconnected, you might see the red iPhone icon , the red X icon , or the Wi-Fi icon . Try to connect your iPhone and Apple Watch.

Why is my Apple Watch not notifying me of text messages?

Turn off wrist detection. If you're wearing your Apple Watch but it doesn't detect you are, switch off wrist detection to circumvent the sensor. Switch off Cover to Mute. Like the Apple Watch's Do Not Disturb mode, Cover to Mute silences notifications, including text messages.


4 Answers

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.

like image 130
Tiago Almeida Avatar answered Sep 18 '22 06:09

Tiago Almeida


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

like image 36
Harjot Singh Avatar answered Sep 18 '22 06:09

Harjot Singh


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 ]];
like image 30
vomako Avatar answered Sep 22 '22 06:09

vomako


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"])
like image 25
Seyyed Parsa Neshaei Avatar answered Sep 18 '22 06:09

Seyyed Parsa Neshaei