Check if app has been purchased - iphone

I am a beginner; I am trying to solve this but am unable to do so.
I have created an app that displays a few options in a table. When the user taps the cell the details of selected option is shown on another page (details are stored in a plist file).
What I want to do is to set it up so that if the user has purchased the app then only the details should be visible, but if the user has not purchased it, the user should be prompted to do so.
I have created a product ID for iTunes Connect and also created a testing account to test the app.
My problem is with the code: how should i check if user has already made the purchase?

Using NSUserDefaults is the easiest solution.
After a successful purchase:
[[NSUserDefaults standardUserDefaults]
setObject:#"purchased"
forKey:#"myPaidItem"];
[[NSUserDefaults standardUserDefaults]
synchronize];
And when you need to check if the user has purchased the item:
NSString* isPurchased =
[[NSUserDefaults standardUserDefaults]
stringForKey:#"myPaidItem"];
if ([#"purchased"
compare:isPurchased]==NSOrderedSame) {
........ }
Hope that helps

you have to store this information in appContext as a bool if the transaction is OK , boo= true
and when he want to buy another time you hve to check the value of boo wich is stored in app Context

Related

storing username and password in session iphone

I want to store username and password in a session , if a user login successfully then he will be directed to the welcome screen where a logout button is exist.
now if user close the application without log out the account and restart the app the he must be directed to the welcome screen skipping the login screen. and if user restart the app after logout then he must enter the username and password to come on welcome screen. help me out. i am new to iphone.
you may store the username and password in User Defaults or in Keychain (if want to keep it secure ). So on loading the app if you find username and password stored in Userdefaults , you may directly login to welcome screen and if not stored then show the login screen...
see this is the same as you want help with iphone session login
you can store whole array in the Userdefault, UserDefault is a one type of session in the iPhone...
here you can store any value or array in the UserDefaul..
In AppDelegate.h file just declare variable...
NSUserDefaults *userDefaults;
here it not compulsory to global declare this userDefault, you can direct use the userdefault where you want
after...
In AppDelegate.m Fille in applicationDidFinishLonching: Method or anywhere which you want to store username and pssword
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefault setObject:yourName forKey:#"UserName"];
[userDefault setObject:yourPassword forKey:#"Password"];
[userDefault synchronize];
after that when you want get data from this UserDefaults Use Bellow Code...
NSString *userName = [[NSUserDefaults standardUserDefaults] valueForKey:#"UserName"];
NSString *password = [[NSUserDefaults standardUserDefaults] valueForKey:#"Password"];
i hope this helpful to you...
:)

NSUserDefaults boolForKey strange behaviour on real phone

My application is approved and ready to sale in appstore today. I downloaded it from appstore immediately but when I open my application there is no ads in my application. I dont understand anything.
In my code if user buy inapp item I set the value of userdefault to TRUE.
So when application starts Im checking this userdefaults. If it is TRUE I dont show any ads , if not I call ads api.
this is part of inapp purchasing area :
if ([productId isEqualToString:#"dontshowads"])
{
//user bought inapp item. so I must remove advertorials.
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:#"aldimi_lite_reklamikaldirma_099"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
and this is viewwillappear area :
BOOL appin_reklamkaldirmaalindimi = [[NSUserDefaults standardUserDefaults] boolForKey:#"aldimi_lite_reklamikaldirma_099"];
NSLog(#"appin_reklamkaldirmaalindimi :%d",appin_reklamkaldirmaalindimi);
if (!appin_reklamkaldirmaalindimi)
{
NSLog(#"showing ads");
}
else {
NSLog(#"no ads. user bought inapp item");
}
When I run my application in simulator everything is fine. (I deleted it first in simulator and re build it)
Console gives this logs:
appin_reklamkaldirmaalindimi : 0
showing ads
When I run my application in my real phone everything is fine also. (I deleted it first in my phone and re build it)
Console gives this logs:
appin_reklamkaldirmaalindimi : 0
showing ads
But when I run my application which is downloaded from appstore everything is different.
Console gives this logs:
appin_reklamkaldirmaalindimi : 0
no ads. user bought inapp item
I tried to download and check the situation in my friends phone . Everything is the same. So it is not about my phone.
how can it possible ? I thought one hour and I cant find any explanation to this situation ...
The only explanation for such a weird phenomenon is that the code which you submitted to the App Store is in some way corrupted or different than the code which you tested by building off your computer. The only way to fix this problem would be to resubmit your application to the App Store.
The only other possibility I can think of is that your app has some VERY poor and incorrect memory-access tendencies, such that part of your executable code or stack somehow gets over-written before running your ad logic.

How to make my app stay login even after power-off on ipad?

I created a web service application. I want my user to allways stay login after installition but i have no idea about how to do this. I save user informations in NSUserDefaults. Thank in Advance
EDIT I log into the server, user informations are firstly created on server and i sen Username&Password ect via a GET to the server. If this data sent in GET are the same with ones in server, the user will login into the application. Anything(content) of application comes from server after logging in.
NSString *savedUserName = [[NSUserDefaults standardUserDefaults] stringForKey:#"userName"];
NSString *savedUserPass = [[NSUserDefaults standardUserDefaults] stringForKey:#"userPass"];
if (savedUserName && savedUserPass){ // check for length as well
//don't show login, create user with these credentials
}
else{
[[NSUserDefaults standardUserDefaults] setObject:#"newUserName" forKey:#"userName"];
[[NSUserDefaults standardUserDefaults] setObject:#"newUserPass" forKey:#"userPass"];
//then show login for logging in as a new user
}
Don't forget to save nil or empty string when user logs out.

YAFBLQ: How to pre-populate FB login and password fields?

Yet another fb login question:
By the time my user has something to share, they've already logged into my app server.
I give them the option to use their FB creds for my app.
So if they share, I want to pre-populate the FB login page with the creds they've already supplied to me.
Does anyone know if this is possible, and if so, how to do it?
Here's what I'm talking about:
I use Facebook API too. You never keep login and pass in your app (check Facebook API doc). However you can keep session. So the user enter login and pass once. In my code I've a "FacebookLogger" who is a singleton with a Facebook object.
To store session I use NSUserDefaults (find doc here).
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.accessToken forKey:#"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.expirationDate forKey:#"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
Where m_Facebook is my Facebook object in my singleton. After that I can catch Access with :
m_Facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:#"AccessToken"];
m_Facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:#"ExpirationDate"];

Facebook Connect for iOS auto signs out after quitting app

I have implemented Facebook Connect in my app, and it seems to work fine. I can publish statuses and everything. However, when I quit the app and run it again, the session token seems to have expired or something, because I can't post anymore. But if I authenticate with Facebook again, it shows that permissions have already been granted, then only when the callback to the app is called, can I post again.
What am I doing wrongly?
you should store the accessToken and expireTime of your Facebook object yourself. Restore it after startup and verify via isSessionValid, otherwise reauthorize user. You can use these functions for it:
Call the store function within your fbDidLogin, and the restore function whenever it is appropriate after startup of your app.
-(void)restoreFBAccessToken {
self.facebook.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:#"fb_accessToken"];
self.facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"fb_expirationDate"];
}
-(void)storeFBAccessToken {
[[NSUserDefaults standardUserDefaults] setValue:self.facebook.accessToken forKey:#"fb_accessToken"];
[[NSUserDefaults standardUserDefaults] setValue:self.facebook.expirationDate forKey:#"fb_expirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I'm also using FB Connect in my own app, but in an indirect way. I use it through a third-party open source kit named ShareKit. http://www.getsharekit.com/
As I remember, ShareKit does the job pretty well. It remembers those login information even though the app is shut down.
Since ShareKit is open-source, I suggest you take a look at its code. It should have an answer for you. :-)
Have you made sure to start the session once again when the app is reloaded up?