Web Service sending information to iPhone

空扰寡人 提交于 2019-12-25 04:14:50

问题


I created a couple web services for my app as follows. 1. Allows a host device to send its information to the web service and will store it in a sql database. 2. Allows a client device to see all nearby devices based on the information in the sql database.

I want the client to now be able to click one of the devices to connect to, which would then somehow link the two together. The question I have is right now my web services can only send a response to a device's request. What I want is if the host device maybe presses a button, it will send that info to the web service, which will then send some information to the client who previously connected.

How can I accomplish sending information from a web service to a device that hasn't requested anything?

Background info: I am using a php based web service and ASIHTTPRequest to send and receive the information on the iphone side.


回答1:


In 99% of cases, you're going to want to use polling. Think of any application that gets messages (email, Words with Friends, whatever). They all call the server and ask "are there any new messages for me?" You'll have to figure out the right frequency for your application.

In the rare case where you need near realtime communication, then you'll need to setup your app to run as a server. See here: What classes do I use to make an iPhone act as a server?. Obviously the battery and bandwidth cost of this is much higher.




回答2:


You could probably keep a persistent connection open, but this is going to break when network conditions change (such as when moving from network to network, on/off cellular, etc.).

The client could poll the server at rapid intervals, but this will not only incur a higher than desirable load on your server but, much more importantly, burn through the client device's battery. (Doubly so on 3G since the cellular radio has a fairly lengthy minimum power-up interval regardless of how small a network transaction might be.)

If you are talking about fairly sporadic updates (real-time updates really would call for the first option above, with appropriate error handling and reconnection if a TCP session is killed), you might consider Apple Push Notifications - provided that the notification payload is consistent with the APNS guidelines. They would behave almost exactly as you describe.



来源:https://stackoverflow.com/questions/10200082/web-service-sending-information-to-iphone

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