Análisis de datos en una aplicación de iOS usando un analizador XML o JSON

I just wanted to clarify my understanding of web services consumed in iOS apps.

So, NSURLConnection is used to invoke a web method from an iOS app and to get the data returned by the web method. And once the data is received, we can use either XMLParser or JSON parser to parse that data, depending on what the web service is written in. Is that correct?

preguntado el 04 de julio de 12 a las 01:07

Yup. If you're using NSURLConnection directly (which is fine) then you get the bytes and can do what you need to with them, and parse them however you'd like. -

What would be the other way of doing this, if not NSURLConnection? -

NSURLConnection is the best way to go. It can be run synchronously or asynchronously and have nice delegate call back. But there is also method on NSData that can take a web URL to fill your NSData but it will block your thread so dispatch async. Must be use with it. -

If you're parsing JSON, you can try using the SBJSON library for objective-c. That's what I use and it works very well stig.github.com/json-framework -

1 Respuestas

Just to add onto what others have already said in the comments:

Ideally you'll want to always connect asynchronously, using NSURLConnection's Delegate which will not block the main thread. If you're not already aware, iOS will force quit applications that block the main thread for too long a time. Synchronous connections can be ok in certain instances, but I'd say over 90% of the time you'll want asynchronous.

That said, asynchronous connections cause their own set of headaches. If you need to have data before allowing your users access to the application interface, you'll need to throw up loading screens and then remove them. You'll need to manage canceling requests if the user moves to a different part of the application that starts a new request. You'll also want to determine if any ongoing requests need to be registered for background completion in the event the user backgrounds the app.

On the JSON parsing side of things, it's always recommended to use NSData when you can, as converting JSON from NSString adds a lot of overhead from NSString itself. I personally do NOT use Apple's provided JSON parser, but the excellent JSONKit as it's faster than even Apple's binary plist parser. It does however require that your JSON adhere very strictly to the JSON RFC, and that it be UTF-8/16/32 encoded, not ASCII. This is fairly standard amongst the faster JSON parsers available.

Avoid XML all together when possible. The built-in iPhone parser is a SAX-style parser only, which is obnoxious to implement. If you must use XML, take a look at Ray Wenderlich's XML parser comparison and choose an appropriate one. If you have a large XML document to parse though, SAX is probably your only option given the limited processing capabilities of iDevices.

--EDITAR--

One of the commenters mention SBJSON. I'd recommend against that entirely. It's slower than Apple's JSON parser (which has been available since iOS 5), and is much slower then JSONKit and several others by an order of magnitude. I specifically moved my current enterprise-level iOS Application off of SBJSON because of errors I was receiving in the JSON parsing as well.

Respondido 04 Jul 12, 03:07

Thanks a lot for your response. One more question, since you have experience with enterprise apps, how would you recommend handling user authentication and registration in iOS app? I want to create a test app, and plan to write everything from scratch. - loco99

That's a big question. I'm assuming you have a web app, either hosted or self-built, to handle storage of reg/auth related details. In an ideal world, your infrastructure would expose a RESTful API for handling everything. Authentication would be specifically handled by OAuthv2, but could also be done using session cookies. Registration would be SSL encrypted, and could be a simple as a POST with a number of data fields in the form. - Matt S.

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.