Strip down webpage to just text on iOS (Objective C) - html

My main goal here is to achieve an effect like Readability or Safari's Reader service where the main content of the webpage is converted to text. I don't actually want to display any images, just get all the webpage's important text. I am currently using some pretty long self-built code to parse the webpage for the s to find out what the heading may look like, and I am also parsing the s that I hope contain the majority of the page's content.
-(void)interpretAndDisplay {
NSURL *URL = [NSURL URLWithString:self.url];
NSData *data = [NSData dataWithContentsOfURL:URL];
NSString *html = [NSString stringWithUTF8String:[data bytes]];
//Getting the H1s
NSMutableArray *h1Full = [[NSMutableArray alloc] init];
h1Full = [self stringsBetweenString:#"<h1" andString:#">" andText:html];
if ([h1Full count] > 0) {
NSMutableArray *h1Content = [[NSMutableArray alloc] init];
h1Content = [self stringsBetweenString:[NSString stringWithFormat:#"<h1%#>",[h1Full firstObject]] andString:#"</h1>" andText:html];
NSMutableArray *h1Sanitize = [[NSMutableArray alloc] init];
h1Sanitize = [self stringsBetweenString:#"<" andString:#">" andText:html];
if ([h1Content count] > 0) {
NSString *finalTitle = [h1Content firstObject];
for (int i = 0; i < [h1Sanitize count]; i++) {
NSString *toRemove = [NSString stringWithFormat:#"<%#>",[h1Sanitize objectAtIndex:i]];
finalTitle = [finalTitle stringByReplacingOccurrencesOfString:toRemove withString:#""];
finalTitle = [finalTitle stringByReplacingOccurrencesOfString:#"\n" withString:#""];
}
finalTitle = [self sanitizeString:finalTitle];
[self.titleLabel setText:finalTitle];
}
}
//Now for the body!
NSMutableArray *pTag = [[NSMutableArray alloc] init];
pTag = [self stringsBetweenString:#"<p" andString:#">" andText:html];
if ([pTag count] > 0) {
NSMutableArray *pContent = [[NSMutableArray alloc] init];
pContent = [self stringsBetweenString:[NSString stringWithFormat:#"<p%#>",[pTag firstObject]] andString:#"</p>" andText:html];
NSMutableArray *pSanitize = [[NSMutableArray alloc] init];
pSanitize = [self stringsBetweenString:#"<" andString:#">" andText:html];
if ([pContent count] > 0) {
for (int i = 0; i < [pContent count]; i++) {
NSString *pToEdit = [pContent objectAtIndex:i];
for (int i = 0; i < [pSanitize count]; i++) {
NSString *toRemove = [NSString stringWithFormat:#"<%#>",[pSanitize objectAtIndex:i]];
pToEdit = [pToEdit stringByReplacingOccurrencesOfString:toRemove withString:#""];
}
[pContent replaceObjectAtIndex:i withObject:pToEdit];
}
for (int i = 0; i < [pContent count]; i++) {
NSString *pToEdit = [pContent objectAtIndex:i];
pToEdit = [pToEdit stringByReplacingOccurrencesOfString:#"\n" withString:#""];
[pContent replaceObjectAtIndex:i withObject:pToEdit];
}
NSString *finalBody = #"";
for (int i = 0; i < [pContent count]; i++) {
if ([finalBody isEqualToString:#""]) {
finalBody = [NSString stringWithFormat:#"%#",[pContent objectAtIndex:i]];
}
else {
finalBody = [NSString stringWithFormat:#"%#\n\n%#",finalBody,[pContent objectAtIndex:i]];
}
}
finalBody = [self sanitizeString:finalBody];
[self.textLabel setText:finalBody];
}
}
}
The above code extracts all of the elements just fine and sanitizes them with a method that I created, but the problem is that just analyzing the P tags sometimes completely fails to simplify the content, and analyzing all possible content tags could mess with the content's order and layout.
Is there a better way or some framework that converts all the text into a nice string?
EDIT
Searching around, I found a Boilerpipe project that can extract text extremely easily (https://github.com/k-bx/boilerpipe/wiki/QuickStart). It looks as easy as this: String text= ArticleExtractor.INSTANCE.getText(url);
Can I do this on Objective C?
Edit 2
It appears that there is a boilerpipe API, but this has limited requests. I am mostly looking for a user-side solution.

Reggie is not the most tolerant approach in my opinion.
I'd try to find an existent open source (i.e. https://github.com/Kerrick/readability-js) and use WebKit to inject JS into a web page once loaded.
After that you can inject another JS, extracting the processed content (using the appropriate class from the source)
Then, using JavaScriptCore you can pass div's content to Objective-C (JS offers many ways of doing that)

Related

Make UIWebview's font bold in iOS

How can i make the font to bold
[cell.itemContentWebView loadHTMLString:[NSString stringWithFormat:#"<div style='font-family:-apple-system','HelveticaNeue-Bold; style='font-size:24px;'>%#",object.title] baseURL:nil];
Rafay for your question
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Font size
int fontSize = 80;
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[webview stringByEvaluatingJavaScriptFromString:jsString];
//Font Family Style - Bold
NSString *jsWebViewFontFamilyString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.fontFamily = 'HelveticaNeue-Bold'"];
[webview stringByEvaluatingJavaScriptFromString:jsWebViewFontFamilyString];
//Font Color or web view text color
[webview stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].style.color = 'Green'"];
}
Just prefix a tag to your string before loading into the webView.
NSString *body = [plist objectForKey:#"foo"];
NSString *htmlString =
[NSString stringWithFormat:#"<font face='GothamRounded-Bold' size='3'>%#", body];
[webView loadHTMLString:htmlString baseURL:nil];

iOS UIWebView not scaling content

I have a UIWebView that is always smaller than the device's screen. Whatever I tried, it just won't scale the website to fit its bounds.
Here is my code:
self.webView = [[UIWebView alloc] initWithFrame:
CGRectMake(.08 * width, .18 * height, .84 * width, .74 * height)];
self.webView.delegate = self;
self.webView.scrollView.delegate = self;
self.webView.scrollView.scrollEnabled = NO;
self.webView.scalesPageToFit = YES;
NSString *urlAddress = #"https://number26.de/app-tutorial/dist/new-feature-invite.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view.window addSubview:self.webView];
That is how the result looks like:
And this is how it SHOULD look like (Google Chrome / Dev mode):
okey, this is my code
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[UIWebView alloc] init];
self.webView.frame = CGRectMake(.08 * self.view.frame.size.width, .18 * self.view.frame.size.height, .84 * self.view.frame.size.width, .74 * self.view.frame.size.height);
self.webView.delegate = self;
self.webView.scrollView.delegate = self;
self.webView.scrollView.scrollEnabled = NO;
NSString *urlAddress = #"https://s.number26.de/app-tutorial/dist/new-feature-invite.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view addSubview:self.webView];
self.webView.scalesPageToFit = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self scaleToFit];
}
-(void)scaleToFit
{
if ([self.webView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[self.webView scrollView];
float zoom=self.webView .bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:YES];
}
}
and see full website in webView. ]
add this code
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self scaleToFit];
}
-(void)scaleToFit
{
if ([self.webView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[self.webView scrollView];
float zoom=self.webView .bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:YES];
}
}

creating new NSDictionaries programmatically

I want to create a set of new NSDictionaries with different names dependant upon input data. Is this possible? E.g.
for (int i = 0; i < [holidayDestination length]; i++) {
NSMutableDictionary *[NSString stringWithFormat:#"holidayDestination%d", i] = [[NSMutableDictionary alloc] init];
// Other code here...
}
Thanks in advance.
No, while Objective-C strives to be like Smalltalk, it's only a thin veneer over C, so you really can't do these kinds of cool "meta" tricks. What you can do instead is set up a mutable array and for each element at i create your mutable dictionary. Something like this:
NSMutableArray *holidayDestinations = [NSMutableArray new];
for (int i = 0; i < [holidayDestination length]; i++)
{
[holidayDestinations addObject:[NSMutableDictionary new]];
NSMutableDictionary *working = [holidayDestinations objectAtIndex:i];
// Add elements to "working"
}

Display multiple markers on the Google Map in Objective C

How can I display multiple markers on the Google Map in iOS?
I used the following approach, but it didn't work.
for (int i = 0; i < [array count]; i++)
{
pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:0] componentsSeparatedByString:#","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0] componentsSeparatedByString:#","] objectAtIndex:1] floatValue]);
[_map animateToLocation:pointsToUse[i]];
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = pointsToUse[i];
[_map animateToLocation:pointsToUse[i]];
[_map addMarkerWithOptions:options];
}
You're using [array objectAtIndex:0] (in two places), when I think you should probably be using [array objectAtIndex:i]?
Also, you probably don't need the calls to animateToLocation?
I tried your code. This seems to work fine. Just delete the object at index 0 after passing it values to pointsToUse.
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"12.981902,80.266333",#"12.982902,80.266363", nil];
CLLocationCoordinate2D pointsToUse[5];
for (int i = 0; i < [array Size]; i++)
{
pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:0] componentsSeparatedByString:#","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0] componentsSeparatedByString:#","] objectAtIndex:1] floatValue]);
[array removeObjectAtIndex:0];
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = pointsToUse[i];
[mapView_ animateToLocation:pointsToUse[i]];
[mapView_ addMarkerWithOptions:options];
}
Try this:
-(void)plotMutliplePinsonMap
{
mapView_ = [[GMSMapView alloc]initWithFrame:CGRectMake(0, 96, 320, 450)];
for(int i=0;i<[arrTobeShown count];i++)
{
double_lat = [[[arrTobeShown objectAtIndex:i]valueForKey:#"latitude"] doubleValue];
double_long = [[[arrTobeShown objectAtIndex:i]valueForKey:#"longitude"] doubleValue];
GMSMarker *mkr = [[GMSMarker alloc] init];
if (double_lat !=0 && double_long!=0)
{
[mkr setPosition:CLLocationCoordinate2DMake(double_lat, double_long)];
[mkr setTitle:[[arrTobeShown objectAtIndex:i] valueForKey:#"name"]];
[mkr setSnippet:[[arrTobeShown objectAtIndex:i] valueForKey:#"address"]];
[mkr setMap:mapView_];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:double_lat longitude:double_long zoom:5];
mapView_.camera=camera;
}
}
[self.view addSubview:mapView_];
[mapView_ setHidden:YES];
[self.view layoutIfNeeded];
}
Yes both of you are correct. According to your suggestions I changed the code and it works.
But the problem is, I set the zoom and the zoom is fixed. If the two locations are far, I can't see both locations on one screen ( i need to pinch to see both). How can I see both locations at the same time?
My code is shown below.
-(void) displayMapwithPositionfortheArray:(NSMutableArray*) array
{
CLLocationCoordinate2D firstPoint = CLLocationCoordinate2DMake([[[[array objectAtIndex:0] componentsSeparatedByString:#","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:0] componentsSeparatedByString:#","] objectAtIndex:1] floatValue]);
GMSCameraPosition *currloc = [GMSCameraPosition cameraWithLatitude:firstPoint.latitude
longitude:firstPoint.longitude
zoom:8
bearing:0
viewingAngle:45];
_map = [GMSMapView mapWithFrame:CGRectZero camera:currloc];
_map.myLocationEnabled = YES;
_map.frame = CGRectMake(0, heightOffset, self.view.frame.size.width, self.view.frame.size.height - heightOffset);
[self.view addSubview:_map];
CLLocationCoordinate2D pointsToUse[[array count]];
for (int i = 0; i < [array count]; i++)
{
pointsToUse[i] = CLLocationCoordinate2DMake([[[[array objectAtIndex:i] componentsSeparatedByString:#","] objectAtIndex:0] floatValue],[[[[array objectAtIndex:i] componentsSeparatedByString:#","] objectAtIndex:1] floatValue]);
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = pointsToUse[i];
[_map addMarkerWithOptions:options];
}
}

How can I perform Paging in UIWebView?

I have ten local HTML files in my Resource folder and I want to performing paging effect like ScrollView and ImageView so please help me how I can do that. I have displayed HTML page in webView, but i don't have any more ideas. My code is (but it's for only One page):
arrHtmlPage=[[NSMutableArray alloc] initWithObjects:#"2",#"3",#"5",#"6",#"8",#"9",#"11",#"13",nil];
NSBundle *bundle = [NSBundle mainBundle];
NSString *htmlTemplateLink = [bundle pathForResource:#"2" ofType:#"htm"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlTemplateLink]];
wview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 768, 940)];
wview.scalesPageToFit = YES;
[wview loadRequest:request];
wview.delegate=self;
//wview.multipleTouchEnabled=YES;
wview.userInteractionEnabled=YES;
[self.view addSubview:wview];
const CGFloat kScrollObjHeight = 900;
const CGFloat kScrollObjWidth = 766;
const NSUInteger kNumImages = 11;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIWebView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
- (void)viewDidLoad {
[super viewDidLoad];
arrHtmlPage=[[NSMutableArray alloc] initWithObjects:#"",#"2",#"3",#"4",#"5",#"6",#"8",#"9",#"11",#"12",#"13",#"14",nil];
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
int i;
for (i = 0; i <[arrHtmlPage count]; i++)
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *htmlTemplateLink = [bundle pathForResource:[arrHtmlPage objectAtIndex:i] ofType:#"htm"];
NSLog(#"htmlTemplateLink==>%#",htmlTemplateLink);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlTemplateLink]];
wview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 766, 940) ];
wview.backgroundColor=[UIColor redColor];
wview.tag=i;
wview.scalesPageToFit = YES;
[wview loadRequest:request];
wview.userInteractionEnabled=YES;
[scrollView1 addSubview:wview];
}
[self layoutScrollImages];
}
-(IBAction)buttonPressed:(id)sender
{
int pageNo = [sender tag] ; // current page no
int tempNo = pageNo%10 + 1; // total count
CGPoint bottomOffset = CGPointMake((tempNo-1) * 768,0);
[scrollView1 setContentOffset:bottomOffset animated:YES];
}
In xcode 4.3.1 create a new project. File -> New project -> Page-Based application. Drag and drop a UIWebView onto the storyboard.