How to use Firebase Crashlytics in macOS app

Issue #585 New Firebase Crashlytics Follow the new Firebase Crashlytics guide Get started with Firebase Crashlytics using the Firebase Crashlytics SDK CocoaPods Specify FirebaseCore for community managed macOS version of Firebase platform :osx, '10.13' target 'MyMacApp' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! pod 'FirebaseCore' pod 'Firebase/Crashlytics' end Signing and capabilities Under Hardware runtime, check Disable library validation Under App sandbox, enable Outgoing connections (Client)...

January 28, 2020 ยท 1 min ยท Khoa Pham

How to handle radio group for NSButton

Issue #579 Use same action, or we can roll our own implementation An NSButton configured as a radio button (with the -buttonType set to NSRadioButton), will now operate in a radio button group for applications linked on 10.8 and later. To have the button work in a radio group, use the same -action for each NSButton instance, and have the same superview for each button. When these conditions are met, checking one button (by changing the -state to 1), will uncheck all other buttons (by setting their -state to 0)....

January 17, 2020 ยท 1 min ยท Khoa Pham

How to use Applications folder in macOS

Issue #573 There are 2 Applications folder /System/Applications: contains Notes, Books, Calculator, โ€ฆ /Applications: contains Safari, Xcode, Keynote, โ€ฆ

January 8, 2020 ยท 1 min ยท Khoa Pham

How to allow unnotarized app to run on macOS Catalina

Issue #520 Remove quarantine xattr -d com.apple.quarantine /Applications/Flipper.app

November 28, 2019 ยท 1 min ยท Khoa Pham

How to make Swift Package Manager package for multiple platforms

Issue #504 https://twitter.com/NeoNacho/status/1181245484867801088?s=20 Thereโ€™s no way to have platform specific sources or targets today, so youโ€™ll have to take a different approach. I would recommend wrapping all OS specific files in #if os and just having one target. For tests, you could do something similar, one test target, but conditional tests Every files are in Sources folder, so we can use platform and version checks. For example Omnia is a Swift Package Manager that supports iOS, tvOS, watchOS, macOS and Catalyst....

November 13, 2019 ยท 1 min ยท Khoa Pham

How to use Firebase in macOS

Issue #501 Use Catalyst Add to CocoaPods platform :ios, '13.0' target 'MyApp' do use_frameworks! pod 'FirebaseCore' pod 'Firebase/Firestore' end Troubleshooting Select a team for gRPC-C++-gRPCCertificates-Cpp FIRAnalyticsConnector: building for Mac Catalyst, but linking in object file built for iOS Simulator https://stackoverflow.com/questions/57666155/firanalyticsconnector-building-for-mac-catalyst-but-linking-in-object-file-bui The problem was related to the difference between Firebase/Core and FirebaseCore. The first is a subspec of the Firebase pod that depends on FirebaseAnalytics. The second is only the FirebaseCore pod....

November 12, 2019 ยท 1 min ยท Khoa Pham

How to use Apple certificate in Xcode 11

Issue #458 For push notification, we can now use just Production Certificate for 2 environments (production and sandbox) instead of Development and Production certificates. Now for code signing, with Xcode 11 https://developer.apple.com/documentation/xcode_release_notes/xcode_11_release_notes we can just use Apple Development and Apple Distribution certificate for multiple platforms Signing and capabilities settings are now combined within a new Signing & Capabilities tab in the Project Editor. The new tab enables using different app capabilities across multiple build configurations....

October 11, 2019 ยท 1 min ยท Khoa Pham

How to style NSTextView and NSTextField in macOS

Issue #443 let textField = NSTextField() textField.focusRingType = .none let textView = NSTextView() textView.insertionPointColor = R.color.caret textView.isRichText = false textView.importsGraphics = false textView.isEditable = true textView.isSelectable = true textView.drawsBackground = false textView.allowsUndo = true

October 5, 2019 ยท 1 min ยท Khoa Pham

How to center NSWindow in screen

Issue #442 On macOS, coordinate origin is bottom left let window = NSWindow(contentRect: rect, styleMask: .borderless, backing: .buffered, defer: false) window.center() let frame = window.frame window.setFrameOrigin(CGPoint(x: frame.origin.x, y: 100))

October 4, 2019 ยท 1 min ยท Khoa Pham

How to log Error in Swift

Issue #439 Use localizedDescription We need to provide NSLocalizedDescriptionKey inside user info dictionary, otherwise the outputt string may not be what we want. NSError https://developer.apple.com/documentation/foundation/nserror/1414418-localizeddescription A string containing the localized description of the error. The object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesnโ€™t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code. let error = NSError(domain: "com....

October 3, 2019 ยท 1 min ยท Khoa Pham

How to handle NSTextField change in macOS

Issue #438 Storyboard In Storyboard, NSTextField has an Action option that specify whether Send on Send on Enter only` should be the default behaviour. Code In code, NSTextFieldDelegate notifies whenever text field value changes, and target action notifies when Enter key is pressed import Cocoa class ViewController: NSViewController, NSTextFieldDelegate { @IBOutlet weak var textField: NSTextField! override func viewDidLoad() { super.viewDidLoad() textField.delegate = self } func controlTextDidChange(_ obj: Notification) { let textField = obj....

October 2, 2019 ยท 1 min ยท Khoa Pham

How to add section header to NSCollectionView in macOS

