Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +(void)requestSuggestedLocationsForText:(NSString*)text withBlock:(void (^)(NSArray*callBackArray))block
- {
- if ([text isEqualToString:@""] || [text isEqualToString:@" "])
- {
- block(nil);
- return;
- }
- NSString * key = @"someActualKeyHere";
- ;
- NSString * finalText;
- NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
- NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
- [tagger setString:text];
- NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
- if ([language isEqualToString:@"he"])
- {
- finalText = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- }
- else
- {
- finalText = [text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
- }
- NSString *urlString = [NSString stringWithFormat:
- @"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=geocode&sensor=true&key=%@",finalText,key];
- NSURL *url = [NSURL URLWithString:urlString];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- // 2
- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
- if (!responseObject && ![responseObject respondsToSelector:@selector(dataWithData:)])
- {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving "
- message:@"ERROR"
- delegate:nil
- cancelButtonTitle:@"Ok"
- otherButtonTitles:nil];
- [alertView show];
- return ;
- }
- NSData * responseData = [NSData dataWithData:responseObject];
- NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
- NSError *err;
- if ([responseString respondsToSelector:@selector(JSONObjectWithData:options:error:)])
- {
- NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&err];
- NSArray * predictions = [json valueForKey:@"predictions"];
- block(predictions);
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- // 4
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
- message:[error localizedDescription]
- delegate:nil
- cancelButtonTitle:@"Ok"
- otherButtonTitles:nil];
- [alertView show];
- }];
- // 5
- [operation start];
- }
- [Requests requestSuggestedLocationsForText:text withBlock:^(NSArray *callBackArray)
- {
- NSLog(@"ROFL");
- }];
- +(void)requestSuggestedLocationsForText:(NSString*)text withBlock:(void (^)(NSArray*))block;
- if ([responseString respondsToSelector:@selector(JSONObjectWithData:options:error:)])
- {
- NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&err];
- NSArray * predictions = [json valueForKey:@"predictions"];
- block(predictions);
- }
- NSData * responseData = [NSData dataWithData:responseObject];
- NSError *err;
- NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];
- if (!err) {
- NSArray * predictions = [json valueForKey:@"predictions"];
- block(predictions);
- }
- else {
- block(nil);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement