NSTimer After User Leaves App - ios

I am creating an iOS app in Objective C. I don't want to pay for a server, but I want to create a reward system so that every 3 hours the user is rewarded with "coins" when they enter the app. For example, if a user exits the app at 1pm and re-enters the app at 5pm, a reward will be ready. However I want this to carry over even if the app is not running in the background.
Is this possible?

Maybe not a timer here, but save users' exit time through NSUserDefaults, and then whenever users re-enter the app, compare the exit time with the current time to see if the gap is 3 hours.
For example, in viewWillDisappear()
You might save the exit time through:
NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey: "exitTime")
And in viewDidLoad(), you could extract the exit time through
NSUserDefaults.standardUserDefaults().objectForKey("exitTime")

In your AppDelegate , you can save exit date time to NSUserDefaults
when below function did called
func applicationWillTerminate(application: UIApplication)
func applicationDidEnterBackground(application: UIApplication)
when user enter foreground, you can compare exit time with current time and give some coins
but this way have some problem
if user edited iPhone's date or time in setting , can get a log of coins
but if you use server , you can compare date correctly
there are some idea to solve this problem without server
how about save current time or date periodically ?
this is usecase
user edit iphone's time to 3 hour after
user enter app
user get coins
user edit iphone's time to 3 hour before
user lose coins and some item
if currentdate is earlier then saved currentdate then you have to take coins because that is invalid coins
how about my idea?

Related

How to detect if user navigates away from my app in iOS?

I am trying to develop an app which helps people focus. Essentially, users start a session in which they would like to focus. This session lasts as long as the user does not open another app (for example, the user cannot open Facebook). If the user does so, the session is marked as a failure. Users try to last as long as possible.
The issue I see is that iOS does not allow apps to run for more than 3 minutes once the screen turns off. That means that a user can start a session, then put the phone down for 3 or more minutes, and when they unlock the phone, they are presented with their home screen. The app has been killed. A user is then free to roam around and look at any other app, defeating the purpose of my app.
I could use a timer running on a server to maintain overall session duration, and resume the session once the user reopens the app, but this does not solve the issue of users being able to roam before reopening the app.
Are there any ways to get around iOS killing my app? The behavior I want is:
1) User starts session.
2) Session timer begins.
3) If user navigates away from app, session terminates after 30 second warning.
4) If user locks phone, app still runs.
5) When user unlocks phone, app is displayed (not homescreen). Thus, the termination logic is the same.
Thanks!
You can use an observer (add it to your ViewController's viewDidLoad):
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: UIApplication.willResignActiveNotification, object: nil)
And the function will look like this:
#objc func applicationWillResignActive(notification: NSNotification) {
// do something
}

How can you stop an app from refreshing if it enters the background?

I'm building a social network with the usual functions like a feed. When I am closing the app via home button it sometimes just needs a minute to get refreshed when I am re-entering the app.
Obviously that is bad, when you are scrolling down the feed, you want to get back to the spot where you were the last time. It even refreshes the "new Post" screen when I wrote the title and go to safari to copy a link.. You get the gist of the problem.
Apps like Reddit do it very well and quite long.
Research just showed me functions to fetch in the background, which will let the app stay in its current state, I suppose, but I don't want to fetch anything, just expand the time before it refreshes.
func applicationDidEnterBackground(_ application: UIApplication) {
UIApplication.shared.setMinimumBackgroundFetchInterval( 60 ) )
}
...
Any help to get a longer "active" state is appreciated, I'm just clueless!
Thank You
First of all , usually it happens when we are on the last index we call the refresh api with loader and wait for response and append in previous response . index will be maintained
Second if you want to refresh when appbecomeActive from background state you make app the all the list refresh and index won't be maintained
or
when app enter backgroundstate get the index number and when app is inthe foreground or active state just scroll to that index
if my anwswer is according to your question please elaborate your question
Thank you

Automatically log out user when they leave the iOS app

I have an in-house app which is used by staff but the chances are the device it is used on could become consumer facing. With that in mind I want to ensure that should the staff forget to logout when they switch apps or just reopen the app that I have a command in there to effectively log them out.
After researching I think the best way for me would be to use:
optional func applicationWillEnterForeground(_ application: UIApplication)
and then force the app to go to the login page or the reverse so that when app enters background it forces the app to the logout URL.
Which do you think would be best and how can I use that command to then add in the chosen URL as described above?
So, while I agree with #Rakesha-Shastri in that ""app enters background it forces the app to the logout URL" This seems like bad UX. The first one where you display the login page on returning from background seems fine. It is important that the user is able to resume his work where he left off after logging in again," there does need to be a way, in-case a user is gone too long, that the credentials have passed. It seems in your case, that every time the user LEAVES or CLOSES the app, you want this to be unauthenticated. What if the user gets a phone call? Should it do that? You may want to use Timer, of say some period of time, 2-5 minutes maybe.
Any who, what you can do is force the user to have to RESTART the app, by either presenting a controller that has NO CAPABILITY of going anywhere, therefore forcing a restart, or providing a button that sends them to a login screen you have implemented.
Note:
I would definitely indicate to the user, "due to purposes of security, each time you exit the app, it requires an authentication to re-access. Please log back in". Then provide a button to the login screen.
As you did not provide code, and I'm not going to do this for you, a direction to take this would be to utilize optional func applicationWillEnterForeground(_ application: UIApplication) alongside with getting the current UIViewController. I would google how to do that. Then from there, you can create a new UIViewController that presents this button back to the login screen.

Notification from the background

I'm currently making a TODO app and I save the date that the user has to do a thing. Let's say that he saves the date 28-7-2016 to do something, I make another column tobenotified and I take the date minus it by 1 and save it, so the user should be notified the previous day that tomorrow has a thing to do.
But what if the user has closed the app(killed it). What is the proper way to send a notification from the Realm database when the date tobenotified is equal with NSDate() / the current date?
It's not as simple like that. Realm queries can neither match against a live-updating current date nor send notifications from the background.
You want to look into UILocalNotification and set it up with the tobenotified date as fireDate and schedule it locally. Note that you will require your users consent and have asked them initially by registering for local notification types so that you're permitted by the OS to schedule and present notifications. Also if the user can modify or delete a reminder, then you need to find a way to identify the notification via the the provided identifier and remove or re-schedule it accordingly.

IOS Push Notification for day after without reopening the app

so I'm using OCPrayerTimes to get a set of strings in an
I use this code to get the array,
NSMutableArray *prayerTimes = [prayerTime prayerTimesDate:[NSDate date]
latitude:3.1667
longitude:101.7000
andTimezone:[prayerTime getTimeZone]];
I then set notifications for each date in that array. Now my problem is that everyday those dates change so the user would have to re-open the app at midnight to get the new dates so the notifications would work.
Would I have to somehow run the viewdidload method at midnight everyday?
Thanks.
You can't automatically run your app every day, but you can schedule up to 64 local notifications, so you could calculate the prayer times for the next 12 days, and schedule notifications for all of them. This way the app wouldn't have to be opened every day.

Resources