Issue #437 Normal Use Omnia for itemId extension HeaderCell.swift final class HeaderCell: NSView, NSCollectionViewSectionHeaderView { let label: NSTextField = withObject(NSTextField(labelWithString: "")) { $0.textColor = R.color.header $0.font = R.font.header $0.alignment = .left $0.lineBreakMode = .byTruncatingTail } override init(frame frameRect: NSRect) { super.init(frame: frameRect) addSubviews([label]) activate( label.anchor.centerY, label.anchor.left.constant(8) ) } required init?(coder: NSCoder) { fatalError() } } ViewController.swift collectionView.register( HeaderCell.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: HeaderCell.itemId ) func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView { if kind == NSCollectionView....

October 2, 2019 ยท 2 min ยท Khoa Pham

How to show log in Apple Script

Issue #436 Open Script Editor, use log command and look for 4 tabs in bottom panel Result, Messages, Events and Replies log "hello world"

October 1, 2019 ยท 1 min ยท Khoa Pham

How to show context menu from NSButton in macOS

Issue #435 Use NSMenu and popUp func showQuitMenu() { let menu = NSMenu() let aboutItem = NSMenuItem( title: "About", action: #selector(onAboutTouched(_:)), keyEquivalent: "" ) let quitItem = NSMenuItem( title: "Quit Hacker Pad", action: #selector(onQuitTouched(_:)), keyEquivalent: "" ) aboutItem.target = self quitItem.target = self menu.addItem(aboutItem) menu.addItem(quitItem) menu.popUp( positioning: aboutItem, at: bottomView.quitButton.frame.origin, in: bottomView ) } Use Omnia let menuHandler = MenuHandler() menuHandler.add(title: "About", action: { NSWorkspace.shared.open(URL(string: "https://onmyway133.github.io/")!) }) menuHandler.add(title: "Quit Hacker Pad", action: { NSApp....

October 1, 2019 ยท 1 min ยท Khoa Pham

How to make checked NSButton in AppKit

Issue #433 Use Omnia for convenient style and isOn property let checkButton = NSButton(checkboxWithTitle: "", target: nil, action: nil) checkButton.stylePlain(title: "Autosave", color: R.color.text, font: R.font.text) checkButton.isOn = true

September 30, 2019 ยท 1 min ยท Khoa Pham

How to save files in sandboxed macOS app

Issue #432 Read Container Directories and File System Access When you adopt App Sandbox, your application has access to the following locations: The app container directory. Upon first launch, the operating system creates a special directory for use by your appโ€”and only by your appโ€”called a container. Each user on a system gets an individual container for your app, within their home directory; your app has unfettered read/write access to the container for the user who ran it....

September 28, 2019 ยท 1 min ยท Khoa Pham

How to use marked in WKWebView in AppKit

Issue #429 Use https://github.com/markedjs/marked <!doctype html> <html> <head> <meta charset="utf-8"/> <title>Marked in the browser</title> </head> <body> <div id="content"></div> <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> <script> document.getElementById('content').innerHTML = marked(`# Marked in the browser\n\nRendered by **marked**.`); </script> </body> </html> Should use back tick instead of ' to deal with multiline string, and escape back tick let markdown = markdown.replacingOccurrences(of: "`", with: "\\`") marked(`\(markdown)`); webView.loadHTMLString(string, baseURL: nil) For WKWebView to load even local content, need to enable Outgoing connections, and maybe Incoming connections in Sandbox

September 26, 2019 ยท 1 min ยท Khoa Pham

How to enable NSMenuItem in AppKit

Issue #428 Need to set target let item = NSMenuItem( title: title, action: #selector(onMenuItemClicked(_:)), keyEquivalent: "" ) item.target = self Sometimes, need to check autoenablesItems Indicates whether the menu automatically enables and disables its menu items. This property contains a Boolean value, indicating whether the menu automatically enables and disables its menu items. If set to true, menu items of the menu are automatically enabled and disabled according to rules computed by the NSMenuValidation informal protocol....

September 25, 2019 ยท 1 min ยท Khoa Pham

How to use generic NSCollectionView in macOS

Issue #427 See CollectionViewHandler Use ClickedCollectionView to detect clicked index for context menu. Embed NSCollectionView inside NSScrollView to enable scrolling import AppKit public class CollectionViewHandler<Item: Equatable, Cell: NSCollectionViewItem> : NSObject, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout { public let layout = NSCollectionViewFlowLayout() public let scrollView = NSScrollView() public let collectionView = ClickedCollectionView() public var items = [Item]() public var itemSize: () -> CGSize = { .zero } public var configure: (Item, Cell) -> Void = { _, _ in } override init() { super....

September 24, 2019 ยท 2 min ยท Khoa Pham

How to easily parse deep json in Swift

Issue #414 Codable is awesome, but sometimes we just need to quickly get value in a deepy nested JSON. In the same way I did for Dart How to resolve deep json object in Dart, letโ€™s make that in Swift. See https://github.com/onmyway133/Omnia/blob/master/Sources/Shared/JSON.swift public func resolve<T>(_ jsonDictionary: [String: Any], keyPath: String) -> T? { var current: Any? = jsonDictionary keyPath.split(separator: ".").forEach { component in if let maybeInt = Int(component), let array = current as?...

September 12, 2019 ยท 1 min ยท Khoa Pham