iOS access to Safari Reader feature through UIWebView - objective-c

I am using iOS 4.3 & was wondering if there is any way that I can access the Safari's "Reader" feature through which webpages are removed of ads & other riff raff & the content takes the center stage.
If one opens any article in Safari (on say Wikipedia website), then a "Reader" button appears on the URL bar. Clicking on it presents a new window presenting the content beautifully.
How can I leverage this this functionality in iOS through UIWebView ?
PS: I know there is something called Readability Project. But I have no idea how to use this through UIWebView. Also for some websites Safari's Reader takes a call not to enable "Reader" feature, maybe it has no sufficient confidence?

Important: THIS ANSWER NO LONGER WORKS!
Readability shut down on September 30, 2016.
Here is something they recommend as a replacement:
https://mercury.postlight.com/web-parser/
Keeping the answer as a historical reference
--- Original answer ---
You can use Readability mobilizer for this. You will get a cleaned up version of any article, in the Readability styling:
http://www.readability.com/m?url=http://{URLOFTHEARTICLE}
Just prepare the URL and load it in your UIWebView. Here is how it looks in action:
http://www.readability.com/m?url=http%3A%2F%2Fwww.cnn.com%2F2013%2F01%2F11%2Fshowbiz%2Ftv%2Fgolden-globes-tv-vineyard%2Findex.html%3Fhpt%3Dhp_abar

Apple is making a pretty big deal about the inclusion of "Reader" in iOS 5. I'm assuming by the noise it's not available in 4.3
re: How to use through UIWebView
I can't find any mention of it in the Web Content Guide.
There's nothing about it in the UIWebView class reference.
And there's nothing in QA1630.

Dont parse HTML natively on iOS, I have done it before and its a messy business. Either create your own web service to do all the nasty work or look into using readability (readability.com) they provide an API.
There is also an open source ruby, python and php readability port that you can find here
https://github.com/iterationlabs/ruby-readability
https://github.com/gfxmonk/python-readability
http://code.fivefilters.org/p/php-readability/source/tree/master/
For you ruby enthusiasts, readability is also available as a gem, just google it.

Actually reader button do a bit of analysis where it parse the HTML Page and then it sees a clear body tag to parse. If that plugin is able to extract the exact body it will enable the reader button (My understanding from the readability source code). Now to implement the same for webview you just need to embed java script in your code (this java script is already available in the readability source code) and then you can achieve the same effect.
But I suspect the future plan from apple for the same. Because they can not just let anyone else do this content extraction with the huge business opportunity associated with iCloud with the combination of readability.
If you want you can simple extract the HTML from UIWebView and then extract the body and use it for your purpose. It's not a very rocket science to extract.
For analysis point of view, just have randomly some 10 HTML pages with Reader button enabled, you will see the core cotent belongs to body only and rest of the add, header, footer are separated.
I believe this is the time to re-invent the web content we use, and this is the perfect example of doing the same.

You can even do this by injecting javascript.
#define readJS #"(function(){window.baseUrl='https://www.readability.com';window.readabilityToken='';var s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('charset','UTF-8');s.setAttribute('src',baseUrl+'/bookmarklet/read.js');document.documentElement.appendChild(s);})()"
And then when your webpage finishes loading
- (void)webViewDidFinishLoad:(UIWebView *)webview
{
[webview stringByEvaluatingJavaScriptFromString:readJS];

You can do it in iOS9.
first import SafariServices:
#import <SafariServices/SafariServices.h>
Afterwards we are instantiating SFSafariViewController and adding it as a subview. We have two options doing so:
Creating with only base URL
Creating with bas URL as well as entering 'Reading Mode' in case it is available
NSString *sURL = #"http://google.com";
NSURL *URL = [NSURL URLWithString:sURL];
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL]; // 1.
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL entersReaderIfAvailable:YES]; // 2.
[self presentViewController:safari animated:YES completion:nil];

Related

iOS 9 Universal Links - no parameters recieved

Using the new iOS 9 feature - Universal links, from my understanding, is supposed top open my app whenever a specific domain is opened in browser (or other apps?). I have gone through the documentation and through this guide.
However, when the app opens I do not receive the parameter that is meant to help me open the correct page for the user to view....
I would share the code I'm using, but it's quite a big infrastructure and not really a couple of lines of code (server side JSON, plist rows and some IDs on the developer portal).
Anyone encountered it and could give me a hand here, please?
The Branch guide you linked to (full disclosure: I work with the Branch team) unfortunately doesn't cover a rather important step: what to do after your app opens. Which is exactly the issue you're encountering :). But the good news is you've already done the hard part with all the server and entitlement config.
What you need to complete the loop is a continueUserActivity handler in your AppDelegate.m file. This will pass you a webpageURL property containing the actual URL of the Universal Link that opened your app, which you can then parse and use for routing. It'll look something like this:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSString *myUrl = [userActivity.webpageURL absoluteString];
// parse URL string or access query params
}
return YES;
}
Also, when testing keep in mind that Universal Links unfortunately don't work everywhere yet:
P.S., gotta ask...since you found the Branch blog already, had you considered using the service to handle the link routing for you? It can definitely help simplify things!

Open custom links in UIWebView

I have an application that shows presentations that are composed by pages.
Those pages can contain some kinds of media, and one of them is HTML, wich uses an UIWebView component as viewer.
What I need is some way to insert some kind of link into the HTML document so I can jump to another page of my application presentation.
In my research I have found that subclassing UIWebView is not recommended, so I have no clue of how I should handle this.
Many thanks in advance.
Provide:
-webView:shouldStartLoadWithRequest:navigationType:
in your app delegate (ref here).
It will be called each time the web view is about to load new content and give you the chance to do what you need. E.g., this would work if you embed a "file://" kind of URL in your web page:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] scheme] isEqual:#"file"])
return YES;
...

