AFNetwork and GoogleAPI returning NULL - iphone

After being very disappointed with CLGeocoder, I decided to use the GoogleMaps API instead.
I have designed the call as following, using AFNetwork :
AFHTTPClient *new = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:#"http://maps.googleapis.com/"]];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"thorsgade",#"true", nil] forKeys:[NSArray arrayWithObjects:#"address",#"sensor", nil]];
NSMutableURLRequest *req = [new requestWithMethod:#"GET" path:#"maps/api/geocode/json" parameters:dict];
AFJSONRequestOperation *call = [AFJSONRequestOperation JSONRequestOperationWithRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSArray *geos = [JSON objectForKey:#"results"];
DLog(#"Got result : '%#' %# from %# %# %#",JSON,geos,[NSHTTPURLResponse localizedStringForStatusCode:response.statusCode],response.allHeaderFields,request.URL.description);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
DLog(#"Failed %# %#",error.localizedDescription,request.URL.description);
}];
[call start];
I get this feedback:
Got result : '(null)' (null) from no error {
"Cache-Control" = "public, max-age=86400";
"Content-Encoding" = gzip;
"Content-Length" = 1603;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Fri, 07 Dec 2012 08:51:58 GMT";
Expires = "Sat, 08 Dec 2012 08:51:58 GMT";
Server = mafe;
Vary = "Accept-Language";
"X-Frame-Options" = SAMEORIGIN;
"X-XSS-Protection" = "1; mode=block"; } http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade
Null result, but no errors. The content is recognized in the headers as JSON, but the raw JSON is null.
The annoying thing is that if I open http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=thorsgade in a browser, i get plenty of results.
So far i have tried:
Flicking the sensor booleon true/false.
Faking the user-agent to be regular safari.
Use POST instead of GET.
With no luck...

If the problem persists, I would recommend using MKNetworkKit instead
Here is my solution -
GoogleGeocodeApi.h
//GoogleGeocodeApi.h
#import <Foundation/Foundation.h>
#import "MKNetworkEngine.h"
typedef void (^JsonResponseBlock)(NSDictionary *);
typedef void (^ErrorBlock)(NSError* error);
#interface GoogleGeocodeApi : MKNetworkEngine
-(MKNetworkOperation*) geocodeWithAddress: (NSString *) address
onCompletion:(JsonResponseBlock) completionBlock
onError:(ErrorBlock) errorBlock;
#end
GoogleGeocodeApi.m
//GoogleGeocodeApi.m
#import "GoogleGeocodeApi.h"
#implementation GoogleGeocodeApi
-(id)init
{
if (self = [super initWithHostName:#"maps.googleapis.com" apiPath:#"maps/api/geocode" customHeaderFields:nil]) {
}
return self;
}
-(MKNetworkOperation*) geocodeWithAddress: (NSString *) address
onCompletion:(JsonResponseBlock) completionBlock
onError:(ErrorBlock) errorBlock;
{
MKNetworkOperation *op = [self operationWithPath:[NSString stringWithFormat:#"json?sensor=true&address=%#", address] params:nil httpMethod:#"GET"];
[op onCompletion:^(MKNetworkOperation *completedOperation) {
NSDictionary *responseJSON = [completedOperation responseJSON];
if (responseJSON && [[responseJSON objectForKey:#"status"] isEqualToString:#"OK"]) {
completionBlock(responseJSON);
} else {
NSDictionary* errorDictionary = #{NSLocalizedDescriptionKey :#"Google geocode failed!"};
NSError *error = [NSError errorWithDomain:#"Failed response" code:100 userInfo:errorDictionary];
errorBlock(error);
}
} onError:^(NSError* error) {
errorBlock(error);
}];
[self enqueueOperation:op];
return op;
}
Somewhere in code
GoogleGeocodeApi *gma = [[GoogleGeocodeApi alloc] init];
[gma geocodeWithAddress:#"thorsgade"
onCompletion:^(NSDictionary *responseJSON) {
NSLog(#"Geocode succeeded: %#", responseJSON);
} onError:^(NSError *error) {
NSLog(#"Geocode failed with error: %#", [error localizedDescription]);
}];

Related

Upload Photo from iphone app to rails server

I am building an iphone app that lets users upload photos to a rails-backed web service.
I am testing the app on my device and I am able to take a photo then post it- however the photoData is returned as null.
This is the xcode log after creating a post:
post = {
content = Test;
"created_at" = "2013-08-02T19:15:05Z";
id = 2;
photo = {
thumb = {
url = "<null>";
};
"thumb_retina" = {
url = "<null>";
};
url = "<null>";
};
};
success = 1;
}
What am I missing from the implementation to actually post the data to the server?
On the rails side I am using carrierwave and miniMagick as uploaders and using :fog storage to save everything to S3. (That is the goal at least- but that isn't happening.)
My post model has these properties:
#property (nonatomic, strong) NSString *content;
#property (nonatomic, strong) NSString *thumbnailUrl;
#property (nonatomic, strong) NSString *largeUrl;
#property (nonatomic, strong) NSData *photoData;
The save method is like this:
- (void)saveWithProgressAtLocation:(CLLocation *)location
withBlock:(void (^)(CGFloat))progressBlock completion:(void (^)(BOOL, NSError *))completionBlock {
if (!self.content) self.content = #"";
NSDictionary *params = #{
#"post[content]" : self.content,
};
NSURLRequest *postRequest = [[APIClient sharedClient] multipartFormRequestWithMethod:#"POST"
path:#"/posts"
parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:self.photoData
name:#"post[photo]"
fileName:#""
mimeType:#"image/png"];
}];
AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:postRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite;
progressBlock(progress);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (operation.response.statusCode == 200 || operation.response.statusCode == 201) {
NSLog(#"Created, %#", responseObject);
NSDictionary *updatedPost = [responseObject objectForKey:#"post"];
[self updateFromJSON:updatedPost];
[self notifyCreated];
completionBlock(YES, nil);
} else {
completionBlock(NO, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completionBlock(NO, error);
}];
[[APIClient sharedClient] enqueueHTTPRequestOperation:operation];
}
And the actual method that gets called onSave in the photoViewController is this:
- (void)save:(id)sender {
Post *post = [[Post alloc] init];
post.content = self.contentTextField.text;
post.photoData = UIImagePNGRepresentation(self.imageView.image);
[self.view endEditing:YES];
ProgressView *progressView = [ProgressView presentInWindow:self.view.window];
if (location) {
[post saveWithProgressAtLocation:self.locationManager.location withBlock:^(CGFloat progress) {
[progressView setProgress:progress];
} completion:^(BOOL success, NSError *error) {
[progressView dismiss];
if (success) {
[self.navigationController popViewControllerAnimated:YES];
} else {
NSLog(#"ERROR: %#", error);
}
}];
}
The problem is that the photoData is not saved to the server. Any ideas why?
Initially I had a precompiled default photo in rails: assets/images/nophoto.png - but any time I took a picture, this default would override the new photo and get posted, which is obviously not what I want. So I deleted that, and now I am getting null.
Any ideas?
Convert image to Data then Encode to Base64 String the save that string in server and then when retrieving from server, decode Base64 String to data and use [UIImage imageWithData: data];

Twitter search for hashtag

I'm trying to parse tweets using Twitter Framework, so I write the following code and it's working fine, but it's not Synchronous.
Now I'm trying to get all the tweets from #iOS.
I have used the following code to get the search result for iOS hashtag:
-(void)fetchResults
{
// Do a simple search, using the Twitter API
TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
#"http://search.twitter.com/search.json?q=iOS%20&rpp=20&with_twitter_user_id=true&result_type=recent"]
parameters:nil requestMethod:TWRequestMethodGET];
// Notice this is a block, it is the handler to process the response
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
// The response from Twitter is in JSON format
// Move the response into a dictionary and print
NSError *error;
dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
NSLog(#"Twitter response: %#", [dict description]);
[self filterTweets];
}
else
NSLog(#"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];
}
As a result I got this:
Twitter response: {
"completed_in" = "0.007";
"max_id" = 333837474914766848;
"max_id_str" = 333837474914766848;
page = 1;
query = quranRadios;
"refresh_url" = "?since_id=333837474914766848&q=quranRadios&result_type=recent";
results = (
{
"created_at" = "Mon, 13 May 2013 06:53:51 +0000";
"from_user" = YousefMutawe;
"from_user_id" = 324385406;
"from_user_id_str" = 324385406;
"from_user_name" = "Yousef N Mutawe \Uf8ff";
geo = "<null>";
id = 333837474914766848;
"id_str" = 333837474914766848;
"iso_language_code" = pt;
metadata = {
"result_type" = recent;
};
"profile_image_url" = "http://a0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
"profile_image_url_https" = "https://si0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
source = "<a href="http://twitter.com/download/iphone">Twitter for iPhone</a>";
text = "Testing #quranRadios #Mkalatrash";
},
{
"created_at" = "Sun, 12 May 2013 13:09:43 +0000";
"from_user" = YousefMutawe;
"from_user_id" = 324385406;
"from_user_id_str" = 324385406;
"from_user_name" = "Yousef N Mutawe \Uf8ff";
geo = "<null>";
id = 333569679484416000;
"id_str" = 333569679484416000;
"iso_language_code" = et;
metadata = {
"result_type" = recent;
};
"profile_image_url" = "http://a0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
"profile_image_url_https" = "https://si0.twimg.com/profile_images/1533729607/20090719526_normal.jpg";
source = "<a href="http://twitter.com/download/iphone">Twitter for iPhone</a>";
text = "#quranRadios :)";
}
);
"results_per_page" = 20;
"since_id" = 0;
"since_id_str" = 0;
}
So i use the following method to filter the result and to get the (Tweet,Username,and the User image):
-(void)filterTweets
{
NSArray *results = [dict objectForKey:#"results"];
//Loop through the results
int x =0;
for (NSDictionary *tweet in results)
{
// Get the tweet
NSString *twittext = [tweet objectForKey:#"text"];
NSString *twitPic = [tweet objectForKey:#"profile_image_url"];
NSString *userName = [tweet objectForKey:#"from_user"];
// Save the tweet to the twitterText array
[tweetsInfo addObject:(twittext)];
[tweetPics addObject:(twitPic)];
[imagesArray addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tweetPics objectAtIndex:x]]]]];
[userNameTweet addObject:userName];
x++;
//NSLog(#"tweet ooooooo ======> %#",twitPic);
countMe++;
}
[tweetsTable reloadData];
}
I'm not sure if i'm doing the right thing,so what would you recommend me to do? and how can i make it synchronized?
am new to programming iOS, please advice.
Thanks.

AFNetworking - How to setup requests to be retried in the event of a timeout?

I have recently migrated from ASIHTTPRequest to AFNetworking, which has been great. However, the server that I am connecting with has some issues and sometimes causes my requests to timeout. When using ASIHTTPRequest it was possible to setup a retry count on a request in the event of a timeout using the following selector
-setNumberOfTimesToRetryOnTimeout:
This can be further referenced in this post, Can an ASIHTTPRequest be retried?
This is AFNetworking if you are unfamiliar
https://github.com/AFNetworking/AFNetworking#readme
I was unable to find an equivalent api in AFNetworking, has anyone found a solution for retrying network requests in the event of timeout using AFNetworking?
Matt Thompson developer of AFNetworking was kind enough to answer this for me. Below is the github link explaining the solution.
https://github.com/AFNetworking/AFNetworking/issues/393
Basically, AFNetworking doesn't support this functionality. It is left to the developer to implement on a case by case basis as shown below (taken from Matt Thompson's answer on github)
- (void)downloadFileRetryingNumberOfTimes:(NSUInteger)ntimes
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
if (ntimes <= 0) {
if (failure) {
NSError *error = ...;
failure(error);
}
} else {
[self getPath:#"/path/to/file" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(...);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self downloadFileRetryingNumberOfTimes:ntimes - 1 success:success failure:failure];
}];
}
}
I implemented private method in my ApiClient class:
- (void)sendRequest:(NSURLRequest *)request successBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))successBlock failureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBlock
{
__block NSUInteger numberOfRetries = 3;
__block __weak void (^weakSendRequestBlock)(void);
void (^sendRequestBlock)(void);
weakSendRequestBlock = sendRequestBlock = ^{
__strong typeof (weakSendRequestBlock)strongSendRequestBlock = weakSendRequestBlock;
numberOfRetries--;
AFHTTPRequestOperation *operation = [self.httpManager HTTPRequestOperationWithRequest:request success:successBlock failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSInteger statusCode = [[[error userInfo] objectForKey:AFNetworkingOperationFailingURLResponseErrorKey] statusCode];
if (numberOfRetries > 0 && (statusCode == 500 || statusCode == 502 || statusCode == 503 || statusCode == 0)) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
strongSendRequestBlock();
});
} else {
if (failureBlock) {
failureBlock(operation, error);
}
}
}];
[self.httpManager.operationQueue addOperation:operation];
};
sendRequestBlock();
}
Example of usage:
- (void)getSomeDetails:(DictionaryResultBlock)block
{
if (!block) {
return;
}
NSString *urlString = #"your url string";
NSMutableURLRequest *request = [self.httpManager.requestSerializer requestWithMethod:#"POST" URLString:[[NSURL URLWithString:urlString relativeToURL:self.defaultUrl] absoluteString] parameters:nil error:nil];
// Configure you request here
[request setValue:version forHTTPHeaderField:#"client-version"];
NSMutableDictionary *bodyParams = #{};
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:bodyParams options:0 error:nil]];
[self sendRequest:request successBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
id response = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
block(response, nil);
} failureBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
block(nil, error);
}];
}
In my case, I frequently required retry functionality so I came up wit this retry policy category that will help you with that AFNetworking+RetryPolicy
With respect to AFNetworking 3.0 it could serve well.
Based on your answers, you could do something even more generic (and tricky) by using a block taking as parameter a block :
typedef void (^CallbackBlock)(NSError* error, NSObject* response);
- (void) performBlock:(void (^)(CallbackBlock callback)) blockToExecute retryingNumberOfTimes:(NSUInteger)ntimes onCompletion:(void (^)(NSError* error, NSObject* response)) onCompletion {
blockToExecute(^(NSError* error, NSObject* response){
if (error == nil) {
onCompletion(nil, response);
} else {
if (ntimes <= 0) {
if (onCompletion) {
onCompletion(error, nil);
}
} else {
[self performBlock:blockToExecute retryingNumberOfTimes:(ntimes - 1) onCompletion:onCompletion];
}
};
});
}
Then surround your asynchronous HTTP requests like the following :
[self performBlock:^(CallbackBlock callback) {
[...]
AFHTTPRequestOperationManager *manager = [WSManager getHTTPRequestOperationManager];
[manager POST:base parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
dispatch_async(dispatch_get_main_queue(), ^(void){
if (callback) {
callback(nil, responseObject);
}
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (callback) {
NSError* errorCode = [[NSError alloc] initWithDomain:AppErrorDomain code:[operation.response statusCode] userInfo:#{ NSLocalizedDescriptionKey :error.localizedDescription}];
callback(errorCode, nil);
}
}];
} retryingNumberOfTimes:5 onCompletion:^(NSError *error, NSObject* response) {
//everything done
}];
This way the retries wait for the HTTP request to finish and you don't have to implement the retry loop in each request methods.

Strange RestKit / RKRequestQueue synchronous Issue

I have an ARC-enabled project using RestKit and although most of my requests are done asynchronously, I am having an issue with performing a synchronous request:
In my AppDelegate:
else if (![IKUserController loggedInUserIsAuthenticated]) {
IKLoginViewController *loginVC = [[IKLoginViewController alloc] init];
loginVC.scenario = SCENARIO_EXISTING;
[self.window.rootViewController presentModalViewController:loginVC animated:YES];
}
In the implementation for loggedInUserIsAuthenticated:
+ (BOOL)loggedInUserIsAuthenticated {
IKUser *user = [IKUserController loggedInUser];
if (!user) {
return NO;
}
else {
NSString *username = user.userName;
NSString *password = user.userPassword;
if ([IKUserController loginWithUsername:username password:password]) {
return YES;
}
else {
return NO;
}
}
return NO;
}
and the loginWithUserName:password:
+ (BOOL)loginWithUsername:(NSString *)username password:(NSString *)password {
//return YES;
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:username, #"username", password, #"password", nil];
RKResponse *response = [[[RKClient sharedClient] post:#"/user/authenticate" params:params delegate:nil] sendSynchronously];
if (response.isOK) {
return YES;
}
else {
return NO;
}
return NO;
}
and the error:
*** Assertion failure in -[RKRequestQueue removeRequest:decrementCounter:], /Users/admin/Documents/dev/RestKit/Code/Network/RKRequestQueue.m:350
The RKClient method post already adds the request to the default request queue, so I think the problem is that you send the request twice - once async and once sync. Instead of using RKClient post method, configure the request manually. There is a method setupRequest on RKClient, this will make it easier for you and you will only need to define the url, method and params, like this:
RKRequest* req = [RKRequest requestWithURL:reqURL delegate:self];
[req setMethod:RKRequestMethodPOST];
[req setParams:params];
[client setupRequest:req];
[req sendSynchronously];

Using JSON Framework on iPhone - HELP!

Currently I am using the following code to parse the JSON link sent. This is how I also send a GET call to the Google Reader API for an upcoming iPhone application of mine.
- (NSArray *)subscriptionList
{
if(!cookies && [cookies count] == 0) {
[self requestSession];
}
NSString * url = #"http://www.google.com/reader/api/0/subscription/list?output=json&client=scroll";
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setRequestMethod:#"GET"];
[request setRequestCookies:cookies];
[request addRequestHeader:#"Authorization" value:[NSString stringWithFormat:#"GoogleLogin auth=%#", [self auth]]];
[request startSynchronous];
subfeeds = [NSMutableArray array];
// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];
if ([request responseStatusCode] == 200) {
NSData * sixty = [request responseData];
NSString * body = [[NSString alloc] initWithData:sixty encoding:NSUTF8StringEncoding];
if (body) {
NSArray *feeds = [parser objectWithString:body error:nil];
NSLog(#"Array Contents: %#", [feeds valueForKey:#"subscriptions"]);
NSLog(#"Array Count: %d", [feeds count]);
NSDictionary *results = [body JSONValue];
NSArray *ohhai = [results valueForKey:#"subscriptions"];
for (NSDictionary *title in ohhai) {
subTitles = [title objectForKey:#"title"];
NSLog(#"title is: %#",subTitles);
}
}
}
return subfeeds;
[subTitles release];
[parser release];
}
I can successfully parse the JSON using the above code, and it successfully outputs the titles into NSLog. In my RootViewController.m, I call the following to grab this -(NSArray *)subscriptionList.
-(void)viewDidAppear:animated {
GoogleReader * reader = [[GoogleReader alloc] init];
[reader setEmail:gUserString];
[reader setPassword:gPassString];
//feedItems is a NSArray where we store the subscriptionList NSArray
feedItems = [reader subscriptionList];
//NSString *feedTitle = [];
NSLog(#"%#", feedItems);
[reader release];
// the rest of the function
}
The code above successfully works with the credentials entered. As you can see there is also a commented NSString called feedTitle. This is where I want to pull the #"title" from the parsed JSON but I do not know how to call it.
Any help would be greatly appreciated!
This is what the JSON source looks like:
{"subscriptions":
[
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""}
]
}
I'm interested in only the "title" node.
Well, it would help if you added the source JSON but it's quite easy to grasp how SBJSON parses incoming JSON.
Just an example:
{ "myOutDict" : { "key1": "val1" , "key2" : "val2"} }
This JSON String would be parsed so you can access it by using this code
NSDictionary* myOuterdict = [feeds valueForKey:#"myOutDict"]);
NSString* val1 = [myOuterdict valueForKey:#"key1"]);
NSString* val2 = [myOuterdict valueForKey:#"key2"]);
Edit: Checked my personal Google Reader feed:
The JSON looks like this
{
"subscriptions": [{
"id": "feed/http://adambosworth.net/feed/",
"title": "Adam Bosworth's Weblog",
"categories": [],
"sortid": "0B5B845E",
"firstitemmsec": "1243627042599"
},
{
"id": "feed/http://feeds.feedburner.com/zukunftia2",
"title": "Zukunftia",
"categories": [],
"sortid": "FCABF5D4",
"firstitemmsec": "1266748722471"
}]
}
So the corresponding Objective C Code would be:
NSArray* subscriptions= [feeds valueForKey:#"subscriptions"]);
foreach(NSDictionary* item in subscriptions) {
// Do stuff
// NSString* title = [item valueForKey:#"title"]
// NSString* id = [item valueForKey:#"id"]
}
I'm not sure I understand the question. Are you trying to get a title for the feed as a whole, or per-item? Because I can't see a title property for the subscriptions array in the source JSON.