19

In my WatchKit app, when the user first launches it, I would like to present to them a helpful message alert that tells them how the app works, e.g. what the buttons do, etc.

Is there something similar to UIAlertView / UIAlertController that I can call in a WatchKit app? I couldn't find an answer on this topic which may very well mean that it's not possible.

Pang
  • 9,564
  • 146
  • 81
  • 122
Sam B
  • 27,273
  • 15
  • 84
  • 121

7 Answers7

58

(New in watchOS 2.0)

 WKAlertAction *act = [WKAlertAction actionWithTitle:@"OK" style:WKAlertActionStyleCancel handler:^(void){
        NSLog(@"ALERT YES ");
    }];

 NSArray *testing = @[act];

[self presentAlertControllerWithTitle:@"Voila" message:@"This is Watch OS 2 !" preferredStyle:WKAlertControllerStyleAlert actions:testing];

SWIFT

func showPopup(){

    let h0 = { print("ok")}

    let action1 = WKAlertAction(title: "Approve", style: .default, handler:h0)
    let action2 = WKAlertAction(title: "Decline", style: .destructive) {}
    let action3 = WKAlertAction(title: "Cancel", style: .cancel) {}

    presentAlert(withTitle: "Voila", message: "", preferredStyle: .actionSheet, actions: [action1,action2,action3])

}
Idee
  • 1,887
  • 21
  • 31
Ramanan R R
  • 896
  • 8
  • 18
  • works perfectly for me, even with one button. just remember to also adjust the actions array provided to `presentAlertControllerWithTitle` – oelna Jul 26 '16 at 09:12
12

i will add the swift4 result that work for me while using

WKAlertAction

watchOS 4.0

Swift 4

        let action1 = WKAlertAction.init(title: "Cancel", style:.cancel) {
            print("cancel action")
        }
        
        let action2 = WKAlertAction.init(title: "default", style:.default) {
            print("default action")
        }
        
        let action3 = WKAlertAction.init(title: "destructive", style:.destructive) {
            print("destructive action")
        }
        
        presentAlert(withTitle: "Alert Title", message: "message is here", preferredStyle:.actionSheet, actions: [action1,action2,action3])
Community
  • 1
  • 1
Amr Angry
  • 3,711
  • 1
  • 45
  • 37
3

Yes, after upgrading to watchOS 2, you can present an alert view using presentAlertController of WKInterfaceController.

See the official documentation here.

Pang
  • 9,564
  • 146
  • 81
  • 122
Manish Gumbal
  • 283
  • 3
  • 13
3
   let h0 = { print("h0 action")}
   let h1 = { print("h1 action")}

   let action1 = WKAlertAction(title: "h0 action", style: .default, handler:h0)
   let action2 = WKAlertAction(title: "h1 action", style: .default, handler:h0)

   self.presentAlert(withTitle: "Title", message: "a message", preferredStyle: .actionSheet, actions: [action1, action2])

Code in Swift 3

marcomoreira92
  • 369
  • 3
  • 9
2

There is no such class for alerts. However you can modally present "WKInterfaceController" with the information in "WKInterfaceLabel" and one "WKInterfaceButton".

gagarwal
  • 4,224
  • 2
  • 20
  • 27
2

Sadly, you can not do this. But you can of course have a modal page-based hierarchy with screenshots of how the app is working if it is the first time that the app is launched. I am doing so in my app! :)

Ashraf Tawfeeq
  • 3,036
  • 1
  • 20
  • 34
  • Can you add code and screenshot of how you are doing this modally in storyboard? There is only one main view controller in watchkit and how can you redirect a user to some other place and then never show that page again? I don't understand. – Sam B Mar 22 '15 at 01:40
1

If I could make one more suggestion: Create a separate group for your "alert" in your initial interface controller and show/hide it as needed.

bgilham
  • 5,909
  • 1
  • 24
  • 39
  • Not sure that's possible, given you can't overlap elements so you'll have to hide the main UI and it'll be very hacky (abrupt / no animation etc). Instead, showing a modal controller may be a better option if that's possible. – strangetimes Mar 20 '15 at 12:23