NSURLSession is not downloading full webpage - html

I'm currently attempting to download a webpage using a NSURLSession in order to retrieve a status update on the download progress. Unfortunately, after downloading the webpage, when I go to load the webpage, there are visual issues (missing images, missing javascript, missing styles, etc), leaving the webpage looking broken and in complete. When loading directly to the webView, everything loads correctly. I would like to know if there is a way (or anything I'm missing) to download ALL aspects of the webpage and load them up so I may load the full webpage and display a loading bar while the page loads.
- (void)loadRequest:(NSURLRequest *)request
{
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
self.urlSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];
self.downloadTask = [self.urlSession downloadTaskWithRequest:request];
[self.downloadTask resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL
{
[_webView loadData:[NSData dataWithContentsOfURL:downloadURL] MIMEType:downloadTask.response.MIMEType textEncodingName:downloadTask.response.textEncodingName baseURL:nil];
}

I was having similar problems (missing images etc.). You need to provide a baseURL. Try:
[_webView loadData:[NSData dataWithContentsOfURL:downloadURL] MIMEType:downloadTask.response.MIMEType textEncodingName:downloadTask.response.textEncodingName baseURL:downloadTask.response.URL];

In order to display a loading bar you have to make some kind of workaround, because you are not able to know the progress of the resources that are being downloaded.
Many apps use tricks, to simulate that there is a loading ongoing.
Here is a simple example to ilustrate the idea:
Display loading bar with progress 30% for 1 second.
If the request did not finish, display progress 90% until the progress finishes
When the request finishes, animate progress to 100%.
You can have a look on this open source library to see how it's done. You can do it in a similar way.
https://github.com/ninjinkun/NJKWebViewProgress

Related

How to reload an MVC page?

This is an MVC razor page, that works fine on first load.
If I use a reload from javascript or even a refresh in the browser menu (Chrome), I get
Server Error in '/' Application.
>The view 'actionChangeUserSentence' or its master was not found or no view
>engine supports the searched locations. The following locations were searched:
>~/Views/Conduct/actionChangeUserSentence.aspx
>~/Views/Conduct/actionChangeUserSentence.ascx
>~/Views/Shared/actionChangeUserSentence.aspx
>~/Views/Shared/actionChangeUserSentence.ascx
>~/Views/Conduct/actionChangeUserSentence.cshtml
>~/Views/Conduct/actionChangeUserSentence.vbhtml
>~/Views/Shared/actionChangeUserSentence.cshtml
>~/Views/Shared/actionChangeUserSentence.vbhtml
> and so on...
meaning the url string is considered a non MVC url.
I tried several possibilities to reload, including:
-location.reload();
-window.location.reload(false); with both false and true
-location.href = location.href;
-history.go(0);
By the way, the reason I need to refresh is that js drawing on a html canvas doesn't work except after full page (re)load, reason still unknown. But anyway, a browser menu refresh should of course always work, I guess...
Thanks for any help.
I will suggest put that code in on ready function of drawing canvas may this will help you
Use just an Response.Redirect("/<your page>");

webpage surfing with page curl animation ios

i have a html file that contains some data,since i display this in a webview the user has to scroll down to view the information,what i want to know is whether i can provide a page curl animation to view the contents of the html so that it becomes easier for the user to read the information by swiping across pages,
should i use pageviewcontroller? or are there any other libraries that let me have the page curl effect on a custom(webview)? how do i proceed? is it possible to show some amount of html information on one page and then user turns to the next page? how do i achieve that,its a single html file
If you want that sort of paging, then maybe you could try the leaves library https://github.com/brow/leaves You can use it to page through various UIWebView's
As you can see in the leaves sample you have to render the page in the renderPageAtIndex.
You can then use something like:
- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
CGSize screensize = CGSizeMake(320,480);
UIGraphicsBeginImageContext(screensize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scalingFactor = screensize.width/webView.frame.size.width;
CGContextScaleCTM(context, scalingFactor,scalingFactor);
[webView.layer renderInContext: ctx];
}
You do have to load the webview first.

Black screen appears when viewing HTML5 the second time on UIWebView using iOS6

I'm displaying two HTML5 documents on my iPad application, using a UIWebView on iOS 6.0.1.
One of them has no problems and I can open and close the webview as much as I want.
The other one can be displayed once and then when I close my web view and want to re-open the document again, the webview shows a black screen.
I haven't prepared the HTML5 documents myself and I don't have much knowledge on HTML5, so I can't tell the difference between them that causes this behavior.
Here's how I create my webview and how I load the HTML5:
UIWebView *theWebView = [[UIWebView alloc] init];
theWebView.scalesPageToFit = YES;
theWebView.clearsContextBeforeDrawing = YES;
theWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
//adjust frame and add to view controller's view here
NSURL *url = [NSURL URLWithString:#"http://www.handypdf.net/davsan"];
[theWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0]];
Everytime I open an HTML5 document, I re-create the web view. So I don't re-use it.
To fix the problem, I tried cleaning the cache, cookies, etc.. explained on these SO answers, but it didn't work.
Here are the links to the problematic and the not-problematic HTML documents:
This one works ok.
This one doesn't work the second time.
Also, if you want to check these HTML5 pages online here are the links to them:
This one works ok.
This one doesn't work the second time.
This problem does not happen on iOS 5.x. It's also replicable using the iOS simulator.
What would you suggest to fix it?
Thx
Not a satisfying answer, but here's how I progressed with this problem:
I couldn't solve this for a couple of days. Then I deleted all my code and re-wrote the functionality from scratch, and then it was solved. I couldn't understand the exact reason, because I didn't write anything fancy on my second attempt, so unfortunately I can't tell how it's solved :(
Here is what I just did:
Create the web view once, and cache it:
+(UIWebView *)getTheWebView {
if (theWebView) return theWebView;
theWebView = [[UIWebView alloc] init];
theWebView.scalesPageToFit = YES;
theWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
return theWebView;
}
Then, when I want to show the web view:
UIWebView *theWebView = [HTML5Manager getTheWebView];
theWebView.frame = self.view.frame;
[self.view addSubview:theWebView];
And here's how I load the URL:
[theWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
This is just it and it works. I know it doesn't make sense but I hope it helps you, too...

How to handle HTML content in Windows 8 Metro App

I'm designing a Windows 8 Reader App, and I have to use a control to show the HTML content, which is fetched from some website feeds. Cause those HTML content may contains images or some other formatted text, now I'm using a richtextblock to show the HTML content, but it costs a lot of time to parse the HTML content.
So I'm wondering if there is any controls that can handle the HTML content except the WebView.
Thanks.
Updated:
The reason I can't use WebView is that I need to implement pagination, like the image belowed:
As JP Alioto mentioned you should use the WebView control.
You can use the NavigateToString method to load the HTML. Or use Navigate to request a URI.
There are issues however with using the WebView control, specifically it is rendered differently and is not a standard control, this means things like your app bar or settings pane will not render on top of the WebView, there is a workaround by using the WebViewBrush to "paint" the WebView to standard control such as a rectangle when needed.
Also you can make a screenshot of the webpage you want to display. But to make a screenshot of webpage it's also not easy to do, but I offer you to make it with some special sites wich are created to take screenshot of other websites. Then you can download an image this sites return and open and display it in your windows 8 app. I show You some example how to I did that:
StorageFolder screens = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFolderAsync(#"Screens\" + folderName, CreationCollisionOption.GenerateUniqueName);
var downloader = new BackgroundDownloader();
IStorageFile file = await screens.CreateFileAsync(fname, CreationCollisionOption.GenerateUniqueName);
string my_uri = "http://api.snapito.com/web/e3c351d5994134eb1aea855ce78e296c3292d48a/lc/" + url + "?type=jpeg";
DownloadOperation download = downloader.CreateDownload(new System.Uri(my_uri), file);
await download.StartAsync();
I think there are only two options but none of them are really good:
Use WebView and transform your HTML with CSS and other techniques to look native. Use the ScriptNotify and NavigationStarting and other events to navigate to another page. In W8.1 the WebView is much better (eg. treated as regular control not floating over all other controls,...)
Parse your HTML and generate native elements. I started such an implementation and created a XAML control to display HTML with native controls (see https://mytoolkit.codeplex.com/wikipage?title=HtmlTextBlock). However if you have complex HTML (eg iframes, etc.) this may not work and you have no other choice than to use the WebView control.

Why is my UIWebView empty in my unit test?

I'm putting app-generated content into a UIWebView, and trying to test that I'm doing it correctly. Here's the HTML string I'm putting into the web view:
#"<html><head></head><body><p>Come, let me clutch thee. I have thee not, and yet I see thee still. Art thou not, fatal vision, sensible to feeling as to sight?</p></body></html>"
And it gets into the web view thus:
[self.webView loadHTMLString: [self HTMLStringForSnippet: model.body] baseURL: nil];
where model.body contains just the <p/> element, and -HTMLStringForSnippet: wraps it into well-formed HTML. In my test, I rely on the answers to this question to retrieve the HTML content of the body via JavaScript:
- (void)testCorrectHTMLLoadedInWebView {
UIWebView *bodyView = controller.webView;
NSString *bodyHTML = [bodyView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
STAssertEqualObjects(bodyHTML, model.body, #"Model body should be used for the web view content");
}
While I can see by stepping through the code that the UIWebView is created correctly and is passed the correct HTML in -loadHTMLString:baseURL:, in the test bodyHTML is empty. So what do I have to do to see the actual content I expect and previously passed to the web view?
It takes a noticeable time for the UIWebView to render therefore the problem may be that you access the content before it is fully loaded.
Hook on UIWebViewDelegate's webViewDidFinishLoad: method to make sure the content is ready.
Update:
Maybe WebViewJavascriptBridge will bring some help.