Created
December 12, 2012 23:34
-
-
Save soundsmitten/4272700 to your computer and use it in GitHub Desktop.
Objective-C Getting titles and descriptions in RSS feed
This file contains hidden or 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
-(void) retrieveFromInternet { | |
NSURLRequest * request = [NSURLRequest requestWithURL: [self feedUrl]]; | |
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; | |
NSString * pageSource = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding]; | |
// NSLog(@"%@", pageSource); | |
[self setNewsStories: [self getHeadersAndSnippetsFromSource: pageSource]]; | |
} | |
- (NSArray *) getHeadersAndSnippetsFromSource: (NSString *) pageSource { | |
NSArray * feedItems = [pageSource componentsSeparatedByString:@"<item>"]; | |
NSMutableArray * titlesAndSnippets = [[NSMutableArray alloc]init]; | |
for (NSString * item in feedItems) { | |
//put title and description in array of titles and snippets. | |
NSMutableDictionary * newsItem = [[NSMutableDictionary alloc]init]; | |
[newsItem setObject: [self getTitleFromItem: item] forKey: kTitle]; | |
[newsItem setObject: [self getDescriptionFromItem: item] forKey:kDescription]; | |
[titlesAndSnippets addObject:newsItem]; | |
} | |
return titlesAndSnippets; | |
} | |
// <title>auea</title> | |
- (NSString *) getTitleFromItem:(NSString *)item { | |
NSRange titleOpen = [item rangeOfString:@"<title>"]; | |
NSRange titleClose = [item rangeOfString:@"</title>"]; | |
NSRange titleRange; | |
titleRange.location = titleOpen.location + titleOpen.length ; | |
titleRange.length = titleClose.location - titleRange.location; | |
return [item substringWithRange: titleRange]; | |
} | |
- (NSString *) getDescriptionFromItem:(NSString *)item { | |
NSRange descriptionOpen = [item rangeOfString:@"<description>"]; | |
NSRange descriptionClose = [item rangeOfString:@"</description>"]; | |
if (descriptionOpen.location ==NSNotFound) | |
return @"No Description Available."; | |
NSRange descriptionRange; | |
descriptionRange.location = descriptionOpen.location + descriptionOpen.length; | |
descriptionRange.length = descriptionClose.location - descriptionRange.location; | |
return [item substringWithRange: descriptionRange]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment