Get Current User from root mode? - objective-c

I am writing a login item and I am trying to find if its possible to get the current user.
Lets say, I have logged in with user "Test" and when I execute the exe with root privileges and if I use
char *user = getenv("USER");
user is "root".
My expected answer is "Test".
How can I get it?
I don't know if I can put objective c code in login item? Is it possible to NSUserName in login item.
NSString *user = NSUserName();

You want the SCDynamicStoreCopyConsoleUser function.
QA1133 gives some relevant details and caveats.

Login items can be Cocoa applications, so you can use NSUserName() (which is in Foundation by the way).
Maybe also look at getuid() / geteuid()

Related

Store an array on the app group for WidgetKit

I'm trying to store an struct array into the app group container to use it later on the widget.
let's assume, I have an array of string
let = array = ["object1", "object2", "object3"]
and I saw here I can access to the app group container url
let applicationGroupId = "group.com.development.widget"
guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: applicationGroupId) else {
fatalError("could not get shared app group directory.")
}
Now, I need to know how I can store this array on app group and how I can read from it on the Widgetkit SwiftUI class
Your help will be appreciated.
Signing, Capabilities, Entitlements
Right now it should be possible to do all of this from within Xcode (12+). In the app's project settings go to the Signing & Capabilities tab and make sure Automatically manage signing is enabled and the Team has a valid development team (either company or personal account).
Tap on the + Capability button on the top left to enter the App Group Capability (this will also add an entitlements file). Now you will find an App Group section. Hit the + button for App Groups and enter a globally unique identifier (something like group.<appid>). This will create the app group on the developer platform and add it to your app's provisioning profile and entitlements.
You must repeat the same steps and use the same app group for all targets (app target, widget extension target and (optionally) intent targets). If you hit the reload button for the other targets' app group it should automatically show the app group you previously registered and you can check the checkbox. If not, hitting + and using the same id should work as well.
Finally, check all entitlements files and ensure that they have the same String in App Groups (copy and paste just to make sure).
Code
In code you now just have to access the user defaults using the chosen app group as suitName. To test whether everything is set up correctly, I would put the following line in application(_:didFinishLaunchingWithOptions):
UserDefaults(suiteName: "group.id.test")!.setValue(["object1", "object2", "object3"], forKey: "test")
In the Widget just try to put it in a Text view:
struct WidgetTestEntryView : View {
var entry: Provider.Entry
var text: String {
(UserDefaults(suiteName: "group.id.test")!.value(forKey: "test") as? [String])?.joined(separator: " ") ?? "Nothing stored yet"
}
var body: some View {
Text(text)
}
}
Note: I force unwrap the user defaults on purpose to fail early and check whether the setup of capabilities/signing worked. In production you should guard against a failure.
Alternative Storage
As you pointed out it's also possible to persist data in a shared container by writing to url provided by FileManager.default.containerURL(forSecurityApplicationGroupIdentifier:) but I don't think you need that for your simple example. If you want to share data that is more complex than String, Int, Array ... make sure to convert it to Data since user defaults cannot handle arbitrary data (even if it's Codable).
Manual Signing
If you use manual signing you just have to do the same step in the developer console starting with registering an app group. After that, add the group to all identifiers' capabilities and recreate all provisioning profiles. From there you should be use similar steps to the ones described above.

How to clear Cookies in WebView?

How do you clear the cookies from a web-view in visual studio 2015.
The cookies persist through the code below and even through an application restart.
Dim cookieManager = httpBaseProtocolFilter.CookieManager
Dim cookieCollection = cookieManager.GetCookies(New Uri("https://www.example.com"))
For Each cook As HttpCookie In cookieCollection
cookieManager.DeleteCookie(cook)
Next
The title of this question does not specify the question. I see two questions of which I may have an answer for one. The question mark at the end of the following quote leads me to believe that you are wondering how to fix the fact that the next user is already logged in:
Navigate to a page and ask a user to login
Dim req As HttpRequestMessage = New HttpRequestMessage()
req.RequestUri = New Uri("https://example.com/login")
_WebView.NavigateWithHttpRequestMessage(req)
They do a process and then they close the application
Now when the next user opens the application they are already logged in?!
When the first user closes the application it is an event. Write code which resets the login state to false when the first user closes the application.
You should be able to clear the cache using WebView.ClearTemporaryWebDataAsync.. Some people have reported though that this isn't working for them.
The "old" way of handling this is adding a query string parameter, like a timestamp, to the browser. This usually allows the bypass the caching. So instead of navigating to example.com/login, you navigate to example.com/login?stamp={DateTime.Now.Ticks}

Get list of users that can log in to a Mac

How can I get list of users that can log in to OS X? That is, users that appear in the login window.
I know about Get all Users on OS X
This solution works for only 64 bit architecture. When I switch to 32 bit, it gives all users.
And I do not want to enumerate the Users directory, as a user's home directory can be moved to another location, and other directories can be created inside /Users.
using provided link in question we can retrieve all users.
and the answer to question:
we get CBIdentity object, That object has isHidden flag this is true if user is visible on login window.
Ex:
bool isTrue = [identityObject isHidden];
if(isTrue)
{
NSLog("User is login user");
}

Detect First Time User

How can I detect if the user has just downloaded the application and opened it for the first time? IS this a NSUserDefaults? I want to be able to show a welcome screen only the first time my application is run.
Thanks
check for a bool in NSUserDefaults and if it is not set do whatever you want and save a YES-bool back to NSUserDefaults. If you show an alert you probably should put the setBool:forKey: in the delegate method which is called after you have dismissed the alert.
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"wasLaunchedBefore"]) {
NSLog(#"First launch");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"wasLaunchedBefore"];
}
You can use the NSUSerDefaults.By this when the user opened app check whether there is any values in your user defaults for a key.If it is not then that is first time.After this check you have to update the value for the key which you have checked previously.
You can check if flag is set in NSPreferences or check if file exists in app's file (file that you create after the first launch).
You need to set in your application if opened first time then show that stuff which you want to show first time. There is no any other way to find out that user downloaded your application and he run it or not. On run process you need to set it inside your application.
If you use NSUserDefaults then user can reinstall your application. And application will think thank user use it for first time again. But after updating application remembers that the user has already launched it.
I can't understand from your question if it is appropriate for you but the most of applications work this way

is it possible to locate file on Mac without knowing exact location?

I am trying to locate a file on Machintosh without knowing exact file location. I am developing an app which needs to locate iTunes library XML file and parse it for data. However XML resides in,
/Users/**Current logged in username**/Music/iTunes
Now username may change, So how to set path of let my code find below named file programmatically. Language used is Objective - C.
File name : iTunes Music Library.xml
Possible solution I thought is to get username by code. Is there any method which returns a string of current logged in user's "username".
Thanks
You should be able to use ~/Music/iTunes. ~ represents the home directory of the current user.
Or you could could see this question: Mac OS X: Get current username and home directory for current user from Directory Services
Don't do this! The user's iTunes library doesn't have to be in the default location.
Instead, read the iTunesRecentDatabasePaths key from com.apple.iapps. It'll give you an array that represents the recently used iTunes libraries.
$ defaults read com.apple.iapps iTunesRecentDatabasePaths
(
"/Volumes/Media/iTunes/iTunes Library.xml"
)
NSHomeDirectory() is a Foundation function that returns an NSString of the path to the current user's home directory.
Apple Developer Library - NSHomeDirectory