strana 1 ios Cvičení RSS čtečka Jiří Kamínek Kaminek.jiri@stoneapp.com
strana 2 Vytvoření nového projektu v XCode Název RSSLesson Navigation-based Application use Core Data for storage nezaškrtávat
strana 3 Vytvoření modelu <?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>živě.cz</title> <link>http://www.zive.cz/default.aspx</link> <description> </description> <language>cs</language> <pubdate>mon, 7 Mar 2011 20:00:00 GMT</pubDate> <image> <title>živě.cz</title> <url>http://www.zive.cz/client.images/logos/logo-zive-rss.gif</url> <link>http://www.zive.cz/default.aspx</link> </image> <item> <title>western Digital zaplatí 4,3 miliardy dolarů za výrobu disků Hitachi</title> <link>http://www.zive.cz/bleskovky/western-digital-zaplati-43-miliardy-dolaru-za-vyrobu-disku-hitachi/sc-4-a-156126/default.aspx</link> <guid>http://www.zive.cz/default.aspx?article=156126</guid> <description> Mezi výrobci pevných disků dojde k důležitému spojení sil. Světová dvojka Western Digital koupí divizi pevných disků společnosti Hitachi, uvedl to Western Digital v dnešní tiskové zprávě. Koupě vyjde na 4,3 miliardy dolarů a měla by z Western Digital udělat největšího výrobce pevných disků a...</description> <pubdate>mon, 7 Mar 2011 17:01:00 GMT</pubDate> </item> </chanel> </rss>
strana 4 Vytvoření modelu <item> <title>western Digital zaplatí 4,3 miliardy dolarů za výrobu disků Hitachi</title> <link>http://www.zive.cz/bleskovky/western-digital-zaplati-43- miliardy-dolaru-za-vyrobu-disku-hitachi/sc-4-a-156126/default.aspx</ link> <description> Mezi výrobci pevných disků dojde k důležitému spojení sil. Světová dvojka Western Digital koupí divizi pevných disků společnosti Hitachi, uvedl to Western Digital v dnešní tiskové zprávě. Koupě vyjde na 4,3 miliardy dolarů a měla by z Western Digital udělat největšího výrobce pevných disků a...</description> <pubdate>mon, 7 Mar 2011 17:01:00 GMT</pubDate> </item>
strana 5 XML parser SAX Simple API for XML DOM Document Object Model
strana 6 RSSProcessor.h Protokol <NSXMLParserDelegate> - (void) parserssfeedaturl:(nsstring*)urladdress;
strana 7 RSSProcessor.h id delegate; NSXMLParser * rssparser; NSMutableArray * stories; NSMutableDictionary * item; NSString * currentelement; NSMutableString * currenttitle, * currentdate, * currentsummary, * currentlink;
strana 8 RSSProcessor.m - (id) init { } if (self = [super init]) { stories = [[NSMutableArray alloc] init]; } return self;
strana 9 RSSProcessor.m - (void)dealloc { } [stories release]; [super dealloc];
strana 10 RSSProcessor.m - (void) parserssfeedaturl:(nsstring*)urladdress; { //you must then convert the path to a proper NSURL or it won't work NSURL *xmlurl = [NSURL URLWithString:urlAddress]; 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]; } [rssparser parse];
strana 11 RSSProcessor.m - (void)parser:(nsxmlparser *)parser parseerroroccurred: (NSError *)parseerror { } NSLog(@"error parsing XML");
strana 12 RSSProcessor.m - (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *) qname attributes:(nsdictionary *)attributedict{ 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];
strana 13 RSSProcessor.m - (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *) qname{ 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"]; } } [stories addobject:[item copy]]; NSLog(@"adding story: %@", currenttitle);
strana 14 RSSProcessor.m - (void)parser:(nsxmlparser *)parser foundcharacters:(nsstring *)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];
strana 15 RSSProcessor.m - (void)parserdidenddocument:(nsxmlparser *) parser { NSLog(@"all done!"); } [delegate setitems:stories]; [[_delegate tableview] reloaddata];
strana 16 Zobrazení výsledku
strana 17 MainWindow.xib Nadpis RSS Lesson
strana 18 RootViewController.xib UITableView
strana 19 RootViewController.h NSArray *items; @property (nonatomic, copy) NSArray *items;
strana 20 RootViewController.m @synthesize items;
strana 21 RootViewController.m - (NSInteger)tableView:(UITableView *) tableview numberofrowsinsection: (NSInteger)section return [items count];
strana 22 RootViewController.m - (UITableViewCell *)tableview:(uitableview *) tableview cellforrowatindexpath:(nsindexpath *) indexpath [cell.textlabel settext:[[items objectatindex:indexpath.row] valueforkey:@"title"]];
strana 23 RootViewController.m - (void)viewdidload items = nil; RSSProcessor *rssprocessor = [[[RSSProcessor alloc] initwithdelegate:self] autorelease]; [rssprocessor parserssfeedaturl:@"http:// www.zive.cz/system/rss.xml"]; //#import "RSSPRocessor.h"
strana 24 Protokol Lepší řešení Model musí mít znalost controlleru
strana 25 ItemProtocol.h @protocol ItemProtocol - (void) updateitemsource:(nsarray*)itemarray; @end
strana 26 RSSProcessor.h #import "ItemProtocol.h" id <ItemProtocol> delegate; @property (retain) id <ItemProtocol> delegate; //@synthesize
strana 27 RootViewController.h #import "ItemProtocol.h" @interface RootViewController : UITableViewController <ItemProtocol>
strana 28 RSSProcessor.m - (void)parserdidenddocument:(nsxmlparser *) parser { NSLog(@"all done!"); } [delegate updateitemsource:stories];
strana 29 RootViewController - (void) updateitemsource:(nsarray*)itemarray { } [self setitems:itemarray]; [[self tableview] reloaddata];
strana 30 Zobrazení detailu? Přejdeme na web
strana 31 Přidáme nový View Classes -> Add -> New file Cocoa Touch Class With XIB for user interface WebController.h
strana 32 WebController.h Protokol <UIWebViewDelegate> IBOutlet UIWebView *_webview; NSString *urladdress; @property (nonatomic, copy) NSString *urladdress;
strana 33 WebController.m @synthesize urladdress;
strana 34 WebController.m - (void)viewdidload self.title = @"Web";
strana 35 WebController.m - (void)viewwillappear:(bool)animated{ NSURL *url = [NSURL URLWithString:[urlAddress stringbyreplacingoccurrencesofstring:@" " withstring:@""]]; NSURLRequest *request = [NSURLRequest requestwithurl:url]; [_webview loadrequest:request]; } [_webview setdelegate:self]; [_webview loadrequest:request];
strana 36 WebController.m - (void)viewwilldisappear:(bool)animated{ NSURL *url = [NSURL URLWithString:@"about:blank"]; } NSURLRequest *request = [NSURLRequest requestwithurl:url]; [_webview loadrequest:request];
strana 37 Zobrazený web
strana 38 RootViewController.m: - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath WebController *detailviewcontroller = [[WebController alloc] initwithnibname:@"webcontroller" bundle:[nsbundle mainbundle]]; [detailviewcontroller seturladdress:[[items objectatindex:indexpath.row] valueforkey:@"link"]]; [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; [detailviewcontroller release];
strana 39 Web na celou obrazovku [_webview setscalespagetofit:yes];
strana 40 StatusBar indikátor načítání
strana 41 WebController.m - (void)webviewdidstartload:(uiwebview *)webview { } UIApplication* app = [UIApplication sharedapplication]; app.networkactivityindicatorvisible = YES;
strana 42 WebController.m - (void)webviewdidfinishload:(uiwebview *)webview { UIApplication* app = [UIApplication sharedapplication]; app.networkactivityindicatorvisible = NO; } - (void)webview:(uiwebview *)webview didfailloadwitherror: (NSError *)error { } UIApplication* app = [UIApplication sharedapplication]; app.networkactivityindicatorvisible = NO;
strana 43 Rychlé zobrazení detailu
strana 44 Rychlé zobrazení detailu Classes -> Add -> New file Cocoa Touch Class With XIB for user interface DetailViewController.h
strana 45 DetailViewController.h IBOutlet UILabel *titlelabel; IBOutlet UILabel *timelabel; IBOutlet UITextView *textview; NSString *url; @property (nonatomic, retain) IBOutlet UILabel *titlelabel; @property (nonatomic, retain) IBOutlet UILabel *timelabel; @property (nonatomic, retain) IBOutlet UITextView *textview; @property (nonatomic, retain) NSString *url;
strana 46 DetailViewController.h @synthesize titlelabel, timelabel, textview, url;
strana 47 DetailViewController.m - (void)viewdidload self.title = @"RSS Detail";
strana 48 DetailViewController.h IBOutlet UILabel *titlelabel; IBOutlet UILabel *timelabel; IBOutlet UITextView *textview; NSString *url; @property (nonatomic, retain) IBOutlet UILabel *titlelabel; @property (nonatomic, retain) IBOutlet UILabel *timelabel; @property (nonatomic, retain) IBOutlet UITextView *textview; @property (nonatomic, retain) NSString *url;
strana 49 DetailViewController.m //pridat do (.h) //- (IBAction) showweb - (IBAction) showweb { } WebController *detailviewcontroller = [[WebController alloc] initwithnibname:@"webcontroller" bundle:[nsbundle mainbundle]]; [detailviewcontroller seturladdress:url]; [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; [detailviewcontroller release];
strana 50 Rychlé zobrazení detailu
strana 51 DetailViewController.m DetailViewController *detailviewcontroller = [[DetailViewController alloc] initwithnibname:@"detailviewcontroller" bundle:[nsbundle mainbundle]]; [self.navigationcontroller pushviewcontroller:detailviewcontroller animated:yes]; NSString *l = [[items objectatindex:indexpath.row] valueforkey:@"link"]; [detailviewcontroller seturl:l]; NSString *t = [[items objectatindex:indexpath.row] valueforkey:@"title"]; [[detailviewcontroller titlelabel] settext:t]; NSString *s = [[items objectatindex:indexpath.row] valueforkey:@"summary"]; [[detailviewcontroller textview] settext:s]; NSString *d = [[items objectatindex:indexpath.row] valueforkey:@"date"]; [[detailviewcontroller timelabel] settext:d]; [detailviewcontroller release];
strana 52 Více RSS zdrojů? RootViewController.m RSSProcessor *secondrssprocessor = [[[RSSProcessor alloc] initwithdelegate:self] autorelease]; [secondrssprocessor parserssfeedaturl:@"http:// www.ceskenoviny.cz/sluzby/rss/index.php"];
strana 53 Více RSS zdrojů? - (void)parserdidenddocument:(nsxmlparser *)parser { NSLog(@"all done!"); NSMutableArray *tmparray = [[[NSMutableArray alloc] init] autorelease]; [tmparray addobjectsfromarray:[_delegate items]]; [tmparray addobjectsfromarray:stories]; } [_delegate setitems:tmparray];
strana 54 Děkuji za pozornost!