Getting an ARC warning for capturing an ojbect strongly - iphone

I'm using ARC and getting a warning saying Capturing 'request' strongly in this block is likely to a retain cycle.
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
self.appointmentArray = [responseString JSONValue];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"%#", error.description);
}];

I'm assuming request is declared somewhere before the blocks. You need to declare it as __weak, or set a second, weakly-declared variable to it.
This question is similar. Try this:
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
__weak ASIHTTPRequest *request_b = request;
[request setCompletionBlock:^{
NSString *responseString = [request_b responseString];
self.appointmentArray = [responseString JSONValue];
}];
[request setFailedBlock:^{
NSError *error = [request_b error];
NSLog(#"%#", error.description);
}];

Simply replacing:
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
with:
__weak ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
will suffice.

Related

HTTP Request with nested dictionary

I need to access a web service with a POST method, with nested dictionary to receive relevant data. But I keep getting empty response. What am I doing wrong?
NSURL *url = [NSURL URLWithString:#"http://appzander.com/gateway.php"];
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];
__weak ASIFormDataRequest *request = _request;
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:app.myLocation.longitude],#"longitude",
[NSNumber numberWithDouble:app.myLocation.latitude],#"latitude",
#"500000",#"radius",
#"1000",#"maxResults",
nil];
NSDictionary *requestDic = [[NSDictionary alloc] initWithObjectsAndKeys:
#"GetNearestStations",#"function",
#"false",#"debug",
paramsDic,#"params",
nil];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request appendPostData:[[SBJsonWriter new] dataWithObject:requestDic]];
request.requestMethod = #"POST";
[request setDelegate:self];
[request setCompletionBlock:^{
NSData *responseString = [request responseData];// responseString];
NSLog(#"Response: %#", responseString);
[self plotCrimePositions:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
// 6
[request startAsynchronous];
Use ASIFormDataRequest
NSURL *url = [NSURL URLWithString:#"yourUrlhere"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:email forKey:#"username"];
[request setPostValue:pasw forKey:#"pass"];
[request setDelegate:self];
[request startAsynchronous];
Solved- I wasn't sending the parameters properly:
NSURL *url = [NSURL URLWithString:#"http://appzander.com/gateway.php"];
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];
__weak ASIFormDataRequest *request = _request;
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:32.071738],#"longitude",
[NSNumber numberWithDouble:34.791295],#"langitude",
#"500000",#"radius",
#"1000",#"maxResults",
nil];
[request setPostValue:#"false" forKey:#"debug"];
[request setPostValue:#"GetNearestStations" forKey:#"function"];
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *paramsDicJSON = [jsonWriter stringWithObject:paramsDic];
[request setPostValue:paramsDicJSON forKey:#"params"];
request.requestMethod = #"POST";
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
[self plotBikes:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request startAsynchronous];

Is there any way to cache ASIFormDataRequest?

I'm using ASIFormDataRequest for Post data then parsing with JSON. ASIHTTPRequest has built in caching using [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];. Does anyone know if there is anything similar for ASIFormDataRequest?
Example:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/myapp/20110715/60b88126/load_dr_daily_schedule/%#/", [self getHost], [dateFormat stringFromDate:today]]];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[self addCurrentUserLoginToPostRequest:request];
[request setPostValue:[dateFormat stringFromDate:today] forKey:#"target_date"];
[request startSynchronous];
NSError *error = [request error];
NSString *responseString;
if (!error) {
responseString = [request responseString];
} else {
return NULL;
}
return [responseString JSONValue];
ASIFormDataRequest is a subclass of ASIHTTPRequest, so it was the same properties as ASIHTTPRequest.
Note that it won't cache POST requests though.

Problem with Response asihtttprequest

NSURL *url = [NSURL URLWithString:#"someurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:year1 forKey:#"year"];
[request setPostValue:appy_level forKey:#"appy_level"];
[request setPostValue:reasons forKey:#"reasons"];
[request setPostValue:country forKey:#"country"];
[request setPostValue:city forKey:#"city"];
[request setPostValue:sex forKey:#"sex"];
[request setRequestMethod:#"POST"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request startAsynchronous];
NSLog(#"response -%#",[request responseString]);
[self dismissModalViewControllerAnimated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
NSLog(#"%#",response);
}
any idea why this code return (null) i mean response is null? and this requestFinised: method isnt working even i write[requestsetDidFinishSelector:#selector(requestFinished)];
method too.I am confuesed right now.
The first NSLog won't work because you start your request as asynchronous.
You chould try putting NSLog(#"finished"); in -requestFinished: to check if page is loading or not. It can be an error in your server file (e.g. php fatal error).
Have you tried this:
NSString *response = [[NSString alloc] initWithData:[request data] encoding:NSUTF8StringEncoding];
NSLog(#"%#", response);

How to send a Get request in iOS?

I am making a library to get response from a particular URL with specified data and method type. For this, I am making a request with url. But when I set its method type, it shows an exception of unrecognized selector send in [NSURLRequest setHTTPMethod:]
I am setting it as
[requestObject setHTTPMethod:#"GET"];
Tell me what could be the problem. Also provide me the code if you have.
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL
URLWithString:serverAddress]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10
];
[request setHTTPMethod: #"GET"];
NSError *requestError = nil;
NSURLResponse *urlResponse = nil;
NSData *response1 =
[NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse error:&requestError];
NSString *getString = [NSString stringWithFormat:#"parameter=%#",yourvalue];
NSData *getData = [getString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getLength = [NSString stringWithFormat:#"%d", [getData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"https:yoururl"]];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:getData];
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
NSAssert(self.urlConnection != nil, #"Failure to create URL connection.");
// show in the status bar that network activity is starting
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
Make sure your requestObject is of type NSMutableURLRequest.
Simply call and use:
(void)jsonFetch{
NSURL *url = [NSURL URLWithString:#"http://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSError *erro = nil;
if (data!=nil) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&erro ];
if (json.count > 0) {
for(int i = 0; i<10 ; i++){
[arr addObject:[[[json[#"feed"][#"entry"] objectAtIndex:i]valueForKeyPath:#"im:image"] objectAtIndex:0][#"label"]];
}
}
}
dispatch_sync(dispatch_get_main_queue(),^{
[table reloadData];
});
}];
[data resume];
}

Iphone ASIHTTP setDidFinishSelector issue

i have the following code:
- (void)imageDownloaded:(ASIHTTPRequest *) request idDisco:(NSNumber *)iddisco
NSLog(#"%d",[idPlace intValue]);
}
And
NSURL *url = [NSURL URLWithString:[item objectForKey:#"image"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:[self performSelector:#selector(imageDownloaded:idDisco:)withObject:request withObject:disco.id_disco]];
But the compiler tells me that "passing argument 1 of setDidFinishSelector from incompatible pointe type"
It works fine but i don't know what i'm doing wrong.
Thanks
You have to pass #selector(someMethod:) there:
And the signature of the method should be
- (void)methodName:(ASIHTTPRequest *)request;
Look at this example:
- (IBAction)grabURLInTheBackground:(id)sender
{
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[[self queue] addOperation:request]; //queue is an NSOperationQueue
}
- (void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
}
- (void)requestWentWrong:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}