In iOS, how can you programmatically fill out a pdf form field?

I need to take an existing pdf file and programmatically fill in a list of form fields with text and then save the pdf without ever displaying it to the user.
For instance, if the pdf file contains fields named "LastName", and "FirstName" I would like to set the value of "FirstName" to "Louis" and then save the file.
I've been searching for a long time and can't find any guidance on even where to start since the iOS documentation (and most of the questions on here) seem geared towards displaying or creating pdf content instead of modifying it.
EDIT:
My main question is: Is it possible to open a pdf stream (I know how to do this) and copy each existing pdf dictionary item into a new pdf? I have not been able to find a way to write the dictionary items to a pdf.
I doubt that kind of functionality will ever be in the iOS frameworks. The reason most of the related info you can find "seem[s] geared towards displaying or creating pdf content instead of modifying it" is because that's what the vast majority of use cases will want or need for PDF functionality.
You'll need to find a 3rd party library that can open up PDFs, fill out the AcroForm fields, and then stamp out a PDF. I use iText on Java (there is also iTextSharp for C#) but I don't know of anything for Objective-C.
Once you find that library, you'll need to integrate it into your project. There are undoubtedly several related questions/answers here on SO for whatever version of the SDK you're using.
This would be easier to do with a HTML page. If you wish to use a HTML page instead of a .pdf form then thius is how you would go about doing it:
[field1 setText:#"Field 1 Text"];
[field2 setText:#"Field 2 Text"];
NSString *result;
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"$('#field1').val('%#');", field1.text]];
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"$('#pfield2').val('%#');", field2.text]];
result = [webView stringByEvaluatingJavaScriptFromString:#"$('#submitbutton').click();"];
You would need to create two UILabels or UITextFields and call them "field2" and "field2" in your .h file. You can then find the ID of the field you need to replace e.g. #field1 and then put it where I have put "#field1" and again for the second field where I have put "#field2". There also needs to be a UIWebView with the page already loaded. This code is to be used after the UIWebView page has been loaded. Maybe do the following:
-(void)webViewDidFinishLoad:(UIWebView *)webView {
// Insert above code here
}
You probably need a full understanding of Javascript if you want to do this for the whole form, but this should get you started.
Hope that helps you.

Show contents of a zip file in a WebView

I want to have a WebView that displays some static files from the application bundle. Since I have a large number of small files, I'd like to pack them all into a compressed archive so the application doesn't take up too much space. What's the best way to make this happen?
This should help you out: http://code.google.com/p/ziparchive/
To display data in the WebView:
On Mac OS X use WebFrame's loadHTMLString:baseURL:
On iOS use UIWebView's loadHTMLString:baseURL:
What you probably want to do, is implement an NSURLProtocol subclass that will resolve relative URLs by reading them from the zip archive. That way, you only need to initially read the "main" HTML file from the zip into memory, and the others will be read in on demand. To get WebKit to use your custom URL protocols for resolving relative paths, you could instantiate the WebView like this:
[[web_view mainFrame] loadHTMLString:your_main_html baseURL:[[NSURL alloc] initWithString:#"zip:///"]];
Apple has a really good example of combining a custom URL protocol with a WebView here:
https://developer.apple.com/library/mac/#samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816

Prevent Flash in Cocoa WebView

In my Cocoa app, I want to prevent Flash from loading in a WebView, and let the user decide if the Flash should be shown for each page. (That's the same behavior already available through the ClickToFlash plugin or Safari extension. Bundling any of those extensions is probably not an option because of licensing issues.)
Unfortunately most of the Flash I'm trying to avoid is generated from embedded JavaScript specifically designed to prevent easy flash blocking, so I cannot filter the raw HTML for inclusion of Flash objects.
Also, I cannot disable JavaScript for my WebView, as the page I want to display looks completely different when JavaScript is turned off.
Is there a notification/hook I can use to modify the page DOM after JavaScript has been executed, but before the Flash plugin is loaded?
Or should I pursue a different direction?
Thanks,
Ilja
Ideally, you would just define your own WebKit plug-in that handles the application/shockwave-flash MIME type and make your plug-in do nothing.
However, there is unfortunately no way to control the priority of multiple WebKit plug-ins that all register for the same MIME type. The loading order of WebKit plug-ins is totally random and arbitrary, so you cannot guarantee that your plug-in will handle the Flash object instead of the Flash plug-in.
The only way around this that I've found is to subclass WebView and override the private method -_pluginForMIMEType: like so:
#class WebBasePluginPackage;
#interface WebView ( MyFlashPluginHack )
- (WebBasePluginPackage *)_pluginForMIMEType:(NSString *)MIMEType;
#end
#implementation MyWebView
- (WebBasePluginPackage *)_pluginForMIMEType:(NSString *)MIMEType
{
if ( [MIMEType isEqualToString:#"application/x-shockwave-flash"] )
{
return [super _pluginForMIMEType:#"application/my-plugin-type"];
}
else
{
return [super _pluginForMIMEType:MIMEType];
}
}
#end
Then you just need to create a custom WebKit plugin to handle "application/my-plugin-type" and have that plug-in do nothing at all.
Okay, we pretty much figured this out.
Since there is no official API that lets the host app know when JavaScript has finished or control what plugin should load, we are now using custom JavaScript that gets inserted into the received HTML we want to display.
The ClickToFlash Safari extension (not the Internet plugin, which it is based on) was a good inspiration.