Updating UIWebView off screen? - iphone

I'm trying to get a UIImage of a UIWebView, I can do this fine using renderInContext:. However before I generate the image I would like to update the UIWebView with some HTML code, the trick is the UIWebView is hidden. Right now, I can not get the updated version of the web view to render in the UIImage, just the old one.

Are you waiting until the UIWebView re-renders before querying it again? Loading HTML into a UIWebView does not immediately update the view. You need to wait for webViewDidFinishLoad:. This is true even if the HTML is a simple string with no external references.

Related

Take screenshot of host app using iOS share/action extensions?

I will like to know how to take a screenshot of the iOS host app with the use of a share/action extension.
My use case is as follows:
use the Safari browser to access a webpage (https such as gmail)
tap on the Share button and select the extension
the extension will take a screenshot of the current webpage
An working example for this use case is the Awesome Screenshot iOS app.
I had tried the following methods
reload the baseURI (loadRequest) on a UIWebView/WKWebkit (would not be able to access https contents such as Gmail). So not doing any reload (Awesome Screenshot is not doing a reload btw)
Used the ExtensionPreprocessingJS to obtain the DOM contents through the arguments.completionFunction function. I could not extract the document.body here although i could get the source etc. loadHTMLString with the baseURI will mess up the presentation layer.
Used html2canvas in the ExtensionPreprocessingJS to obtain an image and appended it to the host app's webpage as a child but do not know how to extract it. Also, the image got lost for some webpages such as Gmail.
Mobile Safari does not have the visibleContentsAsDataURL method.
I think a viable approach will be to use the html2canvas in the ExtensionPreprocessingJS but how do I save this image somehow?
Edit: So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.
Here's a solution that I think the Awesome Screenshot app uses:
func captureScreen() -> UIImage
{
// Get the "screenshot" view.
let view = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
// Add the screenshot view as a subview of the ShareViewController's view.
self.view.addSubview(view);
// Now screenshot *this* view.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Finally, remove the subview.
view.removeFromSuperview()
return image
}

Force All Image<img> on UIwebview Display In Progressive Mode

Is there have any way to do this ? I am trying load some local file in the UIwebview, if the content include Image, loadHTMLString is taking much longer to finish page loading just because have to waiting the download of image file.
As I think if the image can be load in progressive mode, the UIwebview can display the html text on the very beginning to prevent the user keep too long time to waiting for the whole page loading.
P.S. The Image is from the internet,but the html string is on the local file
All you need to do is to have the images to be loaded in the Progressive JPEG format. To convert them using ImageMagick you can type:
convert somefile.xxx -interlace JPEG somefile_progressive.jpg
for example.
Another possible solution could be to load the images using JavaScript, once the page has been loaded fully.
Ie just leave the src blank in the html and set it via JavaScript later.
I would really use Javascript. You'd show a blank page or one with just a part of your content and after your document is ready (maybe use jQuery?) you load the other content.
document.getElementById('content1').innerHTML = "<img src="./images/something.png" alt="" />"

Save content of UIWebView

I am working on an app and I need to save the content of UIWebView.
I currently Use stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('html')[0].innerHTML"
to get the html of the UIWebView, but the problem with this method is that it does not save the full content of some webpages.
So my question is How do you save the full content of a webpage including images and css
I think there is already a thread about this topic here:
iphone uiwebview download complete page with CSS and Images
Hope that helps.

Programmatically Generate PDF from HTML on iPhone

I am looking for a way to programmatically (in obj-c) generate a PDF file from a local html file. I am dynamically generating the html from user inputs, I need to create the PDF and send it to the user (via email). I am having difficulty with the PDF generation portion.
I have the code to create a PDF using CGPDFContextCreateWithURL but I am struggling with drawing the page using quartz.
I have searched extensively on SO as well as the internet to no avail.
Any help is much appreciated!
To generate a pdf from an HTML, you need to render the html into a web view, and take snapshots of the web view, and render them into an image context.
The tutorial might be helpful:
http://www.ioslearner.com/convert-html-uiwebview-pdf-iphone-ipad/
I've written a little piece of code that takes an NSAttributedString from DTCoreText, and renders it into a paged PDF file. You can find it on my GitHub Repository. It won't render images or complex html, but it should serve for most uses. Plus, if you're familiar with CoreText, you can extend my PDF frame setter to generate these items.
So what it does now: Give it an HTML string, and it will use DTCoreText to generate an NSAttributedString, then render that into a PDF. It hands back the location that it saved the PDF file in the app's Documents folder.
Why not use a WebService, send the HTML page to this and retrieve the PDF-file ?
That way you can use iTextSharp and C#, and you're done in about 2 minutes.
Plus (if you're evil) you can store and see all the data on your server.
I haven't tried this myself so i have nothing to offer concrete but I'd have to imagine there has to be an easy way to do this on iPhone due to the imaging model. I'd look deeper into the documentation.
As to pushing back with the client that is up to you but there are probably multiple reasons for wanting to keep everything local. Frankly I would not be pleased at all to here from somebody I hired that he couldn't manage this particular task. So think long and hard about this push back. Oh even if you do push back a webserver is a poor choice. I'd go back a step further and investgate why you need something in HTML in the first place.
I've never tried this so I have no idea if it'll work, but how about loading the HTML into a UIWebView, and then make the view draw itself into a PDF context? E.g.
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(...)];
[webview loadHTMLString:html baseURL:...];
Then:
- (void)webViewDidFinishLoad:(UIWebView *)webview {
CGPDFContextRef pdfContext = CGPDFContextCreateWithURL(...);
[webview.layer drawInContext:pdfContext];
...
}
I made it by following this SO: https://stackoverflow.com/a/13342906/448717
In order to maintain the same content's proportions I had to multiply the size of the WKWebView 1.25 times the printableRect's size set for the UIPrinterRenderer, as the screen points differs from the PostScript's... I guess.

No response after submitting form on iPad from UIWebView

I am currently opening a webpage in UIWebView and submitting a form which performs a query on the server side and youtube video is returned (if available). I have no say/control over the server side implementation.
This webview works fine on iPhone/iPod, however, when I try to run the same app on iPad there is no response after submitting the form.
I created a dummy app, compiled on iOS 3.2 and problem is still there.
I put NSLogs in webview delegate methods which shows that after the form is submitted (UIWebViewNavigationTypeFormSubmitted) nothing happens.
I'm unable to figure out why this happens only on iPad and not on iPhone/iPod Touch.
I have created the webview in IB and setting the URL is viewDidLoad
[viewTourWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.realestatetour.com/mobiletours.htm"]]];
The URL for reference is http://www.realestatetour.com/mobiletours.htm
Enter the Tour ID as 10.
Thanks
The reason it wasn't working is because the form opens the result in a new page, (i.e. _blank) and apparantly all links opening in new window are ignored by uiwebview and its delegates.
I found 2 possible solutions for this:
1) Modifying the html using javascript to replace all _blank with _self for all anchor tags.
(source)
2) A more elegant solution which works for handling links which open a new window.
(source)
In my case i recreated the web page displaying form on the iphone app and changed _blank to _self.