HTTP post with UIWebview? - iphone

I am quite new to Objective-C coding, learning at College at the moment so. Anyways, how do I perform a HTTP post with a UIWebview so that I can update the view when needed for a different flyer to put put there etc.
I may not even need to use HTTP post doing this, but my co-partner who is coding for Android insists I may need to!
Any help?

I'm not sure what you mean by flyer. But if you want a web page to appear in a UIWebView you don't need to use HTTP post, you just pass the url of a page to load to the UIWebView, or if you already have html content etc. you can load that content directly.
You can make HTTP requests directly but you probably don't need to. What exactly is it you want to display in the UIWebView?

To make an HTTP Connection start by learning about NSURLConnection at iOS Developer Library
The first thing you're going to want to start with is making your NSURL objects then make your NSURLRequest with the parameters as well as the URL of your request. Then make a NSURLConnection and initiate the connection either asynchronously (preferred) or synchronously. If you want a synchronous (blocking) connection initialize it like so
NSError *error;
NSURLResponse *responseHandler;
NSData *response = [myNSURLConnection sendSynchronousRequest:myNSURLRequest, returningResponse:&responseHandler,error:&error];

Related

Allow trusted SSL Certificate in iPhone

I know how to implement "https" using NSURLConnection for async requests. It can be achieved using a couple of delegate methods. Now my problem is , I have my entire app working using sync request and it is impossible to change it into async at this point. Could anyone please tell me how to implement SSL Verification for synchronous request in NSURLConnection?
I have never found a way to. NSURLConnection sync request are extremely limited in functionality. I wrote a wrapper around NSURLConnection I call GPHTTPRequest. Should allow you do to sync request with the ability to allow self-signed and works a lot like ASIHTTPRequest, but is based on NSURLConnection instead of being the lower level CFNetwork framework. link is here:
https://github.com/daltoniam/GPHTTPRequest
also quick code example:
GPHTTPRequest* request = [GPHTTPRequest requestWithString:#"https://selfsignedURLYouwanttoaccess"];
request.allowSelfSigned = YES;
[request startSync];
NSLog(#"response: %#",[request responseString]);
also for the sake of options you can check out:
http://allseeing-i.com/ASIHTTPRequest/
https://github.com/afnetworking/afnetworking
but I would recommend GPHTTPRequest just because I wrote it and know how it works. ;) Any questions let me know.

ASIHTTPRequest and NSURLConnection

I need to work with ASIHTTPRequest in my app but i'm an absolute beginner in everything that concerns http, json, connections to server and stuff like that, so i decided to learn the basics first and i've got few really dumb questions for you. I have already googled looking for the answers, but all in vain
1)Does NSURLconnection have something to do with http protocol and http prequests? Is it useful for me to get acquainted with this class since i'm gonna work with http requests?
2)A huge problem of mine is that i do not have a web server yet to test my requests and i've been wondering if there is a free web server available for those who just want to upload/download data (images and may be something else) to/from it in order to learn basics of working with servers.
3)As far as i know development of ASIHTTPRequest library has been ceased. Does it mean that this library will become unavailable soon? Should i look for a different library? And btw is there a better one?
Thanks in advance
1.you use NSURLConnection to create a request object from a NSUrl. Here is an example, I used it to load a url to a UIWebView :
//Set the URL to go to for your UIWebView
NSString *urlAddress = #"http://google.com/";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the URL into the web view.
[aWebView loadRequest:requestObj];
You can read more about it here:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
NOTE : also look at NSMutableURLRequest, which is a subclass of NSURLRequest
2.To test, you can install apache on your computer and access it using http://localhost/.
If you are using windows, download WAMP : http://www.wampserver.com/en/
and if on mac, just search google how to install apache through terminal.
3.I havent used ASIHTTPRequest in a long time but as far as I remember it was a good library . if it is being ceased, then that probably means they wont be publishing anymore updates and/or fix bugs, so yes I think its better to use an active project that is being updated constantly. I also havent found any warning on their site about it, so I doubt they are stopping the project.
1) Yes NSURLConnection deals with HTTP and others protocol. But it isn't so simple to use than ASIHTTPRequest
2) Create a dropbox account and use the Public folder. Then right click on the file and select Dropbox > copy public link. You have a Webserver!
3) AFNetworking is really great

sending information from my iphone app to my computer

