Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. +(void)requestSuggestedLocationsForText:(NSString*)text withBlock:(void (^)(NSArray*callBackArray))block
  2. {
  3. if ([text isEqualToString:@""] || [text isEqualToString:@" "])
  4. {
  5. block(nil);
  6. return;
  7. }
  8. NSString * key = @"someActualKeyHere";
  9.  
  10. ;
  11.  
  12.  
  13. NSString * finalText;
  14. NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
  15. NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
  16. [tagger setString:text];
  17. NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
  18. if ([language isEqualToString:@"he"])
  19. {
  20.  
  21. finalText = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  22.  
  23. }
  24. else
  25. {
  26. finalText = [text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
  27. }
  28.  
  29. NSString *urlString = [NSString stringWithFormat:
  30. @"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=geocode&sensor=true&key=%@",finalText,key];
  31.  
  32.  
  33. NSURL *url = [NSURL URLWithString:urlString];
  34. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  35.  
  36. // 2
  37. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  38.  
  39. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  40. if (!responseObject && ![responseObject respondsToSelector:@selector(dataWithData:)])
  41. {
  42. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving "
  43. message:@"ERROR"
  44. delegate:nil
  45. cancelButtonTitle:@"Ok"
  46. otherButtonTitles:nil];
  47. [alertView show];
  48. return ;
  49. }
  50. NSData * responseData = [NSData dataWithData:responseObject];
  51.  
  52. NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
  53. NSError *err;
  54. if ([responseString respondsToSelector:@selector(JSONObjectWithData:options:error:)])
  55. {
  56. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&err];
  57.  
  58.  
  59. NSArray * predictions = [json valueForKey:@"predictions"];
  60.  
  61. block(predictions);
  62. }
  63.  
  64. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  65.  
  66. // 4
  67. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
  68. message:[error localizedDescription]
  69. delegate:nil
  70. cancelButtonTitle:@"Ok"
  71. otherButtonTitles:nil];
  72. [alertView show];
  73. }];
  74.  
  75. // 5
  76. [operation start];
  77.  
  78. }
  79.  
  80. [Requests requestSuggestedLocationsForText:text withBlock:^(NSArray *callBackArray)
  81. {
  82. NSLog(@"ROFL");
  83. }];
  84.  
  85. +(void)requestSuggestedLocationsForText:(NSString*)text withBlock:(void (^)(NSArray*))block;
  86.  
  87. if ([responseString respondsToSelector:@selector(JSONObjectWithData:options:error:)])
  88. {
  89. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&err];
  90. NSArray * predictions = [json valueForKey:@"predictions"];
  91. block(predictions);
  92. }
  93.  
  94. NSData * responseData = [NSData dataWithData:responseObject];
  95. NSError *err;
  96. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];
  97.  
  98. if (!err) {
  99. NSArray * predictions = [json valueForKey:@"predictions"];
  100. block(predictions);
  101. }
  102. else {
  103. block(nil);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement