root/apps/iphone/sampleTelApp/trunk/Classes/sampleTelAppViewController.m
@
84
| Revision 84, 9.7 kB (checked in by henri, 5 years ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // sampleTelAppAppDelegate.m |
| 3 | // sampleTelApp |
| 4 | // |
| 5 | // Created by Henri Asseily on 9/12/08. |
| 6 | // Copyright __MyCompanyName__ 2008. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "sampleTelAppViewController.h" |
| 10 | |
| 11 | @implementation sampleTelAppViewController |
| 12 | |
| 13 | @synthesize headerText; |
| 14 | |
| 15 | #pragma mark --------- View basic methods --------- |
| 16 | |
| 17 | -(id)init { |
| 18 | self = [super init]; |
| 19 | return self; |
| 20 | } |
| 21 | |
| 22 | - (void)viewDidLoad { |
| 23 | // First activate the Edge connection. |
| 24 | // If upon request of a .tel the iPhone doesn't have a connection, the app will |
| 25 | // quit to the springboard. A production application should fail more gracefully. |
| 26 | |
| 27 | // The only way to force the EDGE network to come up is to create a connection to some |
| 28 | // address using CFStream* interface. This code tries to create a connection to |
| 29 | // google.com. |
| 30 | |
| 31 | NSMutableURLRequest *urlReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; |
| 32 | [urlReq setHTTPMethod:@"HEAD"]; |
| 33 | NSURLResponse *response; |
| 34 | NSError *error; |
| 35 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; |
| 36 | NSData *reqResults = [NSURLConnection sendSynchronousRequest:urlReq |
| 37 | returningResponse:&response |
| 38 | error:&error]; |
| 39 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; |
| 40 | if (!reqResults) { |
| 41 | UIAlertView *alert = [UIAlertView alloc]; |
| 42 | [[alert initWithTitle:@"Data Connection Error" |
| 43 | message:[error localizedDescription] |
| 44 | delegate:self |
| 45 | cancelButtonTitle:nil |
| 46 | otherButtonTitles:@"Quit", nil] autorelease]; |
| 47 | [alert show]; |
| 48 | } |
| 49 | |
| 50 | // Data connection activation is now started |
| 51 | // Get on with the program |
| 52 | |
| 53 | [super viewDidLoad]; |
| 54 | if (! resolver) { |
| 55 | resolver = [[[DnsResolver alloc] init] retain]; |
| 56 | headerText = @""; |
| 57 | naptrArray = [[NSMutableArray arrayWithCapacity:10] retain]; |
| 58 | txtArray = [[NSMutableArray arrayWithCapacity:10] retain]; |
| 59 | locArray = [[NSMutableArray arrayWithCapacity:1] retain]; |
| 60 | otherArray = [[NSMutableArray arrayWithCapacity:1] retain]; |
| 61 | } |
| 62 | requestedDomain = [[NSMutableString stringWithString:@""] retain]; |
| 63 | } |
| 64 | |
| 65 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 66 | // Return YES for supported orientations |
| 67 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | - (void)didReceiveMemoryWarning { |
| 72 | [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview |
| 73 | // Release anything that's not essential, such as cached data |
| 74 | } |
| 75 | |
| 76 | |
| 77 | - (void)dealloc { |
| 78 | [super dealloc]; |
| 79 | [resolver release]; |
| 80 | } |
| 81 | |
| 82 | #pragma mark -------------- action calls ------------------ |
| 83 | |
| 84 | // Called when a user enters a .tel in the UITextField |
| 85 | - (IBAction)getTelData:(UITextField *)sender { |
| 86 | [sender resignFirstResponder]; |
| 87 | [self lookupDomain:sender.text]; |
| 88 | //[self performSelectorInBackground:@selector(lookupDomain:) withObject:sender.text]; |
| 89 | } |
| 90 | |
| 91 | // Main method |
| 92 | - (void)lookupDomain:(NSString *)domain { |
| 93 | |
| 94 | //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 95 | |
| 96 | // Append ".tel" if it was not provided |
| 97 | [requestedDomain setString:domain]; |
| 98 | if (![requestedDomain hasSuffix:@".tel"]) { |
| 99 | [requestedDomain appendString:@".tel"]; |
| 100 | } |
| 101 | [telEntryField setText:requestedDomain]; |
| 102 | |
| 103 | // Clear old data |
| 104 | headerText = @""; |
| 105 | if ([naptrArray count] > 0) { |
| 106 | [naptrArray removeAllObjects]; |
| 107 | } |
| 108 | if ([txtArray count] > 0) { |
| 109 | [txtArray removeAllObjects]; |
| 110 | } |
| 111 | if ([locArray count] > 0) { |
| 112 | [locArray removeAllObjects]; |
| 113 | } |
| 114 | [telTable reloadData]; |
| 115 | //[[NSThread mainThread] performSelector:@selector(reloadData) withObject:telTable]; |
| 116 | |
| 117 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; |
| 118 | [resolver getNAPTRForTel:requestedDomain inArray:naptrArray]; |
| 119 | //[[NSThread mainThread] performSelector:@selector(reloadData) withObject:telTable]; |
| 120 | headerText = [[resolver getTXTForTel:requestedDomain inArray:txtArray] retain]; |
| 121 | //[[NSThread mainThread] performSelector:@selector(reloadData) withObject:telTable]; |
| 122 | NSUInteger locCount = [resolver getLOCForTel:requestedDomain inArray:locArray]; |
| 123 | if (locCount > 1) { |
| 124 | NSLog(@"Warning: %d LOC records in domain %@", locCount, requestedDomain); |
| 125 | } |
| 126 | //[[NSThread mainThread] performSelector:@selector(reloadData) withObject:telTable]; |
| 127 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; |
| 128 | [telTable reloadData]; |
| 129 | |
| 130 | //[pool release]; |
| 131 | |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | #pragma mark ---------- Table view Data Source methods ---------- |
| 137 | |
| 138 | |
| 139 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 140 | return 4; |
| 141 | } |
| 142 | |
| 143 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { |
| 144 | switch (section) { |
| 145 | case SECTION_HEADER: |
| 146 | return @"Header"; |
| 147 | break; |
| 148 | case SECTION_NAPTR: |
| 149 | return @"Contact Info"; |
| 150 | break; |
| 151 | case SECTION_TXT: |
| 152 | return @"Keywords"; |
| 153 | break; |
| 154 | case SECTION_LOC: |
| 155 | return @"Location"; |
| 156 | break; |
| 157 | default: |
| 158 | return @"Other Records"; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 164 | switch (section) { |
| 165 | case SECTION_HEADER: |
| 166 | if ([headerText isEqualToString:@""]) |
| 167 | return 0; |
| 168 | return 1; |
| 169 | break; |
| 170 | case SECTION_NAPTR: |
| 171 | return [naptrArray count]; |
| 172 | break; |
| 173 | case SECTION_TXT: |
| 174 | return [txtArray count]; |
| 175 | break; |
| 176 | case SECTION_LOC: |
| 177 | return [locArray count]; |
| 178 | break; |
| 179 | default: |
| 180 | return [otherArray count]; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 186 | |
| 187 | switch (indexPath.section) { |
| 188 | case SECTION_HEADER: |
| 189 | { |
| 190 | static NSString *MyIdentifier = @"headerCell"; |
| 191 | |
| 192 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 193 | if (cell == nil) { |
| 194 | NSArray *headerCellNib = [[NSBundle mainBundle] loadNibNamed:@"HeaderTableViewCell" owner:self options:nil]; |
| 195 | cell = (UITableViewCell *)[headerCellNib objectAtIndex:1]; |
| 196 | } |
| 197 | [(UITextView *)[cell viewWithTag:1] setText:headerText]; |
| 198 | return cell; |
| 199 | } |
| 200 | break; |
| 201 | |
| 202 | case SECTION_NAPTR: |
| 203 | { |
| 204 | static NSString *MyIdentifier = @"naptrCells"; |
| 205 | |
| 206 | RecordNaptr *theRec = [naptrArray objectAtIndex:indexPath.row]; |
| 207 | |
| 208 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 209 | if (cell == nil) { |
| 210 | NSArray *naptrCellNib = [[NSBundle mainBundle] loadNibNamed:@"NaptrTableViewCell" owner:self options:nil]; |
| 211 | cell = (UITableViewCell *)[naptrCellNib objectAtIndex:1]; |
| 212 | } |
| 213 | |
| 214 | [(UILabel *)[cell viewWithTag:1] setText:theRec.serviceDescription]; |
| 215 | [(UILabel *)[cell viewWithTag:2] setText:theRec.labelDescription]; |
| 216 | |
| 217 | // Check if it's a nonterminal naptr. If so, add the accessory indicator |
| 218 | // and remove the trailing part of the string if it is a subdomain of the current domain |
| 219 | if (theRec.isTerminal) { |
| 220 | [(UILabel *)[cell viewWithTag:3] setText:theRec.uriContent]; |
| 221 | } else { |
| 222 | if ([[theRec.uriContent lowercaseString] hasSuffix:[requestedDomain lowercaseString]]) { |
| 223 | NSUInteger suffixIndex = [theRec.uriContent length] - [requestedDomain length] - 1; |
| 224 | [(UILabel *)[cell viewWithTag:3] setText:[theRec.uriContent substringToIndex:suffixIndex]]; |
| 225 | } else { |
| 226 | [(UILabel *)[cell viewWithTag:3] setText:theRec.uriContent]; |
| 227 | } |
| 228 | [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; |
| 229 | } |
| 230 | |
| 231 | return cell; |
| 232 | } |
| 233 | break; |
| 234 | |
| 235 | case SECTION_TXT: |
| 236 | { |
| 237 | static NSString *MyIdentifier = @"txtCells"; |
| 238 | |
| 239 | RecordTxt *theRec = [txtArray objectAtIndex:indexPath.row]; |
| 240 | |
| 241 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 242 | if (cell == nil) { |
| 243 | NSArray *txtCellNib = [[NSBundle mainBundle] loadNibNamed:@"TxtTableViewCell" owner:self options:nil]; |
| 244 | cell = (UITableViewCell *)[txtCellNib objectAtIndex:1]; |
| 245 | } |
| 246 | [(UILabel *)[cell viewWithTag:1] setText:theRec.typeDescription]; |
| 247 | [(UILabel *)[cell viewWithTag:2] setText:theRec.textValue]; |
| 248 | return cell; |
| 249 | } |
| 250 | break; |
| 251 | |
| 252 | case SECTION_LOC: |
| 253 | { |
| 254 | static NSString *MyIdentifier = @"locCell"; |
| 255 | |
| 256 | RecordLoc *theRec = [locArray objectAtIndex:indexPath.row]; |
| 257 | |
| 258 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; |
| 259 | if (cell == nil) { |
| 260 | NSArray *locCellNib = [[NSBundle mainBundle] loadNibNamed:@"LocTableViewCell" owner:self options:nil]; |
| 261 | cell = (UITableViewCell *)[locCellNib objectAtIndex:1]; |
| 262 | } |
| 263 | [(UILabel *)[cell viewWithTag:1] setText:theRec.description]; |
| 264 | return cell; |
| 265 | } |
| 266 | break; |
| 267 | |
| 268 | default: |
| 269 | { |
| 270 | UITableViewCell *cell = nil; |
| 271 | return cell; |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | #pragma mark ---------- Table view Delegate methods ---------- |
| 278 | |
| 279 | // Row selection actions: |
| 280 | // - terminal naptr: fire off the url, and let the OS handle it |
| 281 | // - non-terminal naptr: query that domain |
| 282 | // - loc: fire off a maps url, and let the OS handle it |
| 283 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
| 284 | NSUInteger row = indexPath.row; |
| 285 | if (row != NSNotFound) { |
| 286 | [tableView deselectRowAtIndexPath:indexPath animated:NO]; |
| 287 | switch (indexPath.section) { |
| 288 | case SECTION_NAPTR: |
| 289 | { |
| 290 | RecordNaptr *naptr = [naptrArray objectAtIndex:row]; |
| 291 | if (naptr.isTerminal) { |
| 292 | NSURL *naptrUrl = [NSURL URLWithString:naptr.uriContent]; |
| 293 | [[UIApplication sharedApplication] openURL:naptrUrl]; |
| 294 | } else { |
| 295 | // Non-Terminal NAPTR |
| 296 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; |
| 297 | [self lookupDomain:naptr.uriContent]; |
| 298 | } |
| 299 | } |
| 300 | break; |
| 301 | case SECTION_LOC: |
| 302 | { |
| 303 | RecordLoc *loc = [locArray objectAtIndex:row]; |
| 304 | NSURL *locUrl = [NSURL URLWithString: |
| 305 | [NSString stringWithFormat:@"http://maps.google.com/maps?q=%f,%f&ll=%f,%f", |
| 306 | loc.coordinate.latitude, |
| 307 | loc.coordinate.longitude, |
| 308 | loc.coordinate.latitude, |
| 309 | loc.coordinate.longitude |
| 310 | ]]; |
| 311 | [[UIApplication sharedApplication] openURL:locUrl]; |
| 312 | } |
| 313 | break; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | #pragma mark ---------- UIAlertView Delegate methods ---------- |
| 321 | |
| 322 | - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { |
| 323 | exit(0); |
| 324 | } |
| 325 | @end |
Note: See TracBrowser
for help on using the browser.








