How can I create a single HTTP Get request for iPhone? - iphone

First of all, sorry for my posibly bad english...
I got a surely big stupid question...
In my enterprise have an automatic door system, that is opened with a HTTP GET request to a file.
Example:
http://ipaddress/rc.cgi?o=1,50
Where the o=number indicates the amount of seconds that the automatic door will run.
The is no need for authentification or nothing (that is made by LAN Radius).
So, the question is...
How can I make a single button (for example in the springboard) that when you touch it, runs the GET request?
You thing that it should be possible with NSURLConection ?
Thanks for all

I'm not sure if this is the best way of going about it, but this is how I've achieved something similar in my own app. Just create a new NSData object that hits the required URL, then release it if you don't need to do anything with the returned data:
NSURL *theURL = [[NSURL alloc] initWithString:#"http://ipaddress/rc.cgi?o=1,50"];
NSData *theData = [[NSData alloc] initWithContentsOfURL:theURL];
[theData release];
[theURL release];

Or just create an NSURLConnection to run asynchronously, then you don't have to worry about the UI hanging and if the delegate is set to nil, you can pretty much forget about it after you've run it.
NSURL * url = [NSURL URLWithString:#"http://ipaddress/rc.cgi?o=1,50"];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:nil];
[request release];
[theConnection release];

Related

Failing to connect to my WCF webget exposed service from objective c

This is a tough question.
When I press on the button, it is supposed to connect to a URL, which will initiate that method of my wcf service. However, when I debug, I noticed that the connection fails (like none of the delegate methods are called and stuff). I know that my WCF service works because when i type the URL in safari directly, it works perfectly, and performs that method. Here is my code:
- (IBAction)RBCButtonPressed:(id)sender {
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://10.1.51.55:8732/windows2/OnApplication?appName=RoyalBank.BankOfTheFuture.Surface.exe&directory=C:\\Program Files (x86)\\Bank of the Future"]];
NSString *urlString = #"http://10.1.51.55:8732/windows2/OnApplication?appName=RoyalBank.BankOfTheFuture.Surface.exe&directory=C:\\Program Files (x86)\\Bank of the Future";
NSLog(urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url ];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
if(connection)
self.valueReturned = [[NSData data] retain];
else
NSLog(#"data failed");
NSLog(#"connection failed");
[captureView addSubview:loader];
}
I'm not including the delegate methods cause they aren't called anyway.
It prints "connection failed."
UPDATE: When I use a method that takes in only one parameter, it works fine from both the browser and the device (the connection succeeds and the delegate methods are called). However, when there are two parameters, it works fine only from the browser. The connection always fails.
Possibilities - the use of the backslash confuses it somehow (?).
This is urgent and any help would be greatly appreciated.
EDIT: It will always print "connection failed" I realize, but I know that the connection fails because 1) the delegate methods aren't called and 2)the application does not turn on, which it will if the method is called.
If you check your NSURL object in the debugger, you'll find it's nil immediately after you try to initialize it. This is because it's malformed. Try URL encoding it first.
Notice the URL you pasted into safari changes to this:
http://10.1.51.55:8732/windows2/OnApplication?appName=RoyalBank.BankOfTheFuture.Surface.exe&directory=C:%5C%5CProgram%20Files%20(x86)%5C%5CBank%20of%20the%20Future
See all the %20s and %5Cs, etc.? That's because safari URL encodes it before sending the request. You must do the same.
Best regards.
It's failing because you are releasing connection right after you create it. If you're not using ARC, you could create it with autorelease, or just use ARC and forget about the releases.

how to load xml into memory in faster way?

I am new to objective-c. I load the whole xml string from server to an nsxml parser like below: but the problem is loding from url to memory takes much more time than parsing it. how can i solve that? (my app is very slow)
xmlString = [[NSMutableString alloc] initWithContentsOfURL:url usedEncoding:&NSUTF8StringEncoding error:&error];
NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
Run your XML Parser in different Thread.(this will not make your parsing faster)
How To Choose The Best XML Parser for Your iPhone Project
Basically NSXMLParser is slow. There is a possibility of finding a better parser for the specific need. A true stream parser might help if you do not need the entire DOM and validation.
Don't use
xmlString = [[NSMutableString alloc] initWithContentsOfURL:url usedEncoding:&NSUTF8StringEncoding error:&error];
Use a network library like ASIHTTPRequest to load the content from a URL and then parse it.
Loading directly from URL like you've done will hang your app till the data is loaded.
Download and install that library as given in the instructions (it's easy), then you can do
{
//in some function
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous]; //this will load URL in background
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
//parse the XML here
[self parseXML:responseString];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
You cannot load the xml faster from the url as it totally depends on your internet connection.
To keep your application smooth, you need to parse the xml in another thread. I answered a similar question in the following url
How to handle freezing programm at loading xml from bad url?
Also there are faster XML parser than the defaul NSXMLParser. There is a good introduction about them in the following blog post. So you can use any of these parser in place of NSXMLParser to improve the performance of your application.
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
I hope that it may clarify a little bit of your question.

UIWebView loadRequest causing a crash in simulator

So I'm trying to load a simple URL (http://www.google.com) into a web view. I had this in the application I was working with, but boiled it down to a basic application to remove other variables. I have an absolute minimal application setup, one view controller with a webview as its primary view. For code I have
-(void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = (UIWebView*)self.view;
NSMutableString *urlString = [[NSMutableString alloc] initWithString:#"http://www.google.com"];
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
NSLog(#"Sending web request to %#", urlString);
[webView loadRequest:urlRequest];
[urlRequest release];
[url dealloc];
[urlString dealloc];
}
And when it loads, I get an EXC_BAD_ACCESS crash within the Web Thread. I am unsure if this is a problem related to the fact that I am working in the simulator, or something I've just screwed up with the setup.
DO NOT CALL dealloc. Call release on url and urlString instead.
This is because other objects have references to those objects, and when you say dealloc, you are explicitly destroying them. This is causing the EXC_BAD_ACCESS, because when other objects try to access the url and string objects, they have already been destroyed.
This is the whole point of reference counting. YOU are done with those objects, so if you say:
[url release];
[urlString release];
You are declaring that. This decrements the count of references to those objects. But up above, you said:
NSURL* url = [[NSURL alloc] initWithString:urlString];
This means url probably has a reference to that string. So it would have retained it when you created it. So after you release the string, it does not get destroyed because url still has a reference to it. When IT is done with it, it too will release it, and then (if no one else has claim to the object), it will automatically be dealloced, because its count will have dropped to zero.
Always keep in mind who else might be using your objects when you are dealing with this sort of memory management. You can read more about it in Apple's docs.

Iphone: Need release using ASIFormDataRequest

i have one simple question, if i'm using ASIFormDataRequest when i need to release the request object?
NSURL *url = [NSURL URLWithString:#"url"];
ASIFormDataRequest *requestForm = [ASIFormDataRequest requestWithURL:url];
[requestForm addPostValue:[[NSUserDefaults standardUserDefaults] stringForKey:#"user"] forKey:#"user"];
[requestForm setRequestMethod:#"POST"];
[requestForm setDelegate:self];
[requestForm startAsynchronous];
Thanks
You need to remember that you are always responsible for releasing an object if the method from which you receive it contains new, copy, or init.
In this case, you don't need to release it. The ASIHTTPRequest class autoreleases it for you.
A quick search in the implementation file shows that it'll be autoreleased.
+ (id)requestWithURL:(NSURL *)newURL
{
return [[[self alloc] initWithURL:newURL] autorelease];
}
Your request is autoreleased in your code, so you don't need to release it (as others have said).
However, you are starting an asynchronous request - it'll complete/fail sometime in the future, and if it is associated with other objects which will get freed when the view exits you're potentially leaving yourself open to a crash. So I'd suggest you would want to make requestForm a property of your class (so when you assign the request to self.requestForm it will get retained for you), and explicitly release & nil it when the request completes.
If it's a very simple app with just one view you may get away without that though.

TTURLResponse is nil

I am trying to implement a simple TTURLRequest in my app. I'm pretty new to the Three20 framework. I mainly want TTURLRequest and TTImageView for the awesome caching stuff.
I have the following code in my delegate:
- (void)requestDidFinishLoad:(TTURLRequest*)request {
TTURLDataResponse *response = request.response;
// Stuff to process response.data
}
response is always nil. I can't figure out what I'm doing wrong. I looked in the Application Support directory and it's creating the cache file with the proper data, so I know it's getting a response. What am I doing wrong?
Debugged your code - you actually have to create a response object and add it to the request before sending your request:
NSString *url = #"http://twitter.com/statuses/user_timeline/samsoffes.json?count=1";
TTURLRequest *theRequest = [[TTURLRequest alloc] initWithURL:url delegate:self];
theRequest.response = [[[TTURLDataResponse alloc] init] autorelease];
[theRequest send];
One would think the request would be in charge of creating the response, but the Three20 stuff doesn't.
Mark Allen from revetkn.com posted a datasource implementation for TTURLRequest.
He also posted an example project where he assumes you have Three20 installed at ../ (relative to your project). It's here: http://revetkn.com/?p=72