iOS Networking Library Suggestion [closed] - ios

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 months ago.
Improve this question
I would some suggestion about the choose of an iOS networking library for my app.
My needs are:
Send asynchronous request (GET and POST) and if the network is down display a UIAlertView to inform the user of the error.
Send simple synchronous request (GET) and if the network is down do the same of the above point.
Does anyone have some lib to suggest? (except ASIHTTPRequest that is no longer supported) Possibly, if this lib have some nice doc is better. I'm an iOS beginner.
Thanks for help in advance.

I've heard good things about RestKit https://github.com/RestKit/RestKit
There is a good list of alternatives at the bottom of this blog post. http://allseeing-i.com/[request_release];
I've only used ASIHTTPRequest before, and even though it's not longer being developed by Ben Copsey, it looks like he's still merging pull requests and stuff. It's in use by so many people, I wouldn't be surprised if the community picks it up. It will probably be safe for at least another version of iOS.

You don't really need a library for this.
To send a synchronous GET request:
//set up the GET URL and params
NSURL *getURL = [NSURL URLWithString:#"http://somesite.com/somepath?foo=bar"];
//create the request
NSURLRequest *request = [NSURLRequest requestWithURL:getURL];
//get the response
NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
To send a synchronous POST request:
//set up the POST URL and params
NSURL *postURL = [NSURL URLWithString:#"http://somesite.com/somepath"];
NSString *postParams = #"foo=bar&hello=world";
//create the request - this bit is the same for every post
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:postURL];
[request setHTTPMethod:#"POST"];
[request addValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSData *data = [postParams dataUsingEncoding:NSUTF8StringEncoding];
[self addValue:[NSString stringWithFormat:#"%i", [data length]] forHTTPHeaderField:#"Content-Length"];
[self setHTTPBody:data];
//get the response
NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
In either case if responseData is nil or error is not nil, present an alert using the following:
[[[[UIAlertView alloc] initWithTitle:#"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease] show];

As stated in the question he is also looking for asynchronous requests.
I would suggest AFNetworking or GCNetworkKit (the last one is my own). Both libraries are very easy to use and yet powerful.
I don't think AFNetworking provides a synchronous network request though. Well at least mine doesn't. You shouldn't do that anyway.
Both libraries support error handling. There should be no problem on implementing an UIAlertView.
You can find them on GitHub. Just search for them. Hope this helps :)

Related

Issue with NSURLSession and Content-Type

I am trying to upload a photo to a webservice, and I am using the following code:
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:imageToUpload], 0.5);
// 1
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
[config setHTTPAdditionalHeaders:#{#"Content-Type":#"multipart/form-data"}];
// 2
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
// for now just create a random file name, dropbox will handle it if we overwrite a file and create a new name..
NSString *urlString =[NSString stringWithFormat:#"https://ABC.co.uk/photos?api_key=MYAPIKEY" ];
NSURL *webUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:webUrl];
[request setHTTPMethod:#"POST"];
// 3
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:imageData];
// 4
// uploadView.hidden = NO;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// 5
[uploadTask resume];
I receive an error 500, and I'm not sure what to do.
I have tried implementing AFNetworking, body data appending like This, This, and This. Yet, no luck.
The code above is from Ray Wenderlich's website: Here.
All I need to provide the server is this:
file (the image)
API Key (I tried using the NSMutableURLRequest's addValue:forHTTPHeaderField: but it didn't work, so I'm using it in the URL. I know it's not the best practice in terms of safety, but I'm getting so desperate as I've been working on this for over 10 hours! One step at the time!)
The Content-Type to be multipart/form-data
That's all I need to provide, and yet I'm not getting anywhere!
Can anyone please help?
Thank you in advance
UPDATE 1: So I made some alterations to the URL structure, and added the API key as a dictionary in HTTPBody, and now I am getting error 403.
It seems really weird that apple has not made any effort to serialise this part of connection. Anyway. I really appreciate anyone helping out!
Try setting the Content-Type in the request as well:
[request addValue:#"multipart/form-data" forHTTPHeaderField:#"Content-Type"];

Posting data from phpMyAdmin to IOS app. [duplicate]

This question already has answers here:
iOS Xcode connecting to MySQL database
(4 answers)
Closed 8 years ago.
I have seen a lot of tutorials how to connect IOS application with external database but none of them doesn't really show how to read data from phpMyAdmin. My idea is to read data from phpMyAdmin and show it in UITableView. Can someone show me some tutorial how to make my idea happen, i would really appreciate that.
Basically, you can use a NSURLConnection (iOS7+: NSURLSession) to get the output of your service running on the server:
NSString *url = #"http://example.com/path/to/service.php";
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSError *error;
if (data) {
// process the data...
}
}];
another great tutorial: http://codewithchris.com/iphone-app-connect-to-mysql-database/

How to connect to EC2 instance within an iOS App

I have an AWS EC2 instance and an app that I created. The app is for people who get migraines (tracks info, tells them what their trigger(s) are). Now I want to be able to send user input from my application to a server so that I can see trends. I am having difficulty connecting to the server and finding out how I would be able to write files to the server.
I have written this method:
- (void) sendDataToServer:(NSString *)url :(NSString *)key : (NSString *) content{
// define your form fields here:
//NSString *content = #"field1=42&field2=Hello";
NSString *address = [NSString stringWithFormat:#"ssh -i %# %#", key, url];
NSLog(#"%#", address);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:address]];
[request setHTTPMethod:#"POST"];
NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:contentData];
NSString *postLength = [NSString stringWithFormat:#"%d",[contentData length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
// generates an autoreleased NSURLConnection
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn){
NSLog(#"connection");
}
//[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//[self doSomethingWithData:data];
if (error){
NSLog(#"ERROR");
}
}];
}
I put the private key from my key pair into the app. How would I use that to connect? Should I not be using my private key? Should I be doing this differently?
Should I be doing this differently?
Absolutely, 100%, YES. You don't want to let people SSH into your server, especially by embedding your private key into an app binary. It's crazy easy for someone to get it, then wreak havoc upon your server.
Don't do this.
Instead, I would get a web server like Apache running on your instance (it's trivial), and write an application (in PHP, Rails (with Passenger), Python, whatever) that saves files to the server's hard drive. You'll also want to get an Elastic IP address so that it stays constant, as ashack mentioned.
In your iOS app, you'll want to send a POST request to your server. See Sending an HTTP POST request on iOS, it's essentially what you're doing now.
Don't publish your private SSH key. It's private for a good reason.

POST JSON data from iOS to mssql Server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can someone please tell me what is the best way to create web service that sends JSON to MS SQL server.(php or .net POST web service?)
I want to host the web service on IIS if possible.Links of tutorials will be very useful.
Many Thanks.
//Create the request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"your script name.php"]];
// create the Method "GET" or "POST"
[request setHTTPMethod:#"POST"];
//Pass The String to server
NSString *userUpdate =[NSString stringWithFormat:#"artist_email=%# ",your string Name,nil];
//Check The Value what we passed
NSLog(#"the data Details is =%#", userUpdate);
//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
//Apply the data to the body
[request setHTTPBody:data1];
//Create the response and Error
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(#"got response==%#", resSrt);
if(resSrt)
{
NSLog(#"got response");
}
else
{
NSLog(#"faield to connect");
}
I would recommend using ASP.NET Web API it supports both JSON and XML out of the box and the development is relatively easy as you don't have to manage the parsing of JSON data to and from C# objects, you can then just use Entity Framework to communicate with the database.
Start with : http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations
And: http://www.entityframeworktutorial.net/EntityFramework4.3/Introduction.aspx I think this is a bit out dated but the core will remain the same

Reusing an https connection on iOS

I've written a short iOS application that contacts a server via the usual:
NSString *URLString = #"http://mysite/script.php?foo=1";
NSURL *getURL = [NSURL URLWithString:URLString];
NSURLRequest *getRequest = [NSURLRequest requestWithURL:getURL];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:getRequest returningResponse:&response error:&error];
This works OK, but as I access the web site relatively frequently with various values of foo, and as we plan to migrate to https, people have raised the question about the overhead on the phone of setting up and tearing down the NSURLConnection every time. I see this answer saying that the class handles caching, but is that true for https too?
According to my tests, yes, it does appear to be true that the NSURLConnection class does caching of https to some extent. I did like the look of AFHTTPClient as mentioned in the comment, but due to various issues I was unable to use it in this project, however.

Resources