xcode iphone 应用程序如何与服务器交互?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3269981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:33:26  来源:igfitidea点击:

How does iphone apps interact with server?

iphoneobjective-ccocoa-touchxcode

提问by David Gao

I am a new programmer who is new to iPhone development and server stuff. I have a lot of questions to ask.

我是一名新程序员,对 iPhone 开发和服务器方面的东西不熟悉。我有很多问题要问。

You don't have to answer all the questions; any help is appreciated!

您不必回答所有问题;任何帮助表示赞赏!

  1. How does iPhone apps interact with server?
  2. Is there a particular kind of server i should use to interact iphone app with server?
  3. If there is no particular kind of server then what kind of server can be used?
  4. What are their advantages and disadvantages?
  5. What should the iPhone app (which is the client) do in order to interact with the server?
  6. How does the server know which iPhone to send data to?
  7. What should the server do in order to interact with iPhone app (client)?
  1. iPhone 应用程序如何与服务器交互?
  2. 我应该使用某种特定类型的服务器来将 iphone 应用程序与服务器进行交互吗?
  3. 如果没有特定类型的服务器,那么可以使用哪种服务器?
  4. 它们的优点和缺点是什么?
  5. 为了与服务器交互,iPhone 应用程序(即客户端)应该做什么?
  6. 服务器如何知道将数据发送到哪个 iPhone?
  7. 服务器应该怎么做才能与 iPhone 应用程序(客户端)交互?

采纳答案by Dan Ray

Your best bet is to have your iPhone make web requests of a web server. Your iPhone app acts just like a web browser, making http requests to a web server, and parsing the response.

最好的办法是让你的 iPhone 向网络服务器发出网络请求。你的 iPhone 应用程序就像一个网络浏览器,向网络服务器发出 http 请求,并解析响应。

I'm building an app right now that hits PHP scripts I've written that do database work, etc, and return JSON objects. It's not fancy--I could have built a whole SOAP or RPC web service, but I didn't do that, it just makes GET requests with query-string arguments.

我现在正在构建一个应用程序,它命中我编写的执行数据库工作等的 PHP 脚本,并返回 JSON 对象。这并不奇怪——我本可以构建一个完整的 SOAP 或 RPC Web 服务,但我没有这样做,它只是使用查询字符串参数发出 GET 请求。

There are handy libraries you want to know about. Google "iPhone JSON" to find the JSON library written by Stig Brautaset, that's the one most people seem to be using. Also, rather than putting yourself through all the hoops that the iPhone's built-in web client framework requires, go get ASIHTTPRequest, a very powerful and MUCH simplified web client library.

有您想了解的方便的库。谷歌“iPhone JSON”以找到 Stig Brautaset 编写的 JSON 库,这是大多数人似乎在使用的库。此外,与其让自己经历 iPhone 的内置 Web 客户端框架所需的所有麻烦,不如去获取 ASIHTTPRequest,这是一个非常强大且非常简化的 Web 客户端库。

As a general rule, you want to do as much processing on the server as possible. For instance, there's a place in my app I'm searching for events happening within a user-specified range of their local coordinates ("within 10 miles of me"). I wrote PHP to build a latitude/longitude bounding box, and query from the database based on that. That's WAY faster than bringing a bunch of events down and then asking Core Location to calculate their distance from where I'm standing.

作为一般规则,您希望在服务器上进行尽可能多的处理。例如,在我的应用程序中有一个地方,我正在搜索在用户指定的本地坐标范围内(“距离我 10 英里内”)发生的事件。我编写了 PHP 来构建一个纬度/经度边界框,并基于此从数据库中进行查询。这比将一堆事件放下然后要求 Core Location 计算它们与我站立的位置的距离要快得多。

回答by Alan

You've asked quite a few questions so I'll try my best to answer them all:

你已经问了很多问题,所以我会尽力回答它们:

First, you need to be a bit clearer, what type of server are you talking about? Email server, web server, lolcat server, it depends.

首先,您需要更清楚一点,您在谈论什么类型的服务器?电子邮件服务器、网络服务器、lolcat 服务器,视情况而定。

At the basic level, the iphone communicates over the internet. The internet uses Internet Protocol, and there are two standard protocols built atop of IP: Transmission Control Protocol, and User Datagram Protocol. Each has it's own uses and functions.

在基本层面上,iphone 通过互联网进行通信。互联网使用互联网协议,有两个建立在 IP 之上的标准协议:传输控制协议和用户数据报协议。每个都有它自己的用途和功能。

TCP/IP and UDP/IP make up the backbone of internet communication.

TCP/IP 和 UDP/IP 构成了 Internet 通信的主干。

A more specific application protocol is built atop of these two internet protocols, with a specific format to a given application. For example, HTTP is the standard protocol for transferring HTML and other Web information between a web server to a web browser client, over TCP.

更具体的应用程序协议建立在这两个互联网协议之上,对给定的应用程序具有特定的格式。例如,HTTP 是通过 TCP 在 Web 服务器和 Web 浏览器客户端之间传输 HTML 和其他 Web 信息的标准协议。

So, your iPhone would use whatever protocol is required to commuincate with the server. For more common server communication, the iOS SDK provides methods to construct messages (for example if you wish to make an HTTP request to a web server, you can use initWithContentsOfURLto send a GET request).

因此,您的 iPhone 将使用与服务器通信所需的任何协议。对于更常见的服务器通信,iOS SDK 提供了构造消息的方法(例如,如果您希望向 Web 服务器发出 HTTP 请求,您可以使用initWithContentsOfURL发送 GET 请求)。

If you built a custom server, then you will need construct the required message protocol on the iphone, and send it to the server, using either TCP or UDP (whatever your custom server expects).

如果您构建了自定义服务器,那么您将需要在 iphone 上构建所需的消息协议,并将其发送到服务器,使用 TCP 或 UDP(无论您的自定义服务器期望什么)。