0

I'm creating an Safari App Extension that communicates with native code in swift.

I'm sending a message to swift, and I want the native code to ask if user accept an action, like delete something.

I found this code:

`

func dialogOKCancel(question: String, text: String) -> Bool {
        let alert = NSAlert()
        var image = NSImage(named:NSImage.Name(rawValue: "b.png"))
        alert.icon = image;
        alert.messageText = question
        alert.informativeText = text
        alert.alertStyle = .informational
        alert.addButton(withTitle: "Permitir")
        alert.addButton(withTitle: "Negar")
        return alert.runModal() == .alertFirstButtonReturn
    }

` But this code only opens an alert if it is inside a view class, and I want it to open in any class that wants to ask something, is that possible? I saw many implementations that uses another libs for IOS, but I want to do this in Mac OSX, thanks!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • In iOS, you can use `view` of rootViewController to do it and in MacOSX, i think we have something like rootViewController too ;) – trungduc Nov 01 '17 at 17:10
  • 2
    The code you showed has no requirement that "it is inside a view class", whatever you mean by that. What makes you think it does? It should work fine. It should only be executed on the main thread, though. Also, are Safari extensions run in separate processes that are sandboxed? That might be interfering. – Ken Thomases Nov 01 '17 at 17:20
  • The code is working, when i call it from a view controller it works, like SomeViewController.alert(). But if i declare it in any other class that isn't a controller, nothing happens. BUT if i call SomeViewController.alert() in this other class it works, but only if the view is opened. Looks like it depends from soething. – David Grechi Döll Nov 01 '17 at 21:13

0 Answers0