IOS How to find full rss feed link with nsscanner class - ios

I am working on fetching data from rss feed based project.From searching on google i found that generally RSS link found in this format in source of HTML.
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://feeds.abcnews.com/abcnews/topstories" />
so, I have to use nsscanner class to find the link of RSS feed from HTML source. but i don't know proper pattern and which i have to set scanUpToString: and haracterSetWithCharactersInString: or etc.
So, please help me how to i find the full link of RSS feed.
Here is my try:
- (void)viewDidLoad {
NSString *googleString = #"http://abcnews.go.com/";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL encoding:NSASCIIStringEncoding
error:&error];
NSLog(#"%#",[self yourStringArrayWithHTMLSourceString:googlePage]);//will return NSMutableArray
}
-(NSMutableArray *)yourStringArrayWithHTMLSourceString:(NSString *)html
{
NSString *from = #"<a href=\"";
NSString *to = #"</a>";
NSMutableArray *array = [[NSMutableArray alloc]init];
NSScanner* scanner = [NSScanner scannerWithString:html];
[scanner scanUpToString:#"<link" intoString:nil];
if (![scanner isAtEnd]) {
NSString *url = nil;
[scanner scanUpToString:#"RSS Feed" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:#"/>"];
[scanner scanUpToCharactersFromSet:charset intoString:nil];
[scanner scanCharactersFromSet:charset intoString:nil];
[scanner scanUpToCharactersFromSet:charset intoString:&url];
NSLog(#"%#",url);
// "url" now contains the URL of the img
}
return array;
}
currently i am able find only link with this code .
output:
But full link is :-
http://feeds.abcnews.com/abcnews/topstories

That is because
[NSCharacterSet characterSetWithCharactersInString:#"/>"];
contains characters "/" which is the last character of http://
and also the character right after feeds.abcnews.com.
Edit: Here's a playground which shows the approach you could take.(Not fully tested)
It's in Swift but the API is the same in Obj-C.
var str = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS Feed\" href=\"http://feeds.abcnews.com/abcnews/topstories\" />";
var scanner = NSScanner.init(string: str);
var result: NSString? = nil
scanner.scanUpToString("href=\"", intoString: nil);
scanner.scanString("href=\"", intoString: nil);
scanner.scanUpToString("\" />", intoString: &result);

Use "link" instead of "a" tags from this reference.
Reference : Regular expression in ios to extract href url and discard rest of anchor tag

Related

Getting multiple tags from html source code in Objective-C

I have extracted the source code from a website but i would like to display the strings of three urls. I have managed to strip the code so the only url's are the ones I need. How can I get the three strings in an array. The URL's look like this: Example
where I need to extract the string: 'example'
I have tried the NSScanner but without any luck. Please advice
Not the most clever way, but you can get the first approach of > and then the first < from there. All with standard NSString methods like rangeOfString: and such.
This code with NSScanner should give you luck :)
-(NSMutableArray *)yourStringArrayWithHTMLSourceString:(NSString *)html {
NSString *from = #"<a href=\"";
NSString *to = #"</a>";
NSMutableArray *array = [[NSMutableArray alloc]init];
NSScanner* scanner = [NSScanner scannerWithString:html];
for(int x=0;x<3;x++) {//You said only 3 strings
NSString *tempString;
[scanner scanUpToString:from intoString:nil];
[scanner scanString:from intoString:nil];
[scanner scanUpToString:to intoString:&tempString];
NSString *str = [tempString substringFromIndex:[tempString rangeOfString:#"\">"].location+2];
[array addObject:str];
}
return array;
}
usage:
for example:
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://facebook.com"] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"%#",[self yourStringArrayWithHTMLSourceString:html]);//will return NSMutableArray
Here is how to convert NSMutableArray to NSArray if you would like to to that:
NSArray *array = [NSArray arrayWithArray:mutableArray];

Extract one word from a two word string

I have a two word string in another view controller containing a user defined first and last name
NSString *userName = ([self hasAttributeWithName:kContractorName] ? [self attributeWithName:kContractorName].value : [self.certificate.contractor.name uppercaseString]);
when retrieving this string in another view controller I want to extract only the first name.
I researched SO on using scanner and found a very helpful answer here: Objective C: How to extract part of a String (e.g. start with '#'), and im almost there.
The problem is I can only seem extract the second name with my variation on the origial code. Im scanning my string up to the space between first and second name, this returns the second name fine. Just need a nudge now on how to set this to extract the first name instead of the second
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:userName];
[scanner scanUpToString:#" " intoString:nil]; // Scan all characters before
while(![scanner isAtEnd]) {
NSString *name = nil;
[scanner scanString:#" " intoString:nil]; // Scan the character
if([scanner scanUpToString:#" " intoString:&name]) {
// If the space immediately followed the , this will be skipped
[substrings addObject:name];
}
[scanner scanUpToString:#" " intoString:nil]; // Scan all characters before next
}
Better use NSString's componentsSeparatedByString method:
NSString* firstName = [userName componentsSeparatedByString:#" "][0];
If first and last name are separated with a space you can use:
NSArray *terms = [userName componentsSeparatedByString:#" "];
NSString *firstName = [terms objectAtIndex:0];
You could just split the string into first and last names using componentsSeparatedByString.
NSArray *subStrings = [userName componentsSeparatedByString:#" "];
NSString *firstName = [subStrings objectAtIndex:0];
Sure, you can just split the string by spaces and take the first element, but where’s the fun in that? Try NSLinguisticTagger to actually split this using a Cocoa API:
__block NSString *firstWord = nil;
NSString *question = #"What is the weather in San Francisco?";
NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames;
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes: [NSLinguisticTagger availableTagSchemesForLanguage:#"en"] options:options];
tagger.string = question;
[tagger enumerateTagsInRange:NSMakeRange(0, [question length]) scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass options:options usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
firstWord = [question substringWithRange:tokenRange];
*stop = YES;
}];

Extract substring from a string in iOS?

Is there any way to extract substring from a string like below
My real string is "NS09A" or "AB455A" but i want only "NS09" or "AB455" (upto the end of numeric part of original string).
How can i extract this?
I saw google search answers like using position of starting and endinf part of substring we can extract that ,But here any combination of "Alphabets+number+alphabets" .I need only " "Alphabets+number"
Perhaps not everybody will agree, but I like regular expressions. They allow to specify
precisely what you are looking for:
NSString *string = #"AB455A";
// One or more "word characters", followed by one or more "digits":
NSString *pattern = #"\\w+\\d+";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:NULL];
NSTextCheckingResult *match = [regex firstMatchInString:string
options:NSMatchingAnchored
range:NSMakeRange(0, [string length])];
if (match != nil) {
NSString *extracted = [string substringWithRange:[match range]];
NSLog(#"%#", extracted);
// Output: AB455
} else {
// Input string is not of the expected form.
}
Try This:-
NSString *str=#"ASRF12353FYTEW";
NSString *resultStr;
for(int i=0;i<[str length];i++){
NSString *character = [str substringFromIndex: [str length] - i];
if([character intValue]){
resultStr=[str substringToIndex:[str length]-i+1];
break;
}
}
NSLog(#"RESUKT STRING %#",resultStr);
I tested this code:
NSString *originalString = #"NS09A";
// Intermediate
NSString *numberString;
NSString *numberString1;
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"];
[scanner scanUpToCharactersFromSet:numbers intoString:&numberString];
[scanner scanCharactersFromSet:numbers intoString:&numberString1];
NSString *result=[NSString stringWithFormat:#"%#%#",numberString,numberString1];
NSLog(#"Finally ==%#",result);
Hope it Help You
OUTPUT
Finally ==NS09
UPDATE:
NSString *originalString = #"kirtimali#gmail.com";
NSString *result;
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *cs1 = [NSCharacterSet characterSetWithCharactersInString:#"#"];
[scanner scanUpToCharactersFromSet:cs1 intoString:&result];
NSLog(#"Finally ==%#",result);
output:
Finally ==kirtimali
Use NSScanner and the scanUpToCharactersFromSet:intoString: method to specify which characters should be used to stop the parsing. This could be in a loop with some logic or it could be applied in conjunction with setScanLocation: if you already have a method of finding the start of each section you want to extract.
When using scanUpToCharactersFromSet:intoString: you are looking for the next invalid character. It doesn't need to be a 'special' character (in a unicode sense), just a known set of characters that aren't valid for the content you want. So, you might use:
[[NSCharacterSet characterSetWithCharactersInString:#"1234567890"] invertedSet]
You can use - (NSString *)substringWithRange:(NSRange)aRange method on NSString class to get a substring extracted. Use NSMakeRange to create the NSRange object.

Is this NSScanner 's bug?

The code snippet is as follows:
unichar chars[] = {0x0030, 0x0031, 0x0032, 0x003B, 0x0E31};//the testString is "012;" plus a thai character
NSString *testString = [[NSString alloc] initWithCharacters:chars length:5];
NSLog(#"testString %#", testString);
NSScanner *theScanner = [NSScanner scannerWithString:testString];
NSString *result = nil;
[theScanner scanUpToString:#";" intoString:&result];
//[theScanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:#";"] intoString:&result];
NSLog(#"the result is %#", result);
using scanUpToString failed, however, using scanUpToCharactersFromSet works. And if the character after 0x003B is not 0x0E31, for example ,0x0030, both api works.
So I guess scanUpToString has a bug dealing with some characters.
Does anyone has better ideas?
Thank you.

iOS string manipulation to remove the HTML tag

I have the following string...
Overall: 21 (1,192,742<img src="/images/image/move_up.gif" title="+7195865" alt="Up" />)<br />
August: 21 (1,192,742<img src="/images/image/move_up.gif" title="+722865" alt="Up" />)<br />
I need to remove the HTML tag, is there a way I can say remove everything between <img and />?
Are you wishing to remove all of the HTML content from your string? If so, you could approach it in the following manner:
- (void)removeHtml:(NSString *) yourString
{
NSString *identifiedHtml = nil;
//Create a new scanner object using your string to parse
NSScanner *scanner = [NSScanner scannerWithString: yourString];
while (NO == [scanner isAtEnd])
{
// find opening html tag
[scanner scanUpToString: #"<" intoString:NULL] ;
// find closing html tag - store html tag in identifiedHtml variable
[scanner scanUpToString: #">" intoString: &identifiedHtml] ;
// use identifiedHtml variable to search and replace with a space
NSString yourString =
[yourString stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat: #"%#>", identifiedHtml]
withString: #" "];
}
//Log your html-less string
NSLog(#"%#", yourString);
}
I'm not sure if this will work on iPhone (because initWithHTML:documentAttributes: is an AppKit addition) but I've tested it for a Cocoa app
NSString *text = "your posted html string here";
NSData *data = [text dataUsingEncoding: NSUnicodeStringEncoding];
NSAttributedString *str =
[[[NSAttributedString alloc] initWithHTML: data documentAttributes: nil] autorelease];
NSString *strippedString = [str string];

Resources