Is it possible to only show iAds on first launch - ios

I have a free app that I suspect is only launched by the majority of its users a couple times before its deleted. Is it possible to only show an iAd banner on the first launch of the application, or maybe only implement iAd until after the first ad is filled?

Use NSUserDefaults to store a value that you can check and see if the application has been launched before.
-(void)viewDidLoad {
// Show iAd on first run
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:#"notFirstRun"]) {
// iAd code here
// Update NSUserDefaults
[defaults setBool:YES forKey:#"notFirstRun"];
}
}
I can't imagine a scenario where you would want to limit your ad revenue to just the initial run though.

Related

NSUserDefaults not saving

I am having a problem in my sprite kit app where my NSUserDefaults variable is not working. In createSceneContents (which I know is being called)
if (![defaults objectForKey:#"obj"]) {
difficultyLabel.text = #"Difficulty: Easy";
[defaults setObject:#"Easy" forKey:#"obj"];
} else {
difficultyLabel.text = [NSString stringWithFormat:#"Difficulty: %#", [defaults objectForKey:#"diff"]];
}
and then when you click on the SKLabelNode to change the difficulty and this code is being called
if ([label.text isEqualToString:#"Difficulty: Easy"]) {
label.text = #"Difficulty: Hard";
[defaults setObject:#"Hard" forKey:#"obj"];
NSLog(#"%#",[defaults objectForKey:#"obj"]);
} else {
label.text = #"Difficulty: Easy";
[defaults setObject:#"Easy" forKey:#"obj"];
NSLog(#"%#",[defaults objectForKey:#"obj"]);
but when I stop the program and run it again, it always just says Difficulty: Easy. Any suggestions?
As rmaddy's answer explains, NSUserDefaults won't immediately write data to long-term storage.
The values are saved in temporary memory when you call setObject:forKey:, so if you ever try to call objectForKey: after setObject:forKey:, it will return the value in memory if it exists, and if not, it goes to look for what's been saved to long-term storage.
In the background, the device will eventually save these values to permanent storage. It's something the OS handles, but in normal operation of your app, it shouldn't be necessary to immediately store these values to permanent storage most of the time, so let the OS do this at times when its been optimized to do so (I imagine the OS probably synchs every app's user defaults at once on some regular schedule, and as much as possible, tries to do this when the processor is idle probably).
But with that said, as rmaddy explains, if you've got something that crucially needs to be saved to the permanent storage immediately, you can always call synchronize. Keep in mind though... as long as your app isn't killed while in debug mode, the values you've set to be stored in user defaults will be stored.
But for your own sake, if you want to be absolutely certain, you can put it a call to synchronize in applicationDidEnterBackground as rmaddy suggests. Keep in mind though, this method isn't called if you kill the app.
When you say you "stop the program", what do you mean? Are you running in the debugger and killing the app and rerunning it again? NSUserDefaults doesn't persist your changes immediately. Either call [defaults synchronize] after writing important data or at least add such a call to your app delegate's applicationDidEnterBackground method. And then be sure you put your app in the background before killing it to ensure the data is written first.
To store:
NSString * str = #"name123";
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
[def setObject:str forKey:#"Key"];
[def synchronize];
NSLog(#"str = = %#",str);
To Retrieve:
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
NSString *str2 = [def valueForKey:#"Key"];
NSLog(#" Saved str = = %#",str2);
I found another reason why NSUserDefaults not saving,following code can make value in NSUserDefaults not saving:
NSUserDefaults *def= [[NSUserDefaults standardUserDefaults] init];
...
NSUserDefaults *def= [[NSUserDefaults standardUserDefaults] initWithSuiteName:#"123"];
You can search NSUserDefaults in your project to catch those init twice bug.
I had this issue where it was working previously but then wasn't. I couldn't figure it out.
It turned out restarting my mac fixed it. There must be some issue with the prefsd daemon that runs in the background that can cause this I'm guessing

IPhone - One time setup screen [duplicate]

This question already has answers here:
Detect when an iOS app is launched for the first time? [closed]
(7 answers)
Closed 9 years ago.
I am developing an iphone app which ask for one time set up details like user name in the first two screens when the user launches the app for the first time. I dont want these 2 screens to come up when I am launching the app once the user has given the details.
I tried getting the info from internet but I was not able to how to find it out. May be I am missing the technical term or wordings involved in doing this. Could any one please help in accomplishing this scenario. Any sample code would be very helpful.
Thanks for your time
Just set a boolean variable to the user-defaults. The user-defaults is a way to save information to the phone, external from the application, that the app can call upon whenever you want.
Inside your app-delegate when it boots, check the user-defaults for a boolean.
To save a boolean to user-defaults:
NSValue *state = ... //Whatever state you want. NSValue allows for booleans.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:obj forKey:#"notFirstTimeRun"];
[defaults synchronize];
To load the boolean from user-defaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSValue *state = [defaults objectForKey:#"notFirstTimeRun"];
Do a check:
if ([state boolValue] == true){
//Has ran before, skip your UIViews or whatever
}
else{
//Has not ran before, do your setup or whatever.
}
What you are looking to do is basically store a flag that dictates whether the user has gone through the setup steps. And upon app launch, you check to see if that flag exists with the respective value to denote whether or not to show a setup screen. NSUserDefaults provides a really simple, easy way of persisting state across sessions.
How about setting a bool in AppDidFinishLaunching. You can check that bool everytime the app is launched and show or hide respective screen based on that.
This is how I would do it:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasBeenLaunched"]){
// This is not the first time, therefore open app as usual
} else {
// This is the first time, show special views
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasBeenLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}

is that possible to know my apps is first time run or not in xcode?

i create an apps that i want to prompt an message for the user.. maybe using UIAlertview. After that, if the user run the apps for the second time, the alert won't prompt up anymore.
is that possible? honestly i don't have any idea about how to doing this.
Any idea? I search on STO, actually this link, still confused.
what is NSUserDefaults? How can NSUserDefaults store this information? i mean this is my first time or second time.
thanks.
To know what's NSUserDefaults, I suggest to take a look the official doc.
And of course you can use it to fulfill your goal.
You use a user default to store information about the current amount of runs in the app.
More or less like:
BOOL isRunMoreThanOnce = [[NSUserDefaults standardUserDefaults] boolForKey:#"isRunMoreThanOnce"];
if(!isRunMoreThanOnce){
// Show the alert view
// Then set the first run flag
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isRunMoreThanOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Yes, U can save a value in NSUserDefault for the first time in your app & set it some other value once you open the app.
like
if(![[NSUserDefault standardUserDefault] objectforKey:#"AppOpenFirstTime"])
{
// App Open First time
// Show Alert
[[NSUserDefault standardUserDefault] setObject:#"1" forKey:#"AppOpenFirstTime"]
}
You can check if you stored some value in NSUserDefaults
NSString *flag = [[NSUserDefaults standardUserDefaults] stringForKey:#"not_first_run"];
if (!flag) {
//first run, do somethig
}
and then set it to some value
[[NSUserDefaults standardUserDefaults] setObject:#"just any string" forKey:#"not_first_run"];
NSUserDefaults is a key-value store saved between your application launches.
You can do it exactly as cortez said in your link. NSUserDefaults is written to disc, and will be created and accessed from your app.
See this link
First Time when your application launch at that time boolForKey:#"AlreadyRan" is FALSE. after that set it TRUE.
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"AlreadyRan"] )
{
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:#"AlreadyRan"];
}
With the NSUserDefaults class, you can save settings and properties
related to application or user data.
The objects will be saved in what is known as the iOS “defaults system”.
The iOS defaults system is available throughout all of the code in your app, and any data saved to the defaults system will persist through application sessions.This means that even if the user closes your application or reboots their phone, the saved data will still be available the next time they open the app!

How to determine that user runs the app for the first time?

I got a problem of my iOS app recently. In my app, an instruction view will appear at the first time of running, then hide from then on. How can I implement this effect?
Try to use this function:
- (BOOL) isFirstRun
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"isFirstRun"])
{
return NO;
}
[defaults setObject:[NSDate date] forKey:#"isFirstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
In your app delegate check for a key in the user defaults (your own custom key, something like "AppWasAlreadyStartedPreviously"). If the key doesn't exist yet, it's the first run. You can show your instruction view and add the key to the user defaults. The next time the user starts the app you'll find the key in the user defaults and know that it's not the first run.
See the documentation of NSUserDefaults.
Store a file and check if the file exsists every time when you start the app. If tr file does not exsists then show intro, and then create the file.
The only way I think is to store a value into a specified file, when run the app, you should check the value first, and then you can deal with the result whether the app has already been run.

How to save user informations in application

I enter some user informations to my upp via textifields. I want my app to save these informations and whenever my app is opened, use the same informations. I used NSUserDefaults but if I clean the app from the RAM (mean, from the running processes) and tryto stat/open my app, it crashes. What do I have to use to save user informations all the time.
EDIT
I save my data in AppDelegate, I want when the user clicks home button, user infos be saved. Here is my code:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"url in background: %#", mainUrl);
//uName and passwd are strings that come from other classes
NSUserDefaults *name=[NSUserDefaults standardUserDefaults];
[name setObject:uName forKey:#"userNameDefault"];
NSUserDefaults *passWd=[NSUserDefaults standardUserDefaults];
[passWd setObject:passwd forKey:#"passwordDefault"];
}
EDIT-2
I do this as soon as i receive the response from server in my first view controller. I use these three data objects in sturtup but still it crashes
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:_username.text forKey:#"userNameDefault"];
[defaults setObject:_password.text forKey:#"passwordDefault"];
[defaults setObject:_server.text forKey:#"serverDefault"];
Your setting the defaults again in your view controller when you want to be retrieving them to display them?
uName = [defaults objectForKey#"userNameDefault"]

Resources