Created
January 18, 2013 19:17
-
-
Save MatthewZaso/4567463 to your computer and use it in GitHub Desktop.
This code example is an Objective-C one that I wrote before ARC was implemented into iOS. I have now ventured into ARC-enabled projects as well, and can see it's benefits. This project though was for my "Rise and Shine" app, and is getting data from chosen API's and displaying them to the user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Default_VC.m | |
// Rise and Shine | |
// | |
// Created by new user on 11/2/11. | |
// Copyright 2011 Matthew Zaso. All rights reserved. | |
// | |
#import "Default_VC.h" | |
@implementation Default_VC | |
@synthesize feeds,feedFetcher,feedsInfo,cnnLabel,espnLabel,tempLabel,condLabel,weatherImage,scroll,thingCount; | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
// Custom initialization | |
feedsInfo = [[NSMutableDictionary alloc] init]; | |
[feedsInfo setObject:@"http://rss.cnn.com/rss/cnn_topstories.rss" forKey:@"CNN"]; | |
[feedsInfo setObject:@"http://feeds.foxnews.com/foxnews/most-popular" forKey:@"FOX"]; | |
[feedsInfo setObject:@"http://sports.espn.go.com/espn/rss/news" forKey:@"ESPN"]; | |
[feedsInfo setObject:@"http://pheedo.msnbc.msn.com/id/3032091/device/rss" forKey:@"MSNBC"]; | |
[feedsInfo setObject:@"http://www.engadget.com/rss.xml" forKey:@"Engadget"]; | |
thingCount = 10; | |
} | |
return self; | |
} | |
- (void)getFeed{ | |
for(int i=0;i<[feeds count];i++){ | |
for(NSString* key in feedsInfo) | |
{ | |
id value = [feedsInfo objectForKey:key]; | |
if([[feeds objectAtIndex:i] isEqualToString:key]){ | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; | |
FeedFetcher *fetcher = [[FeedFetcher alloc] initWithURL:value]; | |
fetcher.feedDelegate = self; | |
self.feedFetcher = fetcher; // this releases the previous feedfetcher | |
[fetcher release]; | |
} | |
} | |
} | |
} | |
- (void)getWeather{ | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; | |
NSUserDefaults *nsUserDefaults = [NSUserDefaults standardUserDefaults]; | |
NSString *degUnits = [nsUserDefaults objectForKey:@"DEG"]; | |
FeedFetcher *fetcher; | |
if([degUnits isEqualToString:@"CEL"]){ | |
fetcher = [[FeedFetcher alloc] initWithURL:(NSURL*)@"http://weather.yahooapis.com/forecastrss?w=12763350&u=c"]; | |
} | |
else { | |
fetcher = [[FeedFetcher alloc] initWithURL:(NSURL*)@"http://weather.yahooapis.com/forecastrss?w=12763350"]; | |
} | |
fetcher.feedDelegate = self; | |
self.feedFetcher = fetcher; // this releases the previous feedfetcher | |
[fetcher release]; | |
} | |
#pragma mark - | |
#pragma mark FeedFetcherDelegate protocol | |
- (void)feedFetcher:(FeedFetcher *)feedFetcher didLoadFeed:(NSMutableArray *)array{ | |
//NSLog(@"got feed from %@",array); | |
if([[array objectAtIndex:0] objectForKey:@"condition"] != nil && [[array objectAtIndex:0] objectForKey:@"temperature"] != nil){ | |
condLabel.text = [[array objectAtIndex:0] objectForKey:@"condition"]; | |
tempLabel.text = [NSString stringWithFormat:@"%@ %@",[[array objectAtIndex:0] objectForKey:@"temperature"], @"\u00b0"]; | |
[self setWeather:[[array objectAtIndex:0] objectForKey:@"condition"]]; | |
} else{ | |
ArticleHeadline *deslabel = [[ArticleHeadline alloc] initWithFrame:CGRectMake(10, thingCount, 295, 45)]; | |
deslabel.text = [[array objectAtIndex:0] objectForKey:@"title"]; | |
deslabel.font = [UIFont systemFontOfSize:14]; | |
deslabel.url = [[array objectAtIndex:0] objectForKey:@"link"]; | |
deslabel.editable = NO; | |
deslabel.scrollEnabled = NO; | |
deslabel.backgroundColor = [UIColor lightGrayColor]; | |
[scroll addSubview:deslabel]; | |
thingCount = thingCount + 50; | |
//[namelabel release]; | |
[deslabel release]; | |
} | |
} | |
#pragma mark - | |
- (void)setWeather:(NSString *)string{ | |
NSRange range = [string rangeOfString:@"cloud" options:NSCaseInsensitiveSearch]; | |
if(range.location != NSNotFound){ | |
weatherImage.image = [UIImage imageNamed:@"cloudy.jpg"]; | |
} | |
range = [string rangeOfString:@"snow" options:NSCaseInsensitiveSearch]; | |
if(range.location != NSNotFound){ | |
weatherImage.image = [UIImage imageNamed:@"snowy.jpg"]; | |
} | |
range = [string rangeOfString:@"rain" options:NSCaseInsensitiveSearch]; | |
if(range.location != NSNotFound){ | |
weatherImage.image = [UIImage imageNamed:@"rainy.jpg"]; | |
} | |
range = [string rangeOfString:@"shower" options:NSCaseInsensitiveSearch]; | |
if(range.location != NSNotFound){ | |
weatherImage.image = [UIImage imageNamed:@"rainy.jpg"]; | |
} | |
range = [string rangeOfString:@"sun" options:NSCaseInsensitiveSearch]; | |
if(range.location != NSNotFound){ | |
weatherImage.image = [UIImage imageNamed:@"sunny.jpg"]; | |
} | |
} | |
-(void)refresh{ | |
thingCount = 10; | |
for(UIView *subview in scroll.subviews){ | |
[subview removeFromSuperview]; | |
} | |
[self setUpDefaults]; | |
[self getFeed]; | |
[self getWeather]; | |
} | |
-(void)setUpDefaults{ | |
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; | |
feeds = (NSMutableArray *)[standardUserDefaults arrayForKey:@"FEEDS"]; | |
for(int i=0;i<[feeds count];i++) | |
{ | |
NSLog(@"%@",[feeds objectAtIndex:i]); | |
} | |
} | |
- (void)dealloc | |
{ | |
[super dealloc]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
#pragma mark - View lifecycle | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view from its nib. | |
[scroll setContentSize:CGSizeMake(145, 320)]; | |
[self setUpDefaults]; | |
[self getFeed]; | |
[self getWeather]; | |
} | |
- (void)viewDidUnload | |
{ | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
// Return YES for supported orientations | |
return (interfaceOrientation == UIInterfaceOrientationPortrait); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment