简体   繁体   中英

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). The website in question does not have an api/web-service interface. 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 . 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. 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. 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. 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. The other model is authentication challenges as discussed in the URL Loading System Programming Guide.

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. 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. 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. 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. 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.

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.

See the AdvancedURLConnections sample app for examples of how to handle authentication challenges.

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