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