So I have a pretty simple problem, which I have no idea how to go about (kinda of new for everything here). I am developing an iPhone app that I intend to use only myself - so think small for now :)Let's say all my app is doing is tracking my location every hour. All I want to do, is to be able to read this information not on the iphone, say on a file on my computer. How can I send this information from my app to a personal computer? I am guessing that I will need to set up some server / database or something on my personal machine?
Can someone please help with a quick step by step on how to go about that? I literally have no clue where to start...
Thanks!
You just need a http server and then you cam create HTTP GET/POST requests to url's set up on your machine. You can use the responses to send data back to the device.
You'll need to have a listener somewhere to send your data to. For instance, you could set up a communication class in your project which would be responsible for serving as the medium between your applications. You would then set up a listener on a server, or your machine that would listen for requests.
iPhone > Communication Class > Web service
(.NET in this case)
You can use the NSURLConnection object to serve this purpose. E.g.
- (void) sendSomethingToServer:(NSString*)myData{
NSString*url = [[NSString alloc]initWithFormat: #"http://example.com/service.asmx/RecordData?myData='%#'", myData];
[self createRequest:url];
[url release];
}
http://example.com/service.asmx/RecordData being the location and method on your web service.
Here's a generic request method I created which sets the headers as a JSON packet.
- (void) createRequest: (NSString*)urlFormatted {
NSLog(#"Request Sent");
NSURL *url = [NSURL URLWithString: urlFormatted];
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL: url];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/json; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
}
And on the back-end, on your web server, you would have a method that recieves the data. Obviously you could use any technology you wanted on the server. In this example, I'm using .NET on Mono e.g.
[WebMethod(Description = "Generic Client Data)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string RecordData(string myData)
{
// Do something with data
}
Set up a Ruby on Rails server on your Mac. It's free and quite easy.
Use ASIHTTPRequest to send data to the Ruby server.
http://allseeing-i.com/ASIHTTPRequest/
If you just need to get the data back and forth, I believe you could use file sharing. Here's a tutorial on how to set that up:
http://www.raywenderlich.com/1948/how-integrate-itunes-file-sharing-with-your-ios-app
It would allow you to view the files in itunes and copy them back and forth when the iPhone is connected via USB. If all you need to do is to get a raw data file onto your computer, that would likely be a lot less overhead than having to build/run a full server just to transfer the file over.
Apple's can you help get started. Search for "networking" in the docs and develop a client on both sides which uses your own protocol.

UIWebView with offline access

I need a smart UIWebView (or a similar component) that can display/cache the full content of an HTML page on my server along with the images for offline access. And as soon as internet becomes available it will load the live version.
The app is a membership directory with member profiles. There's a main index and a page for each member.
Can you guys guide me to the right path on how to develop something like this? or point me to an existing library.
Thanks for your help I'm to new iOS development.
Two paths I can think of:
Wrap UIWebView, either in a subclass or a category, to encapsulate the loading logic
If network is online, load the URL directly and caching it using NSCache with the URL string as the key. NSURLConnection or something like EGOHTTPRequest will make loading easy.
If network is offline, check if the data exists in the cache. If so, use the -loadData: method to pull the content out of the cache and render it in the WebView. If it doesn't exist, display an error
Use a caching HTTP library, like ASIHTTPRequest to load the data. It can cache data it has loaded internally, and serve those cached results on subsequent requests if the network is offline.
ASIHTTPRequest looks a lot better on the surface, however there is a possible downside: It is a completely reimplemented URL loading system, based on very low level constructs (CFNetwork). So as Apple makes improvements to their high level libraries (NSURLConnection, NSCache) ASIHTTP doesn't get those advancements. Also, since so much of it's code is in Foundation, it will be a pretty major project to port it to ARC when that becomes generally available.
To load a local file on a UIWebView use fileURLWithPath
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"] isDirectory:NO]]];
It sure seems like you need to look at ASIWebPageRequest, which lets you cache whole pages offline:
http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest

NSURLConnection with delegate vs initWithContentsOfURL: with Grand Central Dispatch

I am writing an application which downloads very much JSON data from the internet. My app needs to parse this JSON data and afterwards return the retrieved objects to a self-made delegate.
Now I was wondering if I could better use NSURLConnection or a combination of Grand Central Dispatch's dispatch_async with initWithContentsOfURL: (using GCD to prevent hanging).
Using Grand Central Dispatch is much easier to implement, especially because I don't need delegates nor NSMutableData nor all other variables that are used temporarily. But of course, everything with pros has its cons.
What are the advantages of NSURLConnection over initWithContentsOfURL with GCD?
You can target iOS 3.x.
You can use a customized NSURLRequest that, e.g., asks for the results of an HTTP POST—the NSData factory only does basic GET when passed an HTTP URL.
You can track the download's progress with your NSURLConnection delegate.
That's it off the top of my head. :)