Can NSNotificationCenter be used between different apps to communicate?

无人久伴 提交于 2020-01-14 14:02:06

问题


An app can make internal use of NSNotificationCenter for different parts to communicate with each other, but can this be extended such that differing apps can use it to communicate?

Edit: sorry for the confusion about iOS versus OSX. I only added the iOS tag, I don't know how/who/why an objective-c tag also got added, it wasn't me.


回答1:


Sorry for a brief answer but in this case that's all there is to it - no they cannot.

You can register to handle a URL type and then launch pass some data that way.




回答2:


I think you need a server to handle the communication between two apps.

Say you want app A and app B to communicate with each other. Have both apps periodically report their "locations" to a server S. Also both apps should be listening to S.

Not sure how effectively apps running in the background can listen for data from a server.

Might be helpful to look into how iOS instant messaging apps work. Perhaps there's some open source projects that help with this (and can be adapted to your needs).




回答3:


Nick's answer is certainly correct, but if you're looking for an alternative to NSNotificationCenter, that works between processes, you can try using the Darwin notification mechanism in CoreFoundation.framework. This works between processes, if you use the Darwin notification center.

See example here on Stack Overflow

... and another one with an example callback

Note that I have not used this in any apps on the iTunes App Store. These APIs are not private, however Apple certainly can reject usage of whatever they like, even if it's not a private API. I just offer this solution as a possibility, without being able to predict App Store acceptibility, or even knowing whether you (or others reading this page) need to submit their apps to the App Store.

I'll also link to this similar question (currently unsolved)



来源:https://stackoverflow.com/questions/8527189/can-nsnotificationcenter-be-used-between-different-apps-to-communicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!