Working in background in iOS - ios

I'm an android developer and I've started learning iOS development - not so long ago. I have
an android app which I want to port onto iOS. My app has
a background service which tracks user geoLocation and sends it to my server then receives response from it and then writes this response to SQLLite, please tell me how much it is possible to make this on iOS? I know that iPhone can receive location updates in background since iOS4, but what about background network connections and SQLLite? Also is it possible to send local push notifications in background?

please tell me how much it is possible to make this on iOS?
iOS is a mature OS that comes with Cocoa Touch, a huge library of classes. So, yes it is possible. And the question of possibility shouldn't be attached with the word much. Either something is possible, or something is impossible. Don't take it as how much. If it is possible, you can do it.
how about background network connections and SQLite?
Of course, this is possible. Like android, there is a rule in iOS that only your main thread can interact with the UI, and all other heavy tasks should be attached with other threads. You will use Blocks to accomplish this kind of functionality.
Also is it possible to send local push notifications in background?
Yes, it is quite possible. Have a look at the following document.
Local and Push Notification Programming Guide
Do have a look at http://developer.apple.com/, here you find a lot of documents related to the tasks you have asked in your question.

Related

Appropriate background mode for Swift NIO client

I've created an iOS app, that connects to an embedded device using swift-nio-ssh. This app has no UI to control the device, as that is supposed to be done with a Watch app, using WatchConnectivity.
As it stands, I have to keep the iOS app in the foreground or the SSH client will die after a few seconds. Instead I'd like to be able to keep the proxy app running in background, screen locked or whatever else the user wants to do with their iPhone.
I've done research on background modes, but none of the available APIs seem to fit my use case.
Have I overlooked something?
If not, would I be able to abuse another API instead (Audio, VoIP or NetworkExtension from what I've gathered)?
If so, would the app still be able to pass the review process somehow?
Thanks!
You haven't overlooked something and this applies to everything, not just SwiftNIO-using apps. On iOS, if an app goes into the background then it will be stopped and network connections will die at some point.
The only way around that are the mainly VoIP exceptions you mention in (2) which you have to specifically request from Apple.

Android Services equivalent in iOS Swift

I am looking for something that is equivalent to Services in Android. As far as I have searched I haven't found anything useful. I found Background Fetch but according to my understanding it cannot be used long processes.
Here is what I want to use it for.
fetch a list of twitter user ids from our web server then auto-follow those usernames in background. As there are limits in Twitter API, so the process will be long possibly 3-4 hours. Is this possible on iOS?
There is no long-running background mode for continuous execution of apps in iOS.
This doesn't really sound like the right task for a mobile device. Running a 3-4 hour background job would be bad for battery life.
You can implement the function on a server somewhere and use push notification to send updates to the device.
An Android Service use case that is often cited is a background music service, which I have implemented as an exercise. It is not a given that a long running task actually will consume a lot of battery. Rather, it appears Apple is guaranteeing that such an event cannot happen through API design.

iOS wake up/interrupt the device with Swift

I hope this question is fit for SE, but if not please tell me where I could move it and I'll do so gladly. I'm currently building an iOS app and need to wake the device up/interrupt the current app when a "match booking request" is made, the behaviour I'm looking for is similar to what Skype uses when a call is inbound, the handset either wakes up or interrupts the current running app with the answer/end call interface.
I have never had experience with iOS, and the only solution I can think of is to poll the app and check for requests but I imagine this could be pretty intensive on power consumption, I was wondering if anyone could point me in the right direction as to what technique I could use to achieve this efficiently?
An app cannot run in the background the way you are expecting. Polling is not an option.
It looks like Skype is using Apple Push Notification Service to wake up the phone.

Chat app synchronization on background in IOS

I have a chat application developed by JS. I want to send PING to server once in a while. Its not a problem if app runs on fore ground. The problem is when user minimizes it or open another app. My app looses its focus and gets into suspended state.
I have following two use-cases.
To keep the chat session open I need to send PING to server (Its an IRC server) every X minutes even the app runs in background.
We also need to check for new messages (by ajax on a local http server) and add a local notification to the notification queue so when user clicks on it app can resume
I have found apple does not allow running apps in the background. if they allow they require special permission. I found some apps does it by requesting finite length execution time.
What is the best way to get highest possible background execution time? As a chat app can I request permission for voip, location or any other way ?
Note: the app will be running in an environment where there is no Internet. Hence push notification will not work here.
Update: After doing a lot searching I found background fetch. It seem background fetch will suite it. But still the problem remains, its not called in a timely manner.
This sounds like an interesting problem. From reading the various comments, it sounds like you want this to work when you're on a local network - so you have wifi, but the wifi router/base station isn't connected to the actual internet?
Because background refresh isn't going to be predictable - you'll never know when it is going to update - you might want to get creative.
You could look into exploiting iOS VOIP support, only without the Voice! Apple has some tips on VOIP here. VOIP basically uses something called SIP (Session Initiation Protocol), which is signalling layer of the call, and a lot like HTTP. It's this SIP layer that you want to take advantage of.
This isn't going to be terribly easy, but it should be achievable. Setup your app to use VOIP, and then look into something like PJSip as your SIP library. Then, on your local network have a SIP Server (I'm sure there are plenty open source implementations) that you can register your iPhone against (so your server knows where your phone is, pretending to be a VOIP phone). This should work, because it doesn't need to go through Apple as far as I am aware... And will run happily on your local network.
Then, the server can send a message via SIP to the handset, as if it were instigating a VOIP session. You app is awoken, gets the messages - ideally from the SIP message if possible - and then just doesn't start the session. SIP was designed just for creating sessions, not just VOIP. When I worked in Telecoms R&D (a long time ago) we were using it to swap between Text/Voice/Video, all using local servers.
You'll have to jump a lot of hoops to make this work, but it would be pretty awesome. I have never tried this actual use case - especially with iOS, but I'm fairly sure it will work. It is a bit of a fudge, but should get you where you need to go.
Good luck!
You can use something like PubNub to build this chat app with iOS using native Objective-C code, or with the Phonegap (Cordova) libs.
The beauty with using a real-time messaging network like PubNub is that when the app goes to the background, you can easily have the chat messages come in on APNS.
When the app is in the foreground, it can just receive them as the native (PubNub) message. And if it needs to "catch-up" with the messages it missed while in the background (but received via APNS), its trivial to implement.
Also, PubNub is platform agnostic -- so you can easily also use it on Web, Android, BB, Windows Phone, etc.
http://www.pubnub.com/blog/build-real-time-chat-10-lines-code/
http://www.pubnub.com/blog/html5-websockets-beautiful-real-time-chat-on-mobile-using-pubnubs-channel-presence/
https://github.com/pubnub/objective-c/tree/master/iOS
https://github.com/pubnub/javascript/tree/master/phonegap
geremy

IOS - How to run an app in Background?

I'm developping an app, that one one his tasks, is to send messages in tcp to a server. I want that the app continuos sending that tcp messages, even if the user quits the app.
I've that I could do that with:
"In your Info.plist set LSBackgroundOnly to 1."
But it don't work (I just tested on the simulator).
Thanks a lot for the help!
I'll just put Holex's comment into answer form to take care of this question. As he states, there are only 3 types of services allowed to run in the background of an application, location services, audio, and VoIP. A great read on what you can and cant do is the following http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html from Apple. Fairly detailed article of the structure of what happens when an application loses focus.
Alternatively however, if you don't plan on releasing through the app store, it is possible to fake a VoIP application and send/receive messages in the background. This is not a legitimate solution however, and really should be used for nothing other than if you need something quick, and extremely dirty.
iOS Voip Socket will not run in background That question, and any related ones are good places to start.

Resources