I am developing a safari app extension with swift in xcode.
In my SafariExtensionHandler.swift, I declared a func:
override func popoverViewController() -> SFSafariExtensionViewController {
return SafariExtensionViewController.shared
}
and in my SafariExtensionViewController.swift,
class SafariExtensionViewController: SFSafariExtensionViewController {
static let shared: SafariExtensionViewController = {
let shared = SafariExtensionViewController()
shared.preferredContentSize = NSSize(width:320, height:240)
return shared
}()
}
However, when I click the icon in my safari toolbar, nothing shows up and no errors.
Anyone helps? I am fresh to safari app extension and apple-family developing.
Damn I figured that out.
In info.plist, SFSafariToolbarItem dict, change the value of action key to Popover .
Related
I am trying to create a native package for flutter Swift/Kotlin, I built the UIViewController in a XCode project and moving the code to the flutter ios plugin therefore I added the MediaPickerController.swift file (as seen in the screenshot below).
In my SwiftMediaFilePickerPlugin.swift, I am trying to present the MediaPickerController but I am getting the "error: cannot find 'MediaPickerController' in scope"
The snippet of how I am presenting the MediaPickerController in SwiftMediaFilePickerPlugin.swift:-
let mediaPickerController = MediaPickerController(mediaType: mediaType, limit: limit)
UIViewController.topViewController().present(mediaPickerController, animated: true, completion: nil)
fileprivate extension UIViewController {
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
The code is working fine on a seperate swift project
Open example/ios/Runner.xcworkspace in Xcode and add additional files to Pods/Development Pods/.../ios/Classes.
Warning: macOS dev beginner here.
I have a menu bar app (with no dock). Most of the app's functionality is in the menu (and implementation is in AppDelegate), but I need a separate window that will open once I click one of the menu items.
I want to use SwiftUI, Swift 5, Xcode 11.3.
I haven't found an appropriate way to do this. Which files and similar need to be created? How to open this window programatically?
#objc func openPreferences() {
// open a new window here...
}
You have to create a window programatically. I have attached sample code of one of my apps:
private var windowController: NSWindowController?
fileprivate func createWindow()
{
let storyboard = NSStoryboard(name: "Main", bundle: nil)
self.windowController = storyboard.instantiateInitialController() as? NSWindowController
// This is example code to show how to customize the hosted view controller. You can pass additional arguments here (may an important global variables that is declared in the AppDelegate).
if let contentController = windowController?.contentViewController as? MyWindowViewController
{
// Do some assignments here
// contentController.variable = ....
// self.windowViewController = contentController // Maybe save for later use.
}
}
#objc fileprivate func open()
{
if self.windowViewController == nil
{
self.createWindow()
}
self.windowController?.showWindow(self)
NSApp.activate(ignoringOtherApps: true) // Bring window to front.
}
I have linked the open() function to a button call (hence the #objc keyword). I think that you already did this, so my open() function would be your openPreferences function.
I am creating a status bar app for Mac with a settings view.
I have created a NSMenuItem to launch the settings but I don't find any solutions to launch this view.
What I have tried:
NSWorkspace.shared().launchApplication("AppName")
and
StatusMenuController.swift
func showSettings() {
var mainWindow: MainWindowController!
mainWindow = MainWindowController()
mainWindow.showWindow(nil)
}
MainWindowController.swift
override func windowDidLoad() {
super.windowDidLoad()
self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
I saw this problem was faced in many cases and I found a solution that I don't completely understand but that I'll try to explain.
Here is how documents are managed in macOS :
From my experience, you need to check the box "Create Document-Based Application" when creating a new Mac app to have a NSDocument class (You can also add it later) which will handle how your views are being showed or not.
Adding this NSDocument class to my project made the following code work (and wasn't before) :
let storyboard = NSStoryboard(name: "Main", bundle: nil)
var windowController: NSWindowController!
windowController = storyboard.instantiateController(withIdentifier: "ScanWindowController") as! NSWindowController
windowController.showWindow(nil)
I'm trying to create a MacOS app that plays audio or video files. I've followed the simple instructions on Apple's website here
But I want to use the File > Open menu items to bring up an NSOpenPanel, and pass that to the View Controller.
So presumably, the Open action should be in the AppDelegate, as the ViewController window might not be open.
And then pass the filename to a new instance of the ViewController window.
Is that right? If so, how do I "call" the View from AppDelegate?
Here's the AppDelegate:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBAction func browseFile(sender: AnyObject) {
let dialog = NSOpenPanel();
if (dialog.runModal() == NSModalResponseOK) {
let result = dialog.url // Pathname of the file
if (result != nil) {
// Pass the filepath to the window view thing.
} else {
// User clicked on "Cancel"
return
}
}
}
and here's the ViewController:
class ViewController: NSViewController {
#IBOutlet weak var playerView: AVPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
// Get the URL somehow
let player = AVPlayer(url: url)
playerView.player = player
}
There are some details not disclosed in your question, but I believe I can provide the proper answer still.
You can call NSOpenPanel from AppDelegate, nothing wrong with that. Just note that user may cancel the dialog and how to handle that situation.
Considering the view the best thing is to create WindowController that is connected to the ViewController (it is like that by default) in the Storyboard, then access it from the code using NSStoryBoard.instantiateController(withIdentifier:), and then use its window property with something like window.makeKeyAndOrderFront(self) . If you have NSWindow or NSWindowController class in your code then you should initialize the class in the code and again make window key and front.
I am making a Mac app in Swift, and running into a problem. When my app is first launched from the terminated state, it automatically launches a new window. But if the user Xs out of my app (with the red X icon) instead of quitting it, then hits the app icon of my app, a new window doesn't automatically open.
How can I make my Mac app launch a new window every time the dock icon is hit, as long as there isn't already a window of my app open?
Add this to your App Delegate:
func applicationShouldHandleReopen(theApplication: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
for window: AnyObject in theApplication.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
Don't forget to put
lazy var windows = NSWindow()
to somewhere at AppDelegate
and then implement
func applicationShouldHandleReopen(theApplication: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
for window: AnyObject in theApplication.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
Like #Owlswipe wrote