iOS External Accessory background mode and localhost sockets

Hello,


As I understand, iOS restricts background time for networking because of power consumption by WiFi radio. But what if I use lo0 interface (localhost). Is it possible to have External Accessory background mode and local TCP/IP server/client inside? In that case I suppose that app does not consume so much power since localhost is an internal loopback.
Regards,
Mikhail Kolesov
Senior iOS Developer, Noveo

Accepted Reply

Have you thought about doing this as an

NSURLProtocol
subclass? That lets you avoid networking entirely, which is going to make for easier and more efficient code.

Be aware that with

NSURLSession
the app has to explicitly opt in to your
NSURLProtocol
via the
protocolClasses
property. So this approach won’t work if the app doesn’t have control over the session, for example, if it’s using some higher-level API that’s wrapped around
NSURLSession
.

The best place to start with

NSURLProtocol
is the CustomHTTPProtocol sample code.

IMPORTANT Much of the complexity of that sample centres around the recursive dispatch, and that’s something you don’t need because you’re dispatching down to your EA code.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"


WWDC runs Mon, 4 Jun through to Fri, 8 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face.

Replies

As I understand, iOS restricts background time for networking because of power consumption by WiFi radio.

There’s a lot more to this than just Wi-Fi.

Is it possible to have External Accessory background mode and local TCP/IP server/client inside?

Probably not, but I’d need to get a better understanding of what you’re proposing before giving you a definitive answer. What problem are you trying to solve? Are there multiple processes involved? If so, what are those processes?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"


WWDC runs Mon, 4 Jun through to Fri, 8 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face.

> What problem are you trying to solve?
I'm implementing an iOS framework which allows our app to communicate with External Accessory device using general HTTP API over a lightning cable. The scinario is as follow. App uses URLSession to send all requests on localhost with a special port (i.e 1234). The framework runs mux/demux library working as TCP/IP server at localhost:1234. As output mux/demux library returns HTTP payload encapsulated in a custom protocol frame. That frame (just a buffer of data) is sent using External Accessory stream over a cable to accessory device. There is a backward path on accessory device with External Accessory low level staff, mux/demux library and HTTP application server at the top.
That works good in foreground. But for background I have only 3 minutes for localhost sockets. As you described here https://forums.developer.apple.com/message/253232#253232 . I have a background mode (External Accessory type). After that period sockets die and framework causes app to crash.


UPDATE: I've just tried to ask for additional time for "background" using beginBackgroundTaskWithName and looks like sockets work in background (tested several times, every test takes an hour). But I'm not sure if it's a side effect of having External Accessory background mode rights or iOS does not kill me because of using just localhost (without Internet).. So it's not clear if it's a guaranteed background time or not.

Regards,

Mikhail Kolesov

Senior iOS Developer, Noveo

Have you thought about doing this as an

NSURLProtocol
subclass? That lets you avoid networking entirely, which is going to make for easier and more efficient code.

Be aware that with

NSURLSession
the app has to explicitly opt in to your
NSURLProtocol
via the
protocolClasses
property. So this approach won’t work if the app doesn’t have control over the session, for example, if it’s using some higher-level API that’s wrapped around
NSURLSession
.

The best place to start with

NSURLProtocol
is the CustomHTTPProtocol sample code.

IMPORTANT Much of the complexity of that sample centres around the recursive dispatch, and that’s something you don’t need because you’re dispatching down to your EA code.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"


WWDC runs Mon, 4 Jun through to Fri, 8 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face.

No, I havn't. Actually I did't know about that class. Will try to look into

NSURLProtocol
and CustomHTTPProtocol sample code. Looks like it's exactly what I need in my case. HTTP over External Accessory.
Thanks for the tip!
Regards,

Mikhail Kolesov

Senior iOS Developer, Noveo

Can you able to access the External Accessory via "File provider extension" ?

Can you able to access the External Accessory via "File provider extension" ?

That’s a very different question from what’s previously been covered in this thread, so I recommend that you open a new thread for it (go here and click Start Discussion).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"