How to disable shortcut for Spotlight Search programmatically? - objective-c

I successfully changed the defaults settings for spotlight search via setting 'enabled' to NO using NSUserDefaults, but I need to restart my machine in order to implement the change, I guess there is some process that needs to be restarted. I tried looking through Activity Monitor to identify the process, but it's some minor process and can't absolutely find it. Please help, new to MacOS Development.
NSMutableDictionary* spotlightSearchPreferences = [[[NSUserDefaults standardUserDefaults] valueForDefaultsDomain:#"com.apple.symbolichotkeys" key:#"AppleSymbolicHotKeys"] mutableCopy];
NSMutableDictionary* spotlightSearchEnabled = [[spotlightSearchPreferences valueForKey:#"64"] mutableCopy];
[spotlightSearchEnabled setValue:#NO forKey:#"enabled"];
[spotlightSearchPreferences setValue:spotlightSearchEnabled forKey:#"64"];
[[NSUserDefaults standardUserDefaults] setValue:spotlightSearchPreferences forKey:#"AppleSymbolicHotKeys" forDefaultsDomain:#"com.apple.symbolichotkeys"];```

Related

Crash from [[[NSUserDefaults standardUserDefaults] persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]] objectForKey:#"AppleLocale"][0];

my code is crashing on startup when i call this code
[[[NSUserDefaults standardUserDefaults] persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]] objectForKey:#"AppleLocale"][0];
The app is now live in production and when a user tries to upgrade, even though i have taken out this code it crashes before main. Is there any way to repair this or reset NSUserDefaults before main?
any help would be appreciated!!
Try the defaults command to modify preferences.
defaults delete <domain> will delete defaults (preferences) for the domain (e.g. The value of CFBundleIdentifier in Info.plist).
Also try deleting or renaming the application preferences file in ~/Library/Preferences. But note that the user defaults system caches some data in memory, so incorrect application data is still retained even after the preferences file is deleted.

Xcode 5.0 NSUserdefaults still get stored preferences after deleted the plist

I got a problem about programming an osx application on Xcode 5 while using the NSUserDefaults. Usually, we just use [[NSUserDefaults standardUserDefaults] setObject:#"This is an object" forKey:#"Test"] to remember a user preference. After that, the application will generate a plist file at ~/Library/Preferences/application.bundle.identifier.plist.
The problem is, after I deleted the plist file, the application could still get the preferences I stored. There is no way to clear that plist even if I tried to clean project, restart xcode, delete the files in derived folder. The only way for me to solve this problem is to restart the system, so I guess there is something stored in the memory. The question is how can I clear these stored preferences? (I don't think it's convenient to clear the preferences by adding code manually in debugging and testing.) And I tried the former version of Xcode 4.x, there's no such problem. Anyone has interest could just create a new cocoa project, and add the code like:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"This is an object." forKey:#"Test"];
NSLog(#"%#", [defaults objectForKey:#"Test"]);
under "applicationDidFinishLaunching". Then go and delete ~/Library/Preferences/application.bundle.identifier.plist.
After that, comment the line: [defaults setObject:#"This is an object." forKey:#"Test"];
in your code and run the application again. The console will still show "This is an object."
My environment is Mavericks GM and Xcode 5.0(5a1413).
Hope this is not something just only happened to me and appreciated any help!
This is an OS X issue not directly related to the version of Xcode you are using. Apple's official line is that deleting the plist file to remove preferences has never been officially supported, and in more recent OS X releases it is unreliable due to changes in the way the preferences are stored.
The supported way to remove preferences is to use the defaults command at the terminal, e.g.:
defaults delete application.bundle.identifier
The defaults command can also remove/change individual settings with in the preferences. For full details see man defaults.

How to set the User-Agent in UIWebView(IOS5)

The only way I have find is:
NSDictionary*dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:#"my user agent", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[[NSUserDefaults standardUserDefaults] synchronize];
It is work well.But the only problem is that we can't modify the User-Agent later in the program.
Did anybody can solve this problem or have some other way to set the User-Agent in UIWebView?
What I want to do is set the User-Agent in UIWebView freely in the project.
I experimented with this a while back. It is more about the timing than the actual value set. It takes time for it to cycle around and update any changes. The default value needs to be changed/set very early in the application. Like in -applicationDidFinishLoading. Any changes after that may need a bit before the changes will get picked up.
This method does work, but if you set it or change it, then try to load a webview right away, it won't always pick it up.
You can try setting the new values on the main thread and allowing it to finish before trying to load anything new... that might help.

How can I get the preffered languages for a specific user on MacOS?

I'm developing an application for Mac OS which is used by network administrator to prompt the interactive user to select actions he wants to perform after some software deployment(actions such as restart now, restart later etc.). The dialog will be started remotely from root account(through SSH connection).
I have a xml file with localization information from which I want to initialize the dialog GUI and for this I need the top most preferred language of the user but
[[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"]
returns
(es, en, de) when the app is started from interactive user
and
(en) when is started remotely from root account
For getting name of the user I am using bash command 'stat -f\%Su /dev/console' but I can't find how to get preferences for this specific user.
Thanks in advance!
Found a "dirty" solution, the preferences list .GlobalPreferences.plist located in Library/Preferences/ folder of the [user] home directory contains preferred languages of the [user], default home directory is /Users/[user] and if the user is root the path is /var/root/ but this path can be configured, here method -(NSString *)stringByExpandingTildeInPath form NSString can be used to get home directory.
Bellow is the code to get this info:
NSString *interactiveUser = [self getInteractiveUser];
NSString *path = [NSString stringWithFormat: #"~%#/Library/Preferences/.GlobalPreferences.plist", interactiveUser];
path = [path stringByExpandingTildeInPath];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *languages = [dict objectForKey:#"AppleLanguages"];
where [self getInteractiveUser] returns interactive user using 'stat -f\%Su /dev/console' command.
Works on Leopard and Lion.

How do I enable Local Storage in my WebKit-based application?

I have a Cocoa/Objective-C application which embeds a WebKit WebView. I need to turn on database support and local storage. I know it can be done--I have it working in Safari--but I can't find an example of how to set this up in my own application.
I found this (unanswered) SO question which provides an example but, as the original poster mentions, doesn't work. And in fact, the methods he uses (setDatabasesEnabled, setLocalStorageEnabled) aren't defined in my WebKit.framework (Xcode 3.2.5), although they appear to exist if I define them myself.
Can anyone provide an example of how to enable local database storage for a WebKit-based Cocoa application? Many thanks if so!
Update: I've got something working...I was confused by "databases" vs. "local storage", which are apparently quite different things. Here's the code that works:
WebPreferences* prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:#"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];
So that works, but it requires the private method _setLocalStorageDatabasePath, which means no App Store for me. So my amended questions is now: is there a way to make this work without using a private method? I found the WebDatabaseDirectory preference key in this answer, which controls where databases go. But I couldn't find a corresponding key for local storage anywhere in the sources. Or is there a way for me to force local storage to use the database, and so the WebDatabaseDirectory key? Any ideas?
I submitted an app using this code to the Mac App Store, and Apple approved it:
Objective-C
WebPreferences* prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:#"~/Library/Application Support/MyApp"];
[prefs setLocalStorageEnabled:YES];
Swift 3
var prefs: WebPreferences? = webView.preferences
prefs?._setLocalStorageDatabasePath("~/Library/Application Support/MyApp")
prefs?.localStorageEnabled = true
Whether they will continue to approve that, I don't know, but they allowed it for my application as of 2011-01-29. Update: They also approved a version update to the same app, so it has gone through twice.
I'm going to take the Javascript to Objective-C bridge approach and store everything in core data. Set localStorage to false, then build a JS object and an instance named "localStorage" with the same methods. My javascript devs won't know the difference, and I already had to do the same thing with Air (basically). There's another way to leave the localStorage intact even though it doesn't actually store them in a persistent db. The elements can be iterated through in javascript and manipulated from there, but I think it will be better to simply replace the object with my own.
After a lot of pain and frustration I found a way to enable local storage and have it persist across application runs properly. This solution is specifically for OSX, but it may be applicable to iOS as well.
Download and add this header file into your project. It's not included in the XCode Webkit distribution.
click to download WebStorageManagerPrivate.h
Add to it, the following lines:
static NSString* _storageDirectoryPath();
+ (NSString *)_storageDirectoryPath;
These allow you to retrieve the directory location of the WebKit local storage tracker database. This is important because due to a bug in WebKit, if you don't store your LocalStorage WebView files in the same directory as the tracker database, they are deleted every other time you run your application. I didn't see a way in the WebStorageManager code to change this location for an individual application. It is always read from the user preferences.
Include the WebStorageManagerPrivate.h in your appDelegate.
#include "WebStorageManagerPrivate.h"
You need to download and include in your project another header not included in XCode distribution. Save it as WebPreferencesPrivate.h
click to download WebPreferencesPrivate.h
Include the WebPreferencesPrivate.h in your appDelegate.
#include "WebPreferencesPrivate.h"
Now use the code below in your applicationDidFinishLaunching handler to initialize and enable LocalStorage. The code assumes you have an IBOutlet named 'webView' for the WebView you are using.
NSString* dbPath = [WebStorageManager _storageDirectoryPath];
WebPreferences* prefs = [self.webView preferences];
NSString* localDBPath = [prefs _localStorageDatabasePath];
// PATHS MUST MATCH!!!! otherwise localstorage file is erased when starting program
if( [localDBPath isEqualToString:dbPath] == NO) {
[prefs setAutosaves:YES]; //SET PREFS AUTOSAVE FIRST otherwise settings aren't saved.
// Define application cache quota
static const unsigned long long defaultTotalQuota = 10 * 1024 * 1024; // 10MB
static const unsigned long long defaultOriginQuota = 5 * 1024 * 1024; // 5MB
[prefs setApplicationCacheTotalQuota:defaultTotalQuota];
[prefs setApplicationCacheDefaultOriginQuota:defaultOriginQuota];
[prefs setWebGLEnabled:YES];
[prefs setOfflineWebApplicationCacheEnabled:YES];
[prefs setDatabasesEnabled:YES];
[prefs setDeveloperExtrasEnabled:[[NSUserDefaults standardUserDefaults] boolForKey: #"developer"]];
#ifdef DEBUG
[prefs setDeveloperExtrasEnabled:YES];
#endif
[prefs _setLocalStorageDatabasePath:dbPath];
[prefs setLocalStorageEnabled:YES];
[self.webView setPreferences:prefs];
}
I hope this helps others have struggled or are still struggling with this issue, until it is fixed properly within WebKit.