mhdhejazi / Dynamic

Call hidden/private API in style! The Swift way.

Home Page:https://medium.com/@mhdhejazi/calling-ios-and-macos-hidden-api-in-style-1a924f244ad1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mac Catalyst with menu-bar app in NSPopover

tommycarpi opened this issue Β· comments

Hi guys, I'm sorry if I'm flooding the issues here but at least I hope these may serve as reference for the ones to come πŸ˜†

Again with Mac Catalyst and React Native, I was trying to make a Menu-Bar app with Dynamic.NSPopover and Dynamic.NSVireController but it crashes badly:

Screen Shot 2021-01-29 at 20 21 27

Here is my AppDelegate


import UIKit
#if targetEnvironment(macCatalyst)
import Dynamic
import Cocoa
#endif

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  var bridge: RCTBridge!
  
  #if targetEnvironment(macCatalyst)
  let statusItem = Dynamic.NSStatusBar.systemStatusBar.statusItemWithLength(-1.0)
  let popover = Dynamic.NSPopover()
  #endif
  
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let jsCodeLocation: URL
    
    jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index", fallbackResource:nil)
    let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "myApp", initialProperties: nil, launchOptions: launchOptions)

    // Code to make it work outside NSPopover
    // ################
    // let rootViewController = UIViewController()
    // rootViewController.view = rootView

    // self.window = UIWindow(frame: UIScreen.main.bounds)
    // self.window?.rootViewController = rootViewController
    // self.window?.makeKeyAndVisible()
    // ################
        
    #if targetEnvironment(macCatalyst)

    statusItem.button.image = Dynamic.NSImage.imageNamed("iconbar")
    statusItem.button.action = #selector(openApp(_:))
    
    let rvc = Dynamic.NSViewController()
    rvc.view = rootView

    popover.contentViewController = rvc
    popover.showRelativeToRect(statusItem.button.bounds, ofView: statusItem.button, preferredEdge: 1) // NSRectEdge.minY = 1
    
    #endif
    
    return true
  }
  
  
  #if targetEnvironment(macCatalyst)
  @objc func openApp(_ sender: Any?) {
    // Now it does nothing
  }
  #endif
  
}

#if targetEnvironment(macCatalyst)
extension UIWindow {
    var nsWindow: NSObject? {
        var nsWindow = Dynamic.NSApplication.sharedApplication.delegate.hostWindowForUIWindow(self)
        if #available(macOS 11, *) {
            nsWindow = nsWindow.attachedWindow
        }
        return nsWindow.asObject
    }
}

#endif

Any help would be appreciated. πŸ˜…

Thanks