-4

Recently I have been trying to call a *.php file on a server and collect the response.I am trying to do this in xcode to make an ios app. I have tried to do it with a:

NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];

But the program crashs when i do this and it says that an exception was thrown. So i want to know how can i easy call a web-server and get a response. I am very new to ios development so you dont asume i know anything ;).

I have been looking through a lot of tutorial but i can't seem to make one work.

Alex Pelletier
  • 461
  • 5
  • 13
  • 1
    (As H2CO3 says always) I have nothing to do with XCode. – rptwsthi Jun 27 '13 at 05:52
  • 1
    This is a difficult question to answer. When you are asking for help diagnosing a crash you really need to provide an example of the error. The exception which is crashing your app tells you and the rest of us a great deal about what is going wrong. In addition the code sample you provided is so short that it probably does not include enough detail to resolve the problem. Please at least show the class an delegate method you expect this connection to call on completion of the request. – Jonah Jun 27 '13 at 05:58
  • Your question may closed. Your question is not a valid one and unclear too. You have to ask the question technical oriented in this forum only. My opinion is, Get the WebSite details as json or xml webservice format. And, Parse them into your applicaiton. For example, [take a look at here](http://www.raywenderlich.com/5492/working-with-json-in-ios-5) – Praveenkumar Jun 27 '13 at 05:58
  • There are plenty of examples over this question. take a look at this, this will surely help you. http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/ – iLearner Jun 27 '13 at 06:01
  • Sorry i always forget to say that it is for xcode and ios. – Alex Pelletier Jun 27 '13 at 06:04

4 Answers4

1

There are several ways of calling a web service in objective C. you can use ASHIHTTPrequest a good documentation for this is Here. Also I assume you have not found This tutorial since yet.

Also if you strictly want to use NSURLConnection. This can be your way. This is example of a simple login api where value of user id and password are bring send as header vales of request.

NSMutableURLRequest *theRequest   = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kWebServiceURL]];
[theRequest setValue:kWSReqH_GBLRequestMethodName  forHTTPHeaderField:kWSHFunctionName];
[theRequest setValue:userId forHTTPHeaderField:kWSReqH_GBLUserId];
[theRequest setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

//calling for request
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
1

So i might have asked the question wrong but this is exactly what i wanted:

https://www.youtube.com/watch?v=FXQiEfjiYns

This is a video by SimpleSDK. I tested to code on my *.php file and it worked great.

Alex Pelletier
  • 461
  • 5
  • 13
0

You cannot release the conn object right after sending the request. So make it a static or member variable in your class. Something like this..

@property (nonatomic, strong) NSURLConnection *connection;

...

@synthesize connection;
...

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"<url>"]];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.connection start];

and then implement the delegate methods like - (void)connectionDidFinishLoading:(NSURLConnection *)connection and other required methods.

Can you try this?

mohkhan
  • 11,925
  • 2
  • 24
  • 27
0

You are getting errors because you have to implement NSURLConnection correctly and implement delegate methods of it.

Go through these links you will find it how you can declare and structure it:

NSURLConnection delegate method http://yuvarajmanickam.wordpress.com/2012/10/17/nsurlconnection-basics-for-ios-beginners/

Example of it: http://cagt.bu.edu/w/images/8/8b/URL_Connection_example.txt

Community
  • 1
  • 1
Ponting
  • 2,248
  • 8
  • 33
  • 61