简体   繁体   中英

Achieve Inter-App Communication on iOS through sockets

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM