Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Block code is your friend. I have created a class which does this for you</p> <p>Objective-C Block code. Create this class here</p> <p>Interface class</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "WebCall.h" @interface WebCall : NSObject { void(^webCallDidFinish)(NSString *response); } @property (nonatomic, retain) NSMutableData *responseData; -(void)setWebCallDidFinish:(void (^)(NSString *))wcdf; -(void)webServiceCall :(NSString *)sURL_p : (NSMutableArray *)valueList_p : (NSMutableArray *)keyList_p; @end </code></pre> <p>Implementation class</p> <pre><code>#import "WebCall.h" #import "AppDelegate.h" @implementation WebCall @synthesize responseData; -(void)setWebCallDidFinish:(void (^)(NSString *))wcdf { webCallDidFinish = [wcdf copy]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; int responseStatusCode = [httpResponse statusCode]; NSLog(@"Response Code = %i", responseStatusCode); if(responseStatusCode &lt; 200 || responseStatusCode &gt; 300) { webCallDidFinish(@"failure"); } [responseData setLength:0]; } - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"WebCall Error: %@", error); webCallDidFinish(@"failure"); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; response = [response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; webCallDidFinish(response); } -(void)webServiceCall :(NSString *)sURL_p : (NSMutableArray *)valueList_p : (NSMutableArray *)keyList_p { NSMutableString *sPost = [[NSMutableString alloc] init]; //If any variables need passed in - append them to the POST //E.g. if keyList object is username and valueList object is adam will append like //http://test.jsp?username=adam if([valueList_p count] &gt; 0) { for(int i = 0; i &lt; [valueList_p count]; i++) { if(i == 0) { [sPost appendFormat:@"%@=%@", [valueList_p objectAtIndex:i],[keyList_p objectAtIndex:i]]; } else { [sPost appendFormat:@"&amp;%@=%@", [valueList_p objectAtIndex:i], [keyList_p objectAtIndex:i]]; } } } NSData * postData = [sPost dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSURL * url = [NSURL URLWithString:sURL_p]; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:postData]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; if (theConnection) { self.responseData = [NSMutableData data]; } } @end </code></pre> <p>Then you to make this web call, you call it like this</p> <pre><code> WebCall *webCall = [[WebCall alloc] init]; [webCall setWebCallDidFinish:^(NSString *response){ //This method is called as as soon as the web call is finished NSString *trimmedString = [response stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; if([trimmedString rangeOfString:@"failure"].location == NSNotFound) { //Successful web call } else { //If the webcall failed due to an error } }]; //Make web call here [webCall webServiceCall:@"http://www.bbc.co.uk/" :nil :nil]; </code></pre> <p>See the setWebCallDidFinish method, it will not be called until the webcall has finished. </p> <p>Hope that helps!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload