What's the best way to make persistent HTTPS connections in Objective-C with Cocoa Framework for iPhone with NSURLSession? I've read somewhere else that persistent connections are standard since HTTP 1.1. Some code example would be very appreciated. I need to achieve a simple data retrieval task (json encoded string)
best regards
stefan
Follow this link http://www.objc.io/issue-5/from-nsurlconnection-to-nsurlsession.html to find out, what "no persistent" means to understand "persistent".
I haven't used NSURLConnection or NSURLSession myself, but persistent is not a special setting. It means, that the same settings-data is used for connection/session.
Nothing about NSURLSession...
But I've found a tutorial here
http://www.raywenderlich.com/51127/nsurlsession-tutorial.
Related
I just added SSL to my backend framework (Django REST API) and I want my iOS application to talk to it. Do I have to do anything differently on the iOS side of my project? How do I tell Alamofire to encrypt the data its sending? Or Does it happen automatically?
The only difference is using https instead of http. I have the same setup at work, and originally thought I was going to have to delve into certificates. I started heading in that direction and then realized all my requests worked as soon as I stuck the "s" on the end.
I will say, while using NSStream, you do have to setup the stream to handle the certificate. I am doing this in another application, but that is below the URLRequest class. I am unsure of how low level Alomofire actually delves, but it will definitely handle everything you desire without doing anything differently.
Just update URLs inside your app to use "https" and you are done.
There are a wealth of resources for checking for a valid Internet connection with Swift (Reachability) and also ways to check the statusCode of the httpResponse when making an API call, but what is the "right" way to check and handle these errors (Internet not reachable, server 404) when dealing with an API heavy iOS app?
For example, when the app starts in the initial view (or AppDelegate I suppose) one can check for both and redirect to a "ServerProblemsViewController" that displays a message (or show an alert, though those can be dismissed). But what happens is someone is in the middle of using your app and the Internet drops or the server becomes unreachable? How would you handle this?
I am wondering if devs typically check for Reachability before EVERY API call and check the return status of EVERY API call, or somehow encapsulate that logic into a helper function?
How do experienced iOS devs deal with this situation?
I check for reachability on every call and then examine the response status code by type casting the NSURLResponse to an NSHTTPURLResponse (which has a statusCode property). And yes, my requests use the same base class to encapsulate all of the functionality--only creating derived classes when needed. You can use the reachability example code from Apple which has been ported to Swift and can be typically found on Github through the community. Simply reading through this repo will probably answer a lot of your questions about changes in reachability. Notifications FTW!: https://github.com/ashleymills/Reachability.swift
I am a beginner to ios development.
Can anyone tell me how to send data to server in xcode?
I have a requirement where I need to send device information to a server.
You need to first work out what kind of API the server you're talking to has exposed
Most modern web applications expose a Rest API (although I can only speculate as to what the server you mention is exposing). If Rest, then a good starting point should you not wish to write your own network layer is to use Restkit: https://github.com/RestKit/RestKit
If not Rest, then you need information on what the backend API is, and then go from there...
In it's most basic format you'll need to look at using NSURLRequest and NSURLConnection
NSURLRequest : Post data and read the posted page
http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/
Has anyone managed to get Server-Sent-Events (SSE) working nicely through AFNetworking? I know AFURLConnectionOperation has an inputStream property I can attach to, but the architecture of AFNetworking seems to be oriented around receiving a response, so I'm not sure what the best way to go about it would be.
Any on how to structure such a program would be appreciated. I'm also open to other iOS libraries, if they provide cleaner solutions.
AFRocketClient (built on top of AFNetworking 2.0) contains AFEventSource which is an implementation of the W3C Event Source API.
The combination of Server Side Events and JSON Patch is being referred to as Rocket.
We know that https is different with http by encrypting the content over http tunnel, simply speaking.
For the web browser, since user accept the permission from the popup alter dialog, the browser will get the keys from installed certificates and do the job so on.
For iOS client development, how to handle the digital certificate, and get the public key and encrypt the content via the public key ? thanks,
( By the way, AFnetworking is the famous open source project for iOS developer to handle the http stuff, but seems like that it does not handle the https. (correct me if wrong ) )
NSURLConnection and NSURLSession are the underlying classes used by all HTTP iOS libraries. These two classes does the HTTPS handling for you. Nothing for you to worry about.
Then, you can use libraries as AFNetworking or Hermod that work on top of these basic iOS classes.
AFNetworking is the most popular one.
Hermod is my preferred. It is a new library built on top of AFNetworking and its API is much simpler and easy to use. Also, it has built-in support for OAuth.
It's already done for you.
AFnetworking, the underlying iOS network library it's built on, NSURLConnection and pretty much every networking library (eg, ASIHTTP, etc) all handle the https protocol for you transparently.
Indeed, the very first line on the front page of the AFNetworking github page is an https example:
NSURL *url = [NSURL URLWithString:#"https://alpha-api.app.net/stream/0/posts/stream/global"];