iOS app runs in background and gets notification when a contact is inserted - ios

Is this possible to do? I know basic iOS programming (i've only been playing around with it for a couple weeks), but I was wondering if Apple gives you the ability to do this. I would like the app to receive a notification when a new contact is added and then execute a method from there. Any advice would be great. Thanks!
P.S. I would like the app to not even be running when it gets these notifications too if thats even possible

You mean, when a contact is inserted in the device's address book? No, there's no way to automatically receive a notification triggered by activity in the device's address book.
But with iOS7's added background modes, you could probably take advantage of the Background Fetch mode or Remote Notification mode to regularly check for updates to the user's address book. Background Fetch is technically supposed to be used to fetch data from a server, but you may be able to use it to fetch address book data in this case… Your app would still have to be running though, albeit in the background.

Related

Fetching data in a background and sending notification in iOS

What I want to do is: when my app is running in the background, I want to fetch data from API, every, let's say, 5 minutes. If some data changed I want to send a notification to the user. Is something like that possible in Swift? My problem is that I don't know how to run code in the background. I looked up background fetch but it looks like it was not made for this purpose (I can't be sure it will be fired after x amount of time with this). My app will be mostly user opening it once, setting some settings and then leave it in the background for notifications and never open it again. Any suggestions on what I can use for this purpose? I'd like to avoid creating a backend that will send data to the app, if possible.

Schedule UILocalNotification based on changes to Core Data

I'm making a simple app with a Today Widget extension that logs events.
The user can tap a button in the app or the related Today Widget to log an event. These events are saved with Core Data any time the button is pressed either place.
Whenever a new event is logged in the app, I run a function called updateLocalNotificationsFromCoreData(). It handles the setup of UILocalNotifications based on the most recent event in Core Data after clearing the appropriate existing notifications.
However, when a new event is logged from the Today Widget, I can't use this function because I need to register the Local Notification with UIApplication.sharedApplication().scheduleLocalNotification(), and UIApplication is not available in the Today Widget extension.
I realize I'll probably need do something unconventional or hacky to get this working, so I'm trying to evaluate possible approaches and come up with a relatively robust solution.
Basically, I want to find a way I can call my
updateLocalNotificationsFromCoreData() function right away any time a new event is logged.
If I can't do it every time an event is logged, an alternative would be to trigger the updateLocalNotificationsFromCoreData() function periodically (somewhat frequently) another way. Here are some solutions I was thinking about using, but I don't like any of them:
Do it in AppDelegate when the app is launched (or another state change)
One approach I'm thinking about is running my updateLocalNotificationsFromCoreData()function in AppDelegate somewhere, like didFinishLaunchingWithOptions.
The downside is that it would require the user to open the app periodically. If the user didn't open it much the notification behavior would be inconsistent. I'd prefer a solution where a user could interact with only the Today Widget and reliably get Local Notifications without ever opening the app.
Sync the events to a server and use Push Notifications
I've thought about syncing the data in Core Data to a server, then setting up Push Notifications to the user's phone based on that.
I don't like this, because I want the user to still be able to get notifications without an Internet connection. It also introduces a lot of extra overhead of syncing the data with a server.
Ping a server, and send a content-available Push Notification
When someone logs an event with the widget, I could ping a server. That server could send back a silent content-available push notification to trigger the app to run updateLocalNotificationsFromCoreData() in the background.
I found a similar question (Scheduling local notification from within a Today extension) where one answer proposes a similar solution. Unlike the previous solution, an Internet connection is not needed to receive the notifications, but an Internet connection would be required to make sure the notifications are up to date when a new event is logged.
Background fetch
I thought about using Background Fetch to fetch something arbitrary from a server, then run the updateLocalNotificationsFromCoreData(). This would be a way to trigger the update in the background, although it seems silly to fetch data if that data isn't being used, and seems like something for which an app could be rejected. There also seems to be a risk of the system not calling the background update regularly if the user doesn't open the app much and mostly uses the Today Widget.
Use background location updates
This seems like the dumbest approach, but I thought I would mention it anyway since I thought about it. I could use one of the low accuracy background location update modes to trigger updateLocalNotificationsFromCoreData().
It would require the user to allow location in the background, which would be hard to explain. And, it would require the user to at least move around a few blocks to trigger the function, which could provide an inconsistent user experience. Also, it would increase power consumption of the app for a silly reason.
I'd really appreciate fresh ideas about how I might be able to reliably schedule local notifications when Core Data changes on a device that doesn't have an Internet connection!
Or, if that doesn't seem possible, I'd appreciated feedback on which approach seems to make the most sense.
EDIT: I came up with a new solution. It's not ideal, but I think it's better than these other approaches I was considering. When someone taps the button to log the event, I launch the full app. It's annoying because I have all the data I need at that point to give the user feedback and log the event within the Today Widget without launching the app, but by launching the app I have the opportunity to check and schedule local notifications.
Also, in iOS 9 the annoyance on the user is slightly minimized because the system-wide "back" button will appear and let the user go back to the previous app easily once my app has launched from the Today Widget.
In the future I may try a solution where one of the server-based approaches above is used when an Internet connection is available, and I would then fall back to this system of opening the app only when the network connection is not available and I need to schedule the local notifications within the app.

How to run run a process frequently as a background service ios

I am working on ios app that used to read ticket data as a barcode scanner. It needs to upload data frequently to a web server, Like two or three times a day. I have done the sync function. I just wanted to run the function when the app is run in background.
This is not possible on iOS, Apple is not allowing any kind of background service on iOS.
The options you are left with is setting your apps background mode to fetch and implement application:performFetchWithCompletionHandler:. But it is totally up to ios if and when this method is called.
You could misuse one of the other background modes to keep your app open in the background, but Apple might reject your app for doing so. Also user might complain about you app draining battery.
What kind of data is that you need that you have to update it two to three times a day? I would say the when the app is opened by the user would be a good time to update, because this is when the user is expecting new data.
If you need to inform the user about some data changes you should be pull it in the app but a server should send a push notification to inform the user that there is new data.

ios xcode : checking live updates using a .net API

I'm trying to look for a way as to how I can notify a user of new updates based off of a .Net API that I created. Much like that notification you get on facebook, I just need to alert the user that something has been updated. So I'm thinking I need a function that runs in the background while my user uses the app combined with a NSTimer.
Based on my research (and on this question https://stackoverflow.com/a/14835300/639713) apparently you can only achieve such a thing for VOIP and location services. And that using push notification is the only way. Is it really the only option that I can use for such a need? Or are there any other ways?
Thank you for your time.
You have two options:
While your app is open, poll the server every so often to see if there are any updates to report.
This will work only as long as the user has your app open, and as you note, your app will only be allowed to stay open for a long time if it happens to be a VOIP or navigation app.
Use push notifications to push updates to the user’s device.
The disadvantage here is that you will need to write some server-side code to talk to Apple’s push notification servers (as described in this tutorial). This may or may not be a big deal in your particular situation, but it’s the only way to get data to the user when your app isn’t open.

iOS: Start uploading user location after request from server

I need to achieve this: I have an app, which wants to keep track of location of family members. Since I don't want to keep the GPS running all the time, I was thinking I could just send a request from server, when some family member wants to know my location.
But I ran into some problems:
I can't use PUSH Notifications, because those need to be confirmed (tapped on) and only after that the app knows something happened. (This would not be great, since kids probably wouldn't tap the push notifications)
The other option would be to keep the app running in the background, checking server for any news and if it found some request on server only then start the GPS tracking and upload coordinates to server. This sounds a bit better, but also battery draining.
Did anyone already try this? Is there any better way to this problem?
Thanks for any reactions! :)
Yes, I think the only option for you left is run Location Services in background.
For this in Plist set for Required background modes to App registers for location updates. This will enable you to send location updates as you want.
But note that this will only work when application is running or in background, and it will not run when you application is closed. If user closes your application, then you can fire push notification if you done get any updates from device.
Also, note that you need to mention in description of your application, that Application uses location services which will drain your battery, otherwise your application will not be approved.
Hope this info helps you..
We had similar problem in our application. We followed the approach that keep listening to push notification port, whenever we receive any notification, then check if it's for our application and then react accordingly.
We can have listening to port on long time to save battery draining.
Sorry cannot share any of the code with you, but I hope this might be some help.

Resources