Achieve Inter-App Communication on iOS through sockets - ios

I am wondering how to do Inter-App Communication on iOS in iOS 7. It occurred to me that if I was the foreground app I could bind to sockets and act like a server, and if I was the background app (and had a background entitlement like audio) then I can connect to servers. So it seemed to me like it would be possible to do Inter-App Communication by agreeing on a port between 2 apps and simply switching which app binds to a port based on whether it is in the foreground or not.
Problem is, I can never seem to connect to localhost from the background, for example I have this code on a loop:
truct addrinfo *server_address;
int ret = getaddrinfo("localhost", "1666", NULL, &server_address);
int connection_id = connect(self.socketHandle, server_address->ai_addr, server_address->ai_addrlen);
And connection_id is always -1. I am unsure why I cannot connect given the bind process does not throw any errors.

Check this answer: iOS - Is it possible to communicate between apps via localhost?
Also, remember to make sure that you're background app is not stopped when the other app is entering in the foreground (by default the app will be stopped)
But generally speaking I don't think it's the good way to do interapp communication: the 'spirit' of iOS is to generally have only one app running in the foreground. So why do you need to communicate with a background app? I am quite sure that you can find a better way to achieve your result.

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.

How to keep http network alive when the app go into background?

How to keep http network alive when the app go into background? I have tried some method to do for this ,such as I used VOIP ,but when the app goes into background ,it just runs for 3 mins maybe more less.I need send a request once a min.My method is not useful ,so what should I do ?
You can't keep the network alive without a valid reason or apple will reject your app.
iOS offers several background modes that are allowed to keep the app alive, you have to enable this on the Capabilities -> Background Modes in your app target configuration.
If you do enable Voice over IP you shouldn't have an issue keeping it alive, but your app actually has to be doing that since apple is very strict about this.

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

Create an iOS App that runs as Web Server in background

I want to make an iOS application (for iPad specifically), which behave like a web server.
I saw the examples coming with COCOAHTTPSERVER, but i didn't understand something.
Is it possible for my application, while being in background, to receive a request from another application running in the same device?
If it is possible how can i do this?
Otherwise, if it is impossible, what is the meaning of a web server application, that can't work in background?
My exact problem is as follows: I need an iOS application that can receive a request from a javascript application, running on safari, in the same device. Can i create an application behaving like a web server in background (because safari must be in foreground), or there is another way to implement this?
You can run a web server in the background on iOS but unfortunately only for a limited time (up to 10 minutes max) and the OS can stop it at any time. See this tech note [1] for more info.
If that limitation is acceptable to you, you should use an already existing web server like GCDWebServer which handles background mode for you [2].
[1] https://developer.apple.com/library/ios/technotes/tn2277/_index.html
[2] https://github.com/swisspol/GCDWebServer#gcdwebserver--background-mode-for-ios-apps
This library may make your task much easier.
https://github.com/swisspol/GCDWebServer
No, iOS does not support web servers running in the background.
The reason Apple does this is that a web server has to listen for inbound network requests. Listening for inbound network requests requires that the radios be active all the time. Active radios drain the battery.

Any way to cheat in iOS to get networking to work in background?

I'm trying to build a hack so that I can echo incoming TCP packets to local UDP (to connect to 3rd party control surfaces that don't support TCP).
I don't care if Apple won't approve it, I only need to do this on my own iPads. I tried pretending that my app supported audio but that didn't work.
Is this posible (without jail breaking?)
I haven't actually tried this but you might be able to do it by setting the app up as a VoIP app.
From the developer documentation:
Rather than keep VoIP apps awake all the time, the system allows them to be suspended and provides facilities for monitoring their sockets for them. When incoming traffic is detected, the system wakes up the VoIP app and returns control of its sockets to it.

Resources