Posting data from phpMyAdmin to IOS app. [duplicate] - ios

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/

Related

Call A Webpage in iOS App with Xcode

How can I call a webpage (access it without showing it on the display) within an iOS app? I want to be able to have an iOS app, that when opened, will in the background load a webpage, that will execute a shell script.
I think you should use a NSURLConnection with your URL and ignore the data received.
Take a look to Apple Documentation:
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData dataWithCapacity: 0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (!theConnection) {
// Release the receivedData object.
receivedData = nil;
// Inform the user that the connection failed.
}
https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
See the UIWebView docs:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/#//apple_ref/occ/instm/UIWebView/loadRequest:
The request will load even if you dont attach the webview to the view hierarcy. Then implement this method in the UIWebViewDelegate protocol:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIWebViewDelegate/webViewDidFinishLoad:
When loaded, you can inject any (java)script you like with:
stringByEvaluatingJavaScriptFromString:
for a HTTP GET just do:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"YOUR_YOUR_STRING"]]
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//handle it if you like
}];

AFNetworking - add "image\jpeg" content type to AFImageResponseSerializer

I'm trying to load a picture from Facebook's source (a picture I've uploaded through my app and saved it's source).
I'm using AFNetworking to handle all my networking needs but when i'm trying to load a picture from:
http://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xfa1/v/t1.0-9/s720x720/10314676_10152739718934904_452946709678730535_n.jpg?oh=7e73d62e46c33e541e559e07e12bf275&oe=54B7E8CB&gda=1421866048_e364ab835ea15826e7ff28c8382ac085
(which opens in the browser, or in a regular http get request i'm setting in a http generator) but from the AFNetworking it always get a bad request (403) error..
I think that because the response from the server will be image\jpeg and i've ready that by default it doesn't work with AFNetworking..
How can i add it to the serialzer? or should i just write a new http request ?
imageView.imageResponseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"image/jpeg", #"image/jpg"];
I've ended up using:
-(void) loadPictureFromUrl:(NSString*)urlString {
NSURL* url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( !error )
[_httpRequestDelegate httpResponseReceived:nil responsedData:[[UIImage alloc] initWithData:data]];
else
[_httpRequestDelegate httpRequestReturnedError:nil withError:error];
}];
}
instead of fighting with AfNetworking - which is usually awesome but has an issue with what i'm trying to do

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.

AFNetworking alternative to sendSynchronousRequest for populating CoreData

I'm currently doing this when populating core data from a JSON file:
NSString *urlString = [value objectForKey:#"url"];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *dataResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[managedObject setValue:dataResponse forKey:#"image"];
Is there a better (asynchronous) way to do this with AFNetworking? What is the best method for this case? Does it have to be synchronous because we're dealing with CoreData?
UPDATE: Trying this now:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
[managedObject setValue:data forKey:#"image"];
}];
For some reason when I access the managed object later, the image attribute is always null, even though *data above is not null in the completion handler. The image gets saved fine in the synchronous method. What am I missing?
NSURLConnection can deal with async too.
The method that you can use is (iOS >= 5) is
+ sendAsynchronousRequest:queue:completionHandler:
If you need to target iOS < 5 then use the delegate pattern for NSURLConnection. A good wrapper for this can be found in NSURLConnection and grand central dispatch.
About Core Data, I would say it depends. If data you need to store is cheap, do it in the main thread. On the contrary you have three different ways to do it:
(1) use new Core Data queue-based API (iOS >= 5)
(2) kick off a NSOperation within a NSOperationQueue and do the long work in background
(3) use GDC
Pay attention to Core Data constraints (threads constraints) when you deal with (2) or (3).
Hope that helps.
P.S. If you want to know something else let me know.
There's a sendAsynchronousRequest:queue:completionHandler: message of NSURLConnection.

iOS Networking Library Suggestion [closed]

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 :)

Resources