Save changed data to database - iphone

In my app i am taking data from server in json format and making some changes in my data.i am able to download and save data in my app.But I am unable to post the data into the server.
I am using SQLITE and ASIHTTP from this operation.I want to save data on server on Submit Button click.
Any help for saving data into the server.Thanks

The best way to achieve what you want is to create a PHP based API on your server, call the API with post data containing the payload you want to save.
Once you have the post data on the server you can use PHP to parse out the information and inject it into a local database.
Tutorial...
To actually call the PHP API in your Objective C source on iOS I suggest using AFNetworking a nice networking framework for iOS: AFNetworking (This has recently been updated to version 2.0, and the framework has changed slightly, in this example I've linked to the 1.x version and this class only exists in that version).
You can use the AFHTTPClient class.
Assuming your instance of AFHTTPClient is called:
AFHTTPClient *client;
Then use it like this:
First create a dictionary containing your PHP API Parameters, e.g. your API command, and any data you want to pass up to the server:
NSDictionary *params = #{#"cmd":#"uploadData",
#"dataToUpload":[_dataModel someData],
#"someOtherDataToUpload":[_dataModel someOtherData]};
Then call the post function on the client like this. Note: it has completion blocks that will be called for success or failure.
[self.client postPath:#"/api.php"
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
//Success block call back (do some code here,
//update UI to say upload complete)
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Upload failed... Do some UI update here also maybe?
}
The rest you just do in PHP on your web server.

Related

Getting user's form data on iOS

I need to have my users fill out a form, and send me the information. Nothing fancy, just their name and email, and they'd be doing it willingly.
I looked into emailing the information to my account, but it seems like you have to pop the MFMailComposeViewController and let the user submit an email -- and I don't want to bother them with that.
I also tried a simple mailto url, like this:
NSString *url = #"mailto:example#example.com?&subject=Greetings from Cupertino!&body=Wish you were here!";
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
But it doesn't work on my simulator, or my iPhone, which is 4s with iOS 6.
I also looked into creating a google doc, and have the application send the user's info to its URL, but I'm assuming the result would be similar to the mailto URL?
So is there a good, simple way to do it?
So is there a good, simple way to do it?
Sure -- but not using mail. Use HTTP instead, and POST the data to a web server. If you need to collect the results by mail, you could easily create an e-mail message on the server and send from there, but it seems more likely that you'll just want to add the information to a database directly.
To use HTTP, you'll create a NSURLRequest with the relevant parameters and then send it using NSURLConnection. If you don't feel like digging into the URL loading system that iOS provides, there are a number of wrapper libraries that make it even easier. But for what you want to do, using NSURLConnection directly will be pretty straightforward.
Maybe you should have a look at these links :-
How to send mail from iphone app without showing MFMailComposeViewController?
Send mail without MFMailComposeViewController
Sending Email without using MFMailComposeViewController
MFMailComposer send email without presenting view

Using Restkit and Core data to Retrieve Images

I have a core data application using rest kit to retrieve data from a web service. The response i get from the webservice is a json string like this -
"nodeRef": "workspace:\/\/SpacesStore\/b1d51831-990d-47a8-a018-f1c1bg33f594",
"type": "document",
"name": "x.jpg",
"displayName": "x.jpg",
"title": "myTestProject",
(This is some meta data around an image. If i want to retrieve this image then i should access it using this url - http://x.co.uk/share/proxy/alfresco/api/node/workspace/SpacesStore/b1d51831-990d-47a8-a018-f1c1bg33f594/content/thumbnails/ipad1024?c=force) - note ive changed the URL slightly so you wont be able to access it. Also you need to authentication to access the URL. So i was wondering how i would go about getting this image and storing it locally on my SQLite DB? I can obviously access the URL but how exactly do i prompt it to download the image
What i did in this situation was to do a lazy load on the image with something like:
image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.absoluteUri]]];
NSData *imageData=UIImageJPEGRepresentation(image, JPGCompression);
Then store it. Outside of Restkit. This was a while ago, but it worked for me.
Basically extract the URL from restkit, do a lazy load when the image is presented on the screen, and thne store it back to core data (or wherever) when you have loaded it once.
Hope this helps.

how to do server authentication to download plist files from a password protected directory in iOS

I need to access some plist files from a remote server, which are in a password protected directory. In case of not using a password to access the directory, i use the simple code and it works:
NSString *query = #"http://www.sante.com/FFFF/privat/updateDates.plist";
NSURL *urlDates = [NSURL URLWithString:[query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *datesUrl = [NSDictionary dictionaryWithContentsOfURL:urlDates];
But in case of password protected directory, does it exist a way to treat it in xcode (without using NSUrlCOnnection, xmlparser because i don't want to parse the file while i can access directly by using dictionaryWithContentsOfURL)?
Edit: The point is that i just want to write some code to access/download the plist files (which are in a password protected directory of server) from the webserver to ios device. But i don't know how to. In case of not using password for accessing server directory, my above code works very well, but i need to access them in secured mode, and i just need to validate the webserver directory password programatically (hard coded).
If you need to deal with customising the download process by supporting authentication, then you need to stop using dictionaryWithContentsOfURL: and start using NSConnection because NSConnection is the API for customising URL downloads. Particularly, look at the delegate method connection:didReceiveAuthenticationChallenge: which is where you hook in to the authentication procedure.
You don't need to use an XML parser, because you have no need to parse the downloaded contents. Hand the data off to NSPropertyListSerialization once you've downloaded them and that can return an NSDictionary instance to you.

how to extract the url path from my url link in Xcode?

I have a URL link like "http://mobile/testing.php?action=2, when i have this link on my IE browser, it will redirect the link to "http://mobile/myimage.jpeg". It directed to my database server folder name, where my the file I stored called myimage.jpeg.
I am wondering I how can I can put "http://mobile/testing.php?action=2", establish the NSURL connection and let the Xcode learn this path "http://mobile/myimage.jpeg" so that I can extract the image.jpeg and store it into a list where this list can reflect all the files I had in my database.
Anyone can help? would there be any NSURL reference I can use? I did checkup on the pathomponent, but I don know how can I implement it.
If you want to know where it actually gets redirected to, then you'll need to use an asynchronous NSURLConnection and implement the -[<NSURLConnectionDelegate> connection:didReceiveResponse:] delegate method.
The NSURLResponse object passed to that method will actually be an NSHTTPURLResponse, which means you can:
Ask it for its -statusCode. If the code is a redirection code, then you know your original request was redirected, at which point you can...
Ask it for its -URL. This is the NSURL to which your original request was redirected. From here, you can get its -lastPathComponent to extract the "myimage.jpeg" bit.
If I've totally misunderstood your question, then you can probably just extract the -path from your starting URL and get the -lastPathComponent from that.

how to pass function name in url while using some web service in objective c?

I am a beginner in i phone development.I am,right now, using web service to fetch data on my page.now the thing is that i have 2 functions in the same webpage. now i want to fetch data from only 1 function at the time when my url is passed. so how can i pass the argument? note that,my main purpose is to convert data into json format.my URL that i run in web browser is like :
http://localhost/abc/webservices/mainPage.asmx?op=GetEmpDetail
how can i change its format,so that i can use it in objective c?my function name is getEmpDetail.
Thank you in advance.
Not Sure but try this out.
nsurl *url =http://localhost/abc/webservices/whatever/GetEmpDetail?Param1=value1&Param2=value2
url = [url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];