How to programatically open Settings window in a macOS SwiftUI App - macos

I have created a simple macOS-only SwiftUI app and added a Settings screen as per Apple's instructions:
import SwiftUI
#main
struct MyApp: App {
var body: some Scene {
WindowGroup {
RootView()
}
Settings {
SettingsView()
}
}
}
It works: the Preferences option shows up in the app menu. Now, I would also like to open this settings window programatically, e.g. when a button is clicked. Is there a way to achieve this?
I was hoping there would be a method in NSApplication, similar to the About window, but there doesn't seem to be one.

By inspecting the app menu, I found this solution:
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
I assume that it may not pass App Review for using a private API, though, so less shady alternatives are still welcome.
Update: This has actually passed App Review, so I'm marking it as the answer for now.

On macOS 13+ versions the action name has been renamed to Settings.
So to update Joshua's answer, here is the solution to use:
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
I wonder if there is a settings scene id we can use with the new openWindow environment method. :)

Related

How to use a differente theme for a specific page in Xamarin Forms?

I have a default theme for my app. I customize this theme using Style file in Android. But I'm creating a login page that I want use other theme just for it. How can I do that?
I am not sure you can do that, because Android theme can be applied to an activity in Android, but Xamarin.Forms run "within" a single activity, so you would have to do the styling either in Xamarin.Forms. Check out this post on Xamarin Forums where the developer was able to change the theme at runtime, which however restarted the activity and in turn also his Xamarin.Forms app.
If your Xamarin Forms app is only using one activity, then this is quite straightforward. This solution is for Lollipop and versions after. You can do this using platform specific Android code:
public class StatusBarStyleManager : IStatusBarStyleManager
{
public void SetStatusBarColor(Android.Graphics.Color color)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.DecorView.SystemUiVisibility = (StatusBarVisibility)SystemUiFlags.LightStatusBar;
currentWindow.SetStatusBarColor(color);
});
}
}
Window GetCurrentWindow()
{
//Note this is a nuget package for retrieving the activity.
var window = CrossCurrentActivity.Current.Activity.Window;
// clear FLAG_TRANSLUCENT_STATUS flag:
window.ClearFlags(WindowManagerFlags.TranslucentStatus);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
return window;
}
}
Then just use this implementation in your pages to set their specific status bar colors when they appear.
Inspired by this article when I needed this in my own projects:
https://evgenyzborovsky.com/2018/08/20/dynamically-changing-the-status-bar-appearance-in-xamarin-forms/

IconToolbarItem with Iconize not click event

I am currently developing a Xamarin.Forms application. I am using IconizePlugin. The app is tabbedPage based and on toolbar I'm trying to include icons for some actions.
This is whay I'm getting:
Icons are showing well and in Xamarin.iOS gets the click event, while it is not working in Xamarin.Droid.
I included the corresponding nugets and followed every steps told in documentation and can't archive this. Other iconize controls in Droid are working fine except the IconToolbarItem.
Please help!
The workaround I found was to declare de IconToolbarItem in the c# code instead of xaml. Although I don't know why it wasn't working while on xaml.
Declaring the item li
ToolbarItems.Add(new IconToolbarItem
{
Icon = "fa-plus",
IconColor = Color.White,
Command = new Command(this.addBono)
});

Unable To Share Text On Xamarin Form

Hello Everyone I want to share the Text on Xamarin Form using Library plugin.share. I have successfully implemented the library and on android i can able to share the text but in iOS it returns me nothing. i have done the share code on button click event so when i click on button in iOS its doesn't returns me anything.i tried the Below code but doesn't got succeed
CrossShare.Current.Share("Hii alll", "Share");
please help me to get out of this,thanks in advance
You want to take a look at UIActivityViewController, you can find the documentation here https://developer.xamarin.com/api/type/MonoTouch.UIKit.UIActivityViewController/
And Xamarin.Forms specific example http://devcenter.wintellect.com/jwood/implementing-sharing-in-xamarin-forms
This is a bit old, but I was having a similar issue and was able to resolve it.
First of all, you need to add the plugin Plugin.Share to ALL Xamarin.Forms projects (Portable, iOS, Android, UWP).
In the class that references the share code, add...
using Plugin.Share;
using Plugin.Share.Abstractions;
CrossShare.Share() doesn't work with strings, directly. You need to new up a ShareMessage.
var title = "Title For The Share";
var message = "Text to share.";
await CrossShare.Current.Share(new ShareMessage { Text = message, Title = title });
I can't say specifically to iOS, as I don't have a Mac and can't test that project, but Android and Windows 10 is working.

Custom url protocol for OSX desktop app using electron by atom

I am new to dekstop app by electron. However i built a desktop app using this source code and just changing the links to my site. Now I have a functional desktop app for mac.
But i want to open my app using a link say appName://and-some-link-follows
How can i implement custom url schema to open dekstop app whenever i click appName://....
For example:
I want to do something like Slack desktop app which opens up by clicking slack://and-some-link
Any help please. Stuck here for 2 days. Thanks in advance.
Today I faced a similar thing and upon doing some researching, I found this useful tutorial on how to do it. It explains everything thoroughly and I think it might be useful for others facing the same issue. This is the highlights:
const electron = require('electron')
const protocol = electron.protocol
// handles links `todo2://<something>`
const PROTOCOL_PREFIX = 'todo2'
function createWindow () {
mainWindow = new BrowserWindow({width: 1000, height: 800})
protocol.registerHttpProtocol(PROTOCOL_PREFIX, (req, cb) => {
const fullUrl = formFullTodoUrl(req.url)
devToolsLog('full url to open ' + fullUrl)
mainWindow.loadURL(fullUrl)
})
}
Also there's a guide on how to build for both OSX and Windows at the end of the post I linked.
For those who'd like to read a bit more, here's a link to the official docs.
I found it.
Do a build for your app
Right click -> show package content
In info.plist add the following
Open app once to register customURL into system.
In terminal enter /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump|egrep "(bindings.*:$)"|sort to check wheather yours is in the list.

Xcode Autocomplete Member From Class Extension

Autocomplete isn't working for me when accessing members from a class extension in Xcode (6.3.2).
I made a UIColor extension, and I am accessing them via UIColor().sampleExtendedColorName(), and it does not show up while I'm typing. Autocomplete works for everything other than this. Is there a setting I can change?
extension UIColor{
func sampleExtendedColorName() -> UIColor {
return UIColor(red:200/255, green:100/255, blue:120/255, alpha:1.0)
}
}
Update (new info):
So autocomplete is working if I access the UIColors from a ViewController that directly subclasses UIViewController, but if I subclass a custom CustomViewController that is a subclass of UIViewController, autocomplete doesn't show up for some reason.
Checking all the Target Memberships is working fine. If you don't know where the Target Membership is,
Go to your swift file (Eg: ViewController.swift)
On the right side in the Utilities panel, Click file inspector
You can see Target Membership there.
Check all the fields.
Figured it out, issue was on my ColorExtension.Swift file in my Xcode project. I needed to check all the Target Memberships.

Resources