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

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.

Related

How to disable shortcut for Spotlight Search programmatically?

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"];```

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.

OSX NSUserDefaults not Working

This code gives me always NO in my application. It does indeed work in any other project I copy it... so something must be messed up with my standardUserDefaults, but I absolutely don't know how this can happen and how to solve it!
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"myKey"];
BOOL test = [defaults boolForKey:#"myKey"];
// test is ALWAYS NO here!
Can anybody hint me, where to start or how to get rid of this?
It's a mixed project with swift and objective c and get the same behavior in me AppDelegate.swift, when I put this directly in my applicationDidFinishLaunching
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(true, forKey: "myKey")
let test = defaults.boolForKey("myKey")
// test ALWAYS false here
Before someone asks:
- yes, even with synchronize called between it
- yes this is the whole code, nothing between the lines... set and get it right after does not work
I found a solution, how to fix it, but not why this happens and no clean, good way so far.
If anybody could explain this and give me a better solution, do so!
the problem was:
If you delete the container of a sandboxed app, you also delete the plist for the NSUserDefaults and it's not created again and so NSUserDefaults simply is not working.
the workaround:
As I found here https://ind.ie/labs/blog/app-sandbox-updating-nsuserdefaults-fails-after-deleting-apps-container-directory/ it's a problem with the permanent bookmark of the system.
Well just to empty the trash does not work for me, but what worked: I simply created the missing file!
touch ~/Library/Containers/com.example.myapp/Data/Library/Preferences/com.example.myapp.plist

Force 'Shared User Defaults Controller' to save to disk immediately?

I have a preferences pane that uses the Shared User Defaults Controller, which reading and saving preferences a piece of cake. It seems though that when changes are made to the fields, they aren't immediately saved to the plist fie. This creates a problem when my application needs to re-read the file immediately after the change has been made and the plist still hasn't been updated.
How can I force the preferences pane to update the preferences file immediately?
This will automatically save any change you do right away to disk:
NSUserDefaultsController *controller = [NSUserDefaultsController sharedUserDefaultsController];
[controller setAppliesImmediately:YES];
If you need this only in specific cases, you can also use and save some of the expensive I/O (you really should try to let the cache mechanism cache as much as possible instead of writing everything right away to disk):
NSUserDefaultsController *controller = [NSUserDefaultsController sharedUserDefaultsController];
[controller save:self];
Also, are you sure that you are trying to solve the right problem? You can always get the up to date version of the user defaults by querying NSUserDefaults where you don't need to care about wether the current version is cached in RAM or already written to disk.
Per the docs:
[userDefaults save:self];
or
[userDefaults setAppliesImmediately:YES];

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