开发者

iOS:Problems receiving UDP data while in background

开发者 https://www.devze.com 2023-03-16 00:51 出处:网络
I am trying to receive something on UDP while application is in background and I don\'t know what I am doing wrong or what callback (if any) is called when UDP data is available.

I am trying to receive something on UDP while application is in background and I don't know what I am doing wrong or what callback (if any) is called when UDP data is available.

I am using a BSD socket and I set the voip flags with the following code:

CFSocketContext socketCtxt = {0, self, NULL, NULL, NULL};
CFSocketNativeHandle ffsocket = (CFSocketNativeHandle)_socket;
//CFSocketRef socket = CFSocketCreateWithNative(kCFAllocatorDefault, ffsocket, kCFSocketDataCallBack, (CFSocketCallBack)SocketCallBack, &socketCtxt);
CFSocketRef socket = CFSocketCreateWithNative(kCFAllocatorDefault, ffsocket, kCFSocketReadCallBack, (CFSocketCallBack)SocketCallBack, &socketCtxt);

CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL, socket, 0);
//CFRunLoopRef loop = CFRunLoopGetMain();
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);

CFReadStreamRef readStream = NULL;

CFStreamCreatePairWithSocket(kCFAllocatorDefault, ffsocket,
                             &readStream, nil);

Boolean ret = CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
NSInputStream *inputStream = (NSInputStream*)readStream;   
[inputStream setProperty:NSStreamNetworkServiceType forKey:NSStreamNetworkServiceTypeVoIP];
//[inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];

[inputStream setDelegate:self];
//[inputStream schedu开发者_StackOverflowleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];

CFReadStreamOpen(readStream);

_socket is the BSD socket previously created.

While in foreground, SocketCallBack is called when data is available, but not called anymore in background.

Anyone has idea ?


While in foreground, SocketCallBack is called when data is available, but not called anymore in background.

Apps don't usually get CPU runtime while in Background on iOS. The system is multitasking but apps in background with no visible UI are put to sleep.

Background apps only get a bit of runtime, when notifications arrive for them that require processing by the app before they can be displayed to the user.

They also periodically get a bit of runtime if they support "background fetch" which allows them to pre-fetch app data over the network in the background in case the users has allowed that (there's a global system pref setting for it and further it can be configured per app). In that case the system will tell them, when they are allowed to perform such a fetch.

Since iOS 13, they can implement background tasks, which will allow the app to perform some background processing whenever the system allows them to do so (usually with a low priority, so they only run if the system has spare CPU time).

An app can register for background audio, allowing them to run code in background required to keep an audio stream playing, though what an app can do there is limited.

And finally, the system kills background apps at will that don't perform any of the tasks above, e.g. when it needs to free up memory, and in that case the app shortly gets runtime to process the termination notification, e.g. to perform some clean up before the process dies.


from apple's documentation UDP will not run in latest iOS.

0

精彩评论

暂无评论...
验证码 换一张
取 消