简体   繁体   English

iOS网站互动(NSURLConnection)设计/模式

[英]iOS website interaction (NSURLConnection) design/pattern

This is a question about how to design an app that interacts with a website. 这是一个有关如何设计与网站交互的应用程序的问题。

I have an app which queries a website for information, done with an asynchronous NSURLConnection (which I think is the preferred methode in iOS). 我有一个应用程序,它通过异步NSURLConnection (我认为这是iOS中的首选方法)来查询网站的信息。 The website in question does not have an api/web-service interface. 有问题的网站没有api / web-service界面。 Interaction is done like a browser would do, send request, receive answer, interpret the answer to determine next action. 就像浏览器一样进行交互,发送请求,接收答案,解释答案以确定下一步操作。

The site is also secured with a username and password and does. 该站点还使用用户名和密码进行安全保护。 So when I want to interact with the website (let's say, usecases like: "get a list of photo's", "download photo", "upload photo") I need to login first and when I'm finished I'm logging out. 因此,当我想与该网站进行交互时(例如,用例:“获取照片列表”,“下载照片”,“上传照片”),我需要先登录,完成后我要注销。

I'd like to write my functions like this (but I'm very open to suggestions, perhaps I'm looking at it the wrong way) 我想这样写我的函数(但是我很乐于接受建议,也许我看错了方向)

login
if login successful
  get list of photos
logout

Then after when the user asks for it I'd like to 然后,当用户要求时,我想

login
if login successful
  download photo
logout

The problem I'm running into is, every time you fire of an NSURLConnection , the reply is received in -connectionDidFinishLoading . 我遇到的问题是,每次触发NSURLConnection ,都会在-connectionDidFinishLoading收到答复。 So I need to keep some kind of state to know what to do next, was this login for getting the list of photos or was it a login to download a photo. 因此,我需要保持某种状态才能知道下一步该做什么,这是获取照片列表的登录名,还是下载照片的登录名。

Now, the problem is not that I cant get this to work, the problem is how to set something like this up cleanly and efficiently so that it can scale so that it can support 20+ or even 50+ usecases and still be easily maintainable. 现在,问题不是我无法解决这个问题,而是问题是如何干净,有效地设置类似的内容,以便可以扩展规模,使其可以支持20甚至50多个用例,并且仍然易于维护。 Stressing that not all usecases are as simple as the ones illustrated above. 强调并不是所有用例都像上面说明的那样简单。

An example of what I'm trying to achieve is for instance the facebook app. 我想要达到的目标的一个例子是Facebook应用程序。 That app also needs to login to facebook an support a whole range of usecases like posting an item, downloading posts, like a post, upload photos and numerous others. 该应用程序还需要登录到Facebook,以支持各种用例,例如发布项目,下载帖子(例如帖子),上传照片等。 How have they designed something like this? 他们如何设计这样的东西?

If the question is how to log in to your server, the implementation details will differ a little based upon how your server authenticates a "login". 如果问题是如何登录到服务器,则实现细节将根据服务器对“登录”进行身份验证的方式而有所不同。 One model is a simple HTTP POST. 一种模型是简单的HTTP POST。 The other model is authentication challenges as discussed in the URL Loading System Programming Guide. 另一个模型是“ URL加载系统编程指南”中讨论的身份验证挑战

If you don't know which style your web server has employed, you can generally figure it out if you try to access one of the secured files using a URL from your web browser. 如果您不知道Web服务器采用的是哪种样式,通常可以通过尝试使用Web浏览器中的URL访问其中一个受保护的文件来解决。 Your server is employing authentication challenges if you see a screen like this: 如果您看到以下屏幕,则您的服务器正在使用身份验证挑战:

认证挑战

If you don't see a web browser generated pop-up, but rather see something like a HTML "please log in" message, then you probably have to do the POST login process. 如果您没有看到Web浏览器生成的弹出窗口,而是看到类似HTML“ please log in”的消息,那么您可能必须执行POST登录过程。 Note, if your server is using this sort of process, if the success or failure of the login is reported via an HTML page, having your app decipher that is a little bit of a hassle. 请注意,如果您的服务器正在使用这种过程,则登录成功或失败是通过HTML页面报告的,那么您的应用程序解密就有些麻烦了。 If would be ideal if you had a version of the login page that provided a JSON or XML response, instead of a HTML response. 如果您有一个提供JSON或XML响应而不是HTML响应的登录页面版本,那将是理想的选择。 I don't know if you have the latitude to do that on your server, but it will greatly simply your iOS development if you could do it that way. 我不知道您是否有能力在服务器上执行此操作,但是如果您可以这样做的话,它将大大简化您的iOS开发。

If you're logging in doing a POST, see the SimpleURLConnections sample app for demonstrations on how to make simple POST (and GET) requests of your web server. 如果您要登录进行POST,请参见SimpleURLConnections示例应用程序,以获取有关如何对Web服务器进行简单POST(和GET)请求的演示。

See the AdvancedURLConnections sample app for examples of how to handle authentication challenges. 有关如何处理身份验证挑战的示例,请参阅AdvancedURLConnections示例应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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