-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathFTGooglePlacesAPIExampleDetailViewController.m
More file actions
134 lines (103 loc) · 4.63 KB
/
FTGooglePlacesAPIExampleDetailViewController.m
File metadata and controls
134 lines (103 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// FTGooglePlacesAPIExampleDetailViewController.m
// FTGooglePlacesAPI
//
// Created by Lukas Kukacka on 29/11/13.
// Copyright (c) 2013 Fuerte Int. Ltd. All rights reserved.
//
#import "FTGooglePlacesAPIExampleDetailViewController.h"
#import "FTGooglePlacesAPI.h"
@interface FTGooglePlacesAPIExampleDetailViewController ()
@property (nonatomic, strong) id<FTGooglePlacesAPIRequest> request;
@property (nonatomic, strong) FTGooglePlacesAPIDetailResponse *response;
@property (nonatomic, strong) NSDictionary *responseTableRepresentation;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@end
@implementation FTGooglePlacesAPIExampleDetailViewController
- (instancetype)initWithRequest:(id<FTGooglePlacesAPIRequest>)request
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
self.title = @"Detail";
_request = request;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicatorView.hidesWhenStopped = YES;
UIBarButtonItem *activityBarButton = [[UIBarButtonItem alloc] initWithCustomView:_activityIndicatorView];
self.navigationItem.rightBarButtonItem = activityBarButton;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startSearching];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_responseTableRepresentation count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *key = [_responseTableRepresentation allKeys][indexPath.row];
cell.textLabel.text = key;
cell.detailTextLabel.text = [_responseTableRepresentation[key] description];
return cell;
}
#pragma mark - FTGooglePlacesAPI performing search request
- (void)startSearching
{
// Show activity indicator
[_activityIndicatorView startAnimating];
// Execute Google Places API request using FTGooglePlacesAPIService
[FTGooglePlacesAPIService executeDetailRequest:_request
withCompletionHandler:^(FTGooglePlacesAPIDetailResponse *response, NSError *error)
{
// If error is not nil, request failed and you should handle the error
// We just show alert
if (error)
{
// There may be a lot of causes for an error (for example networking error).
// If the network communication with Google Places API was successfull,
// but the API returned some status code, NSError will have
// FTGooglePlacesAPIErrorDomain domain and status code from
// FTGooglePlacesAPIResponseStatus enum
// You can inspect error's domain and status code for more detailed info
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[_activityIndicatorView stopAnimating];
return;
}
// Everything went fine, we have response object
// You can do whatever you need here, we just construct "table representation"
// of response to the dictionary and reload table
_responseTableRepresentation = @{
@"Name": (response.name?:@"N/A"),
@"Location": [NSString stringWithFormat:@"(%.6f,%.6f)", response.location.coordinate.latitude, response.location.coordinate.longitude],
@"Address": (response.addressString?:@"N/A"),
@"Form. address": (response.formattedAddress?:@"N/A"),
@"Form. phone": (response.formattedPhoneNumber?:@"N/A"),
@"Int. phone": (response.internationalPhoneNumber?:@"N/A"),
@"Rating": [NSString stringWithFormat:@"%.1f", response.rating],
@"Types": [response.types componentsJoinedByString:@","],
@"Website:": (response.websiteUrl?:@"N/A"),
};
[self.tableView reloadData];
[_activityIndicatorView stopAnimating];
}];
}
@end