| Paste number 66412: | Code |
| Pasted by: | MMiller |
| 2 months, 3 weeks ago | |
| #iphonedev | Context in IRC logs | |
| Paste contents: |
| // // MainView.m // HakStore // // Created by Matthew Miller on 9/3/08. // Copyright 2008 __MyCompanyName__. All rights reserved. // #import "MainView.h" #import "AppDelegate.h" @implementation MainView @synthesize myTableView, leafViewController, mySearchBar, listContent, filteredListContent, savedContent; #pragma mark Start Up - (void)awakeFromNib { listContent = [[NSMutableArray alloc] init]; filteredListContent = [[NSMutableArray alloc] initWithCapacity: [listContent count]]; [filteredListContent addObjectsFromArray: listContent]; savedContent = [[NSMutableArray alloc] initWithCapacity: [listContent count]]; mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo; mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; mySearchBar.showsCancelButton = NO; leafViewController.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; NSTimer *timer; timer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(delayedAwakeFromNib:) userInfo: nil repeats: NO]; } - (void) delayedAwakeFromNib: (NSTimer *) timer { NSLog(@"delayed worked"); [myTableView reloadData]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview // Release anything that's not essential, such as cached data } - (void)dealloc { [listContent release]; [filteredListContent release]; [savedContent release]; [currentElement release]; [currentTitle release]; [currentDate release]; [currentSummary release]; [currentLink release]; [item release]; [leafViewController release]; [mySearchBar release]; [myTableView release]; [rssParser release]; [theSelectedRow release]; [super dealloc]; } #pragma mark TableView - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; } // Set up the cell int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; [cell setText:[[listContent objectAtIndex: storyIndex] objectForKey: @"title"]]; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [listContent count]; } - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath { return UITableViewCellAccessoryDisclosureIndicator; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // in case the searchbar's keyboard is up, dismiss it on table selection if ([mySearchBar isFirstResponder]) [mySearchBar resignFirstResponder]; // Navigation logic int appIndex = [indexPath indexAtPosition: [indexPath length] - 1]; NSString * appLink = [[listContent objectAtIndex: appIndex] objectForKey: @"link"]; // clean up the link - get rid of spaces, returns, and tabs... appLink = [appLink stringByReplacingOccurrencesOfString:@" " withString:@""]; appLink = [appLink stringByReplacingOccurrencesOfString:@"\n" withString:@""]; appLink = [appLink stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"link: %@", appLink); // open in Safari [self startDownloadingURL:[NSURL URLWithString:appLink]]; } #pragma mark RSS Feed - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSIndexPath *tableSelection = [myTableView indexPathForSelectedRow]; [myTableView deselectRowAtIndexPath:tableSelection animated:YES]; if ([listContent count] == 0) { NSString * path = @"http://miniflash.freehostia.com/rss/index.rss"; [self parseXMLFileAtURL:path]; } cellSize = CGSizeMake([myTableView bounds].size.width, 60); } - (void)parseXMLFileAtURL:(NSString *)URL { //you must then convert the path to a proper NSURL or it won't work NSURL *xmlURL = [NSURL URLWithString:URL]; // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error // this may be necessary only for the toolchain rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks. [rssParser setDelegate:self]; // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser. [rssParser setShouldProcessNamespaces:NO]; [rssParser setShouldReportNamespacePrefixes:NO]; [rssParser setShouldResolveExternalEntities:NO]; [rssParser parse]; } - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { NSString * errorString = [NSString stringWithFormat:@"Unable to download applications from web site (Error code %i )", [parseError code]]; NSLog(@"error parsing XML: %@", errorString); UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ //NSLog(@"found this element: %@", elementName); currentElement = [elementName copy]; if ([elementName isEqualToString:@"item"]) { // clear out our story item caches... item = [[NSMutableDictionary alloc] init]; currentTitle = [[NSMutableString alloc] init]; currentDate = [[NSMutableString alloc] init]; currentSummary = [[NSMutableString alloc] init]; currentLink = [[NSMutableString alloc] init]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ //NSLog(@"ended element: %@", elementName); if ([elementName isEqualToString:@"item"]) { // save values to an item, then store that item into the array... [item setObject:currentTitle forKey:@"title"]; [item setObject:currentLink forKey:@"link"]; [item setObject:currentSummary forKey:@"summary"]; [item setObject:currentDate forKey:@"date"]; [listContent addObject:[item copy]]; NSLog(@"adding app: %@", currentTitle); } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ //NSLog(@"found characters: %@", string); // save the characters for the current item... if ([currentElement isEqualToString:@"title"]) { [currentTitle appendString:string]; } else if ([currentElement isEqualToString:@"link"]) { [currentLink appendString:string]; } else if ([currentElement isEqualToString:@"description"]) { [currentSummary appendString:string]; } else if ([currentElement isEqualToString:@"pubDate"]) { [currentDate appendString:string]; } } - (void)parserDidEndDocument:(NSXMLParser *)parser { [activityIndicator stopAnimating]; [activityIndicator removeFromSuperview]; NSLog(@"all done!"); NSLog(@"listContent array has %d items", [listContent count]); [myTableView reloadData]; } #pragma mark Search Bar - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { // if a valid search was entered but the user wanted to cancel, bring back the saved list content if (searchBar.text.length > 0) { } [searchBar resignFirstResponder]; searchBar.text = @""; } // called when Search (in our case "Done") button pressed - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { mySearchBar.showsCancelButton = NO; } - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { // only show the status bar's cancel button while in edit mode mySearchBar.showsCancelButton = YES; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { } #pragma mark Download App - (void)startDownloadingURL:(NSURL *)aURL { //PROBLEMS HERE // create the request NSIndexPath *tableSelection = [myTableView indexPathForSelectedRow]; NSString* link = [listContent valueForKey:@"currentLink"]; NSURL * appLink = [listContent objectAtIndex:tableSelection] valueForKey:@"currentLink"]; NSURLRequest *theRequest=[NSURLRequest requestWithURL:appLink cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; //TO HERE... //I cant seem to get the URL that is in a key format from the "item" which is each row. (Item holds the info to the name of the row, the link/url and other information.) // create the connection with the request // and start loading the data NSString *destinationFilename; NSString *homeDirectory=NSHomeDirectory(); destinationFilename=[homeDirectory stringByAppendingPathComponent:@""]; NSURLDownload *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; if (theDownload) { // set the destination file now [theDownload setDestination:destinationFilename allowOverwrite:YES]; } else { // inform the user that the download could not be made } } - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error { // release the connection [download release]; // inform the user NSLog(@"Download failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); } - (void)downloadDidFinish:(NSURLDownload *)download { // release the connection [download release]; // do something with the data NSLog(@"%@",@"downloadDidFinish"); } @end |
Annotations for this paste:
| Annotation number 1: | Code |
| Pasted by: | MMiller |
| 2 months, 3 weeks ago | |
| Context in IRC logs | |
| Paste contents: |
| - (void)startDownloadingURL:(NSURL *)aURL { // create the request NSInteger *tableSelection; NSURL *appURL = [[[listContent objectAtIndex:tableSelection.row] valueForKey:@"currentLink"] absoluteURL]; NSURLRequest *theRequest=[NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; // create the connection with the request // and start loading the data NSString *destinationFilename; NSString *homeDirectory=NSHomeDirectory(); destinationFilename=[homeDirectory stringByAppendingPathComponent:@""]; NSURLDownload *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; if (theDownload) { // set the destination file now [theDownload setDestination:destinationFilename allowOverwrite:YES]; } else { // inform the user that the download could not be made } } |