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.
Related
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 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 .
Xcode 11, Swift 5.1
I'm getting a couple warnings every time I open a file and view it with QuickLook (QL). It seems to be working fine, but I'm wondering if I can get rid of the warnings.
The warnings say:
-[QLPreviewPanel setDelegate:] called while the panel has no controller - Fix this or this will raise soon.
-[QLPreviewPanel setDataSource:] called while the panel has no controller - Fix this or this will raise soon.
I set up and use QL on an NSTableCellView like this:
import Quartz
class AttachmentCell: NSTableCellView, QLPreviewPanelDataSource, QLPreviewPanelDelegate{
var quickLookItem:URL!
#IBAction func clickPreview(_ sender: Any) {
guard let panel = QLPreviewPanel.shared() else{ return }
panel.delegate = self
panel.dataSource = self
panel.makeKeyAndOrderFront(self)
}
func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
return 1
}
func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
quickLookItem = URL(fileURLWithPath: "...").appendingPathExtension(...)
return quickLookItem as QLPreviewItem
}
}
The clickPreview method is on an NSButton I have in my table cell. The closest thing I could find was this, but I don't see how the responder chain is involved: QLPreviewPanel in tableview with issue: "has no controller"
I also tried setting up my delegate methods on my NSViewController instead, but the same warning shows up.
Any ideas how to solve this? Or can I safely ignore it?
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)
Xcode 8.3.2 I don't find QLPreviewPanel in the command list and I don't know how to do (which command must be used) to display a file preview in a ViewController.
First of all you will need to add the import Quartz statement to your NSViewCOntroller. Second step is to add QLPreviewPanelDataSource, QLPreviewPanelDelegate to its declaration. Next you just need to get a reference of the shared QLPreviewPanel, make the view controller its dataSource and delegate and make its window key and order front.
You will need also to add numberOfPreviewItems and previewItemAt methods to your controller. You can do it as follow:
import Quartz
class ViewController: NSViewController, QLPreviewPanelDataSource, QLPreviewPanelDelegate {
#IBAction func button(_ sender: NSButton) {
if let sharedPanel = QLPreviewPanel.shared() {
sharedPanel.delegate = self
sharedPanel.dataSource = self
sharedPanel.makeKeyAndOrderFront(self)
}
}
func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
return 1
}
func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("yourImageAtTheDocs.png")
return url as QLPreviewItem
}
}
Sadly Apples QuickLookDownloader Demo uses Obj-C. I've created a Swift Versiob which is basically the implementation of #Leo Dabus answer in a Demo Project: Panel and Popover example