Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 15    Views: 212

Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
06/12/14 09:55 AM (10 years ago)

Tell Xcode to do something every 24 hours?

Looking for an example of how to trigger an event in my app every 24 hours. For example, in my slot machine plugin, player coins are stored/displayed in an INT variable called myMoney. I'd like to be able to add a code snippet that will give the player 100 coins for free every day, something like //every day at midnight... myMoney = (myMoney + 100); (it's the "every day at midnight" code that I can't seem to figure out) No need to track how many days since they opened the app and multiply it by those days, because the whole point is to get them to open my app as often as possible, they they will only get 100 free coins, whether it's been 1 day or 30 days, it wouldn't matter. So I guess a more accurate code would be... //if app was last used more than 24 hours ago... myMoney = (myMoney + 100);
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/12/14 10:38 AM (10 years ago)
I might have figured this one out... won't know until tomorrow because it uses the date, and I don't know how to spoof it, lol. Here's what I did. Basically, I have it fetch the current date from the device, store it in NSUserdefaults, then right after that compare the date to the current date, and if the same, it doesn't do anything, but if different it adds 100 credits. In theory, this should give me 100 more credits at midnight tonihgt... we'll see: NSDate *nowDate = [NSDate date]; [[NSUserDefaults standardUserDefaults] setObject:nowDate forKey:@"LastLoaded"]; // Get the current date from NSUserDefaults NSDate *lastLoaded = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoaded"]; NSDate *checkDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60]; if ([lastLoaded compare:checkDate] == NSOrderedAscending) { myMoney = (myMoney + 100); }
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
06/12/14 03:31 PM (10 years ago)
I'd be interested to know if you were successful... I dont 'need' something like that, but was wondering the same thing. Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/12/14 04:35 PM (10 years ago)
I'll let you know after midnight. ;). In theory, it should work
 
tb
buzztouch Evangelist
Profile
Posts: 2050
Reg: Nov 03, 2011
Oxford
32,300
like
06/13/14 12:59 AM (10 years ago)
Did it work? Knowing my knowledge in another code language, your method seems write, but I would be curious.
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
06/13/14 02:19 AM (10 years ago)
The NSUserdefault will be set first every time so the comparison will always be wrong as the todays date will always be set. If you don't put the setter of the nsuserdefault in an if statement it will always return that time. I don't knwo how accurate thsic ode will be without ebing in front of XCode. NSDate *lastLoaded = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoaded"]; if ([lastLoaded timeIntervalSinceNow] < -24*60*60 ){ [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"LastLoaded"]; myMoney = (myMoney + 100); } You can test this by changing the date and time on your phone or on your mac. just remember to reset the time as alot of apps rely on date and time.
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
06/13/14 02:37 AM (10 years ago)
As I said in another post when you come up with this idea. I wouldn't rely on using the ios clock I would refer it to a server which holds the correct date and time and always compare them values. As you are giving away physical prizes, I would integrate a whole backend to sort out this validation, if you delete the app it will remove the nsuserdefault so install and get the free 100 credits again. This is a project I wouldn't scrimp on security. Risks 1. User changes time continues to add credits every day 2. Jail breaking community without encryption you could be screwed 3. Evidence feedback are you going to knock back people who seem to have to many credits, how about if they won them fairly. Look into consumables in app purchase you may need to log everyones purchase in a data base. 4. Most betting sites give free credits but don't let you physically withdraw them. You only get the winning part. Think of someone just stock piling the free coins and claiming a prize. 5. Multiple devices how about if all his friends login as him and start redeeming. 6. Actually not releated to security but valid to above if I lose my phone with all my winnings where do my winnings go. If I ahve an iphone and an ipad I should be able to pick up where I left off. You need to look at possible icloud This is a massive task
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/13/14 07:08 AM (10 years ago)
Nope, didn't work. I think kittsy's right, it has the nsdate being set every time, so it needs the if statement. I'll give that a try. Geez Kittsy, calm down, hahaha. This of course would not be used in the secure environment that I was talking about. It was simply to get an understanding of how to get xcode to tigger something after 24 hours. For the other thing we were talking about, there's a whole security backend logging system that I'm working on that will prevent cheating, and handle device/purchase/logging & apple receipts, similar to how my newsstand magazine does, using my servers installation of Baker Cloud to cross reference anything to do with purchases, device id's, consumables, etc. TOS will define the rules, and the app will log the activity. Anyone trying to circumvent the rules will not be eligible. It would allow them to cheat all they want, but all redemptions would be hand verified and hand compared to their activity log & device info. But for now, just trying to figure out how to get xcode to fire an even after a 24 hour period. I can change the method, or get the date from a different location later on, once the function is working.
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/13/14 10:03 AM (10 years ago)
Hmmm... not working, even with the if statement.
 
Annonymous
Profile
06/13/14 10:23 AM (10 years ago)
Ok, it is in fact working... I think my date just wasn't changing in the simulator when I first tried. Here is the working code (thanks Kittsy!) NSDate *lastLoaded = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoaded"]; NSDate *checkDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60]; if ([lastLoaded compare:checkDate] == NSOrderedAscending) { myMoney = (myMoney + 100); }
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/13/14 10:24 AM (10 years ago)
(whoa, I got logged out, haha. The above comment is mine.)
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
06/13/14 10:28 AM (10 years ago)
thought so i just tested and it worked perfectly
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
06/13/14 10:30 AM (10 years ago)
where are you putting this code by the way in the app delegate or the plugin
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/13/14 10:32 AM (10 years ago)
Yep, realized that I had to stop the simulator completely to get the date to refresh from my mac, not just stop the code and re-run it. Works perfectly :) Now, to move on to more secure methods... I'm not implementing the redemption system anytime soon, but as I upgrade the code with new features like this, I'm keeping it in mind so it's ready to do so if I ever decide to do it. Next task is to incorporate user accounts :)
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/13/14 10:34 AM (10 years ago)
In the plugin, so owners of the plugin can adjust it as well. Eventually it will be settable in the control panel. For the next plugin upgrade, it will allow full control panel settings of daily coin amounts, selectable reel graphics, selectable pay tables, turn in app purchases on or off, and a few other things. I've been spending several days trying to figure out how to get a special symbol to only be possible on the third reel, but because the picker uses the same symbols for all rows, it's not cooperating. I've even tried IF statements to say if reels 1 and 2 land on that symbol, repin them (in the background), but no luck. Might have to skip that feature for now. I was going to use it for a multiplier symbol, but it would pay back too much if I allowed them on all 3 reels.
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
06/13/14 05:25 PM (10 years ago)
Really weird... the code worked this morning, and now it doesn't... no changes. I added an NSLOG event to see whats happening, and lastLoaded always returns null, even after changing the date... both on simulator and device, and does not add the 100 to mymoney. Not sure what happened, and why it worked this morning but not now, with no changes to the code. Any ideas why lastLoaded would return null? NSDate *lastLoaded = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastLoaded"]; NSDate *checkDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60]; if ([lastLoaded compare:checkDate] == NSOrderedAscending) { myMoney = (myMoney + 100); } NSLog(@"Last Loaded: %@", lastLoaded); I've reset the simulator, deleted the data from my device, cleaned and rebuilt... still doesn't recognize the lastLoaded data.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.