I am developing a Location based application. application will show route between to locations with turn by turn directions(as in native "Maps" application with all the nodes). I am requesting to google maps for directions in following way.(as example directions between cupertino and stanford)
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.google.com/maps?dirflg=d&output=dragdir&saddr=Stanford&daddr=cupertino"];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSError* error = nil;
NSString* apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error];
NSLog(#"apiResponse=%#", apiResponse);
At the console I got response
{tooltipHtml:" (12.9\x26#160;mi / 19 mins)",polylines:[{id:"route0",points:"kklcFzishVdBb#??#s#rB{PHi#Xk#??bEmDrDgF~DeHVcAhCgG??rFfEv#RbFHnEa#vBGnEd#\\P~EjGtMxK??zNwTxB_ChCqD??bAt#bDdBvDt#~BXrC#xBS|NiClA#fAPvBdA~#dAlIQAxAr#n#|#d#rFxA|AlAAnAz#t#l##nAd#zQnC??bA\\^XVr#Az#KVa#\\m#B[UWq#?i#zBaI#oBd#mDt#kS~#kGAoDrA_DhAqB~AkBlCuBlDiB~Cw#lU}CbBa#~B{#jE_CvAgAjDyDhQ{SvFuFpUgSzD{EdAcBBiDlA_DfAaEbIk#r#mERyBZkMXkHX{DjA{GtBaG|CcFxC{CfAy#vUaMjDmEvA}BxCkGAwCfAyEtCiRv#{ClB_F|B_EpDkEvAoAdGcE|FwEbBmBvBmDhAiCtMma#dAcCrBmDLiNlB{CfAcCtEaMtKsUCmEdP{WbB_EbAoDr#}Dd#aFLkCC_FK_C{C}YMqGPsGRyC\eCdAeFpCaLdBiJXcENgHa#qIcIwo#[DWeFKk[Dyc#??\gHbAgI?#??|bAG???s#_#?",levels:"BBB???BB????BB?#???#??BB??BB???#????#????#??????#BB?????#???#????#???#??????#??????#???#????#?#?????#?????#??????#??#??#?????A???#????#??????#???#?BB??BBBB?B",numLevel`s:4,zoomFactor:16}]}
I am not able understand above response. Does this response contains turn by turn directions? If not how can get these directions?
According to Google Directions API documentation:
overview_polyline contains an object holding an array of encoded
points
That means you cannot directly read real points value.
Also:
Note: the Directions API may only be used in conjunction with
displaying results on a Google map; using Directions data without
displaying a map for which directions data was requested is
prohibited.
Related
I'm working on an iOS client for App.net, and I'd like to use AFIncrementalStore to sync the web service with my Core Data implementation in the app.
I've gotten the two to work for API requests that don't require an auth token, but I can't figure out how to use the auth token with AFIncrementalStore.
In other words, I'm able to pull down the global feed, since it doesn't require auth:
https://alpha-api.app.net/stream/0/posts/stream/global
...however, to get a user's stream, you need auth (you'll note this link gives an error):
https://alpha-api.app.net/stream/0/posts/stream
I understand that I need to append an auth_token to the end of the API call, but I'm not sure how to do that using AFIncrementalStore.
Update: Here's the chunk of code that currently gets the global stream:
This is a method pulled directly from the example App.net project included in AFIncrementalStore.
-(NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest withContext:(NSManagedObjectContext *)context {
NSMutableURLRequest *mutableURLRequest = nil;
if ([fetchRequest.entityName isEqualToString:#"Post"]) {
mutableURLRequest = [self requestWithMethod:#"GET" path:#"stream/0/posts/stream/global" parameters:nil];
}
return mutableURLRequest;
}
All you have to do with the case above to alter it to correctly use an access token is add parameters to the request instead of passing nil.
To build the parameters simply make an NSDictionary with the keys and values you need. For example in some of my App.net code I have this
NSDictionary *params = #{#"access_token": token};
Using the new compiler directives this builds an NSDictionary with a single key (in this case 'access_token') with the value in the NSString token.
After you have this just make your request something like:
[self requestWithMethod:#"GET" path:#"stream/0/posts/stream" parameters:params];
To pass the parameters in the request.
Let me know if this isn't clear enough!
I have an iOS app using RestKit in order to communicate with a RESTful Rails server. The server uses a basic session token for authentication of users. I want to append this token to every request sent using the RKObjectManager's methods.
I've tried creating a Category overload of NSManagedObject in order to use the following method:
- (void)willSendWithObjectLoader:(RKObjectLoader *)objectLoader
That works fine, however I see no way of appending to the object loader's params. I've even gone as far as to reserialize the object but there is no way to do that without it being sent using escape characters. For example, the following code will give me the object in JSON serialized form
RKObjectMapping *serializationMapping = [[[RKObjectManager sharedManager] mappingProvider] serializationMappingForClass:[self class]];
RKObjectSerializer *ser = [RKObjectSerializer serializerWithObject:self mapping:serializationMapping];
NSObject<RKRequestSerializable> *obj = [ser serializationForMIMEType:RKMIMETypeJSON error:nil];
But the object returned there is intend to be used as a param right away so the following code does not work
[params setValue:[LCSingletonLoggedInUser sharedLoggedInUser].sessionToken forParam:#"token"];
[params setData:obj.HTTPBody forParam:#"data"];
I've also tried various combinations of setObject and setData and obj.HTTPBody as well as just obj right away.
Actually appending obj to params in any way will always result in it adding escape characters which the server can't handle. Setting params = obj will give the correct values to the server but won't include the token.
How about adding it to queryParams?
NSString *resourcePath = [#"/products" stringByAppendingQueryParameters:_queryParams];
Where queryParams is a NSMutableDictionary where you add your token.
Disclaimer: I'm new to JSON.
I have a website that offers up it's Data in JSON format and I'm in the process of building an iPhone App to access that data. The problem I have is not actually with the app itself but rather the syntax to access some of the JSON data.
I don't have any issue accessing the data in the Post [ ] Section, however I want to access Attachments > Images > wptouch-new-thumbnail > url
I know you usually would access the data with a dot syntax but with everything I have tried I have been unable to select anything outside of the Post[] section.
it's pretty simple I take what I get from the server through posts and add them to NSStrings or UIImage in the case of the images for use in a UITableView.
I have all of that working however I can't seem to access anything under attachments. I tried to make a different NSDictionary for attachments but that did not work.
Again remember I'm very new to JSON / Accessing data within it.
Any help would be appreciated!
Make NSMutableData as jsonMutableData instead of NSMutableString.
in connection dedreceiveresponse, initialize jsonMutableData then,
//Json Connection
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[jsonMutableData appendData:partialData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonData = [[NSString alloc] initWithData:jsonMutableData encoding:NSUTF8StringEncoding];
NSDictionary *filesJSON = [jsonData JSONValue];
NSArray *files = [filesJSON objectForKey:#"posts"];
//further code
i am using json parsing in my application
and this is my json data is as follow :
{"response" :
{"success":false,"error":
{"code":7,"description":"You are not logged in"}}}
and i want description means "You are not logged in" i my string
so how can i do that
please help me.....
Check the blog post with sample code and step by step parsing of JSON.
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
http://pessoal.org/blog/2008/12/12/iphone-sdk-parsing-json-data/
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
We're using CJSONDeserializer (TouchJSON library) in the iPhone app being developed at work.
Just use the following method:
NSDictionary * dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
where data is of type NSData *. It will convert the JSON string into a dictionary so you could get the value of description as follows:
[[[dictionary objectForKey:#"response"] objectForKey:#"error"] objectForKey:#"description"];
I am able to successfully get the SID (SessionID) for my Google Reader account. In order to obtain the feed and do other operations inside Google Reader, you have to obtain an authorization token. I'm having trouble doing this. Can someone shed some light?
//Create a cookie to append to the GET request
NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"SID",#"NSHTTPCookieName",self.sessionID,#"NSHTTPCookieValue",#".google.com",#"NSHTTPCookieDomain",#"/",#"NSHTTPCookiePath",nil];
NSHTTPCookie *authCookie = [NSHTTPCookie cookieWithProperties:cookieDictionary];
//The URL to obtain the Token from
NSURL *tokenURL = [NSURL URLWithString:#"http://www.google.com/reader/api/0/token"];
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL];
//Not sure if this is right: add cookie to array, extract the headers from the cookie inside the array...?
[tokenRequest setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:[NSArray arrayWithObjects:authCookie,nil]]];
//This gives me an Error 403 Forbidden in the didReceiveResponse of the delegate
[NSURLConnection connectionWithRequest:tokenRequest delegate:self];
I get a 403 Forbidden error as the response from Google. I'm probably not doing it right. I set the dictionary values according to the documentation for NSHTTPCookie.
The keys of a cookie properties should be the NSHTTPCookie constants, not literal strings as you give them.
NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"SID",NSHTTPCookieName,
self.sessionID,NSHTTPCookieValue,
#".google.com",NSHTTPCookieDomain,
#"/",NSHTTPCookiePath,
nil];
Note that the values constants aren't equal to their names; generally speaking, they appear to be the name without the "NSHTTPCookie" (e.g. NSHTTPCookieDomain == #"Domain"), but you shouldn't rely on this. Re-read the documentation.