Update webview page number when updating font - ios

I'm working on a book reader iOS application and using webview for displaying epub books. The 3rd party library I'm using for parsing epub book is "KFEpubKit".
I've to give option to user to adjust the font size (as in iBooks), and I've achieved that feature by updating the font of webview as:
let textSizeRule = "document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '\(fontPercentage)%'"
self.webView.stringByEvaluatingJavaScriptFromString(textSizeRule)
The problem I'm facing is that by increasing/decreasing font size, page number of book must also change. But I'm unable to do so.
How can I achieve this functionality in the way other book reading applications are doing it (i.e. kindle, iBook etc)?

You can create a second WebView object which is hidden. You do the same thing ("For time being, i'm reloading the book and computing page number.") in the background webview.
In your webViewDidFinishLoad method, check which webview is loaded
func webViewDidFinishLoad(webView: UIWebView) {
if webView == backgroundWebview {
//Compute number of pages
} else {
//Foreground webview
}
}
So the user is not disturbed by a reloading webview.
Hope this helps!

Related

WKWebView crashes at 30M of RAM usage on iOS 9

I have a WKWebView that loads a custom HTML file from our server. In our app there's a button to change the web page's font size using JavaScript by iterating through all elements and set their style property(this should be irrelevant since on other iOS it works perfectly).
On iOS 9 devices, when the font gets larger and larger, the webview gets progressively slower to respond to the font change. For example, changing from 22px to 24px takes 2s to take effect, and changing from 24px to 26px can take 3s or more. Eventually, when the font size gets to 28px, the webview would crash the app with a memory warning in the console. If not connect to Xcode, however, it would not crash but turn into a blank screen as described in this similar post.
This is not a duplicate since I'm asking for something different:
I have implemented webViewWebContentProcessDidTerminate and can confirm it gets called, but when I try to reload the webView it simply crashes again and fall into a crash-reload-crash loop. The Xcode panel shows my memory is using 31M of RAM, which like 5% for my iPhone 4S, so this shouldn't be a true memory issue. The web page consists of paragraphs of texts only, so is this a system bug or something that I'm doing wrong? Please comment if you need more details.
Here's my code:
class ReaderController: UIViewController {
// loads webView, injects JavaScript, handles button, etc.
func loadData() {
let html = // get html
webView.loadHTMLString(html, baseURL: htmlURL)
}
}
extension ReaderController: WKNavigationDelegate {
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
loadData()
}
}

Webview scaleToFitPage doesn't scale page to fit in the webView

I am building a simple iOS application in Swift which is built with a webView inside the view. When load a Motion jpeg (mjpeg) in the webView and set
let reqObj = URLRequest(url: url! as URL)
vidView.delegate = self
vidView.scalesPageToFit = true
vidView.loadRequest(reqObj)
the pages load as I want in the right scale but after 1-2seconds the web view zooms in without any reason??? It seems to work but goes back and fourth between the scale I want and the zoomed in scale that i down want. I have read that one can set something to the html meta but don't understand how if this is the problem???
This is what I want it to be like
but this is what It goes to after a few seconds
This is what I don't want it to be like
The web view goes between these 2 content the whole time
Please if someone could help me with this matter and maybe knows what the problem is.

UICollectionView & AVPlayer Performance

I have a UICollectionView whose cells each have an AVPlayerLayer, all playing a video at the same time. About 9 cells fit on the screen at a time and it's pretty laggy so I need ways to boost the performance to achieve smooth scroll. There's a few things I've tried already:
1) Each cell has only one instance of AVPlayer. It doesn't get created, instead, player.replaceCurrentItemWithPlayer is called when the video url changes.
2) Because I'm using ReactiveCocoa, it's trivial to skip repeat urls to avoid playing the same video twice.
What else can I do to speed up the scroll and performance?
First I want to say it's a bit crazy to see several players in action at the same time: it's a heavy task of rendering anyway.
As far as I know, simply scaling/re-framing the video to smaller size doesn't make it less content to render: if you have a 100x100 picture to render and you render it in a frame of 10x10, it still consumes the same amount of memory as the original picture would consume; same thing goes for videos. So, try to make your video assets having similar resolution as the frame where you would present them.
Store each cell in an NSCache or NSMapTable using the NSIndexPath as the key for each; then, whenever you need a cell, call one directly from the cache or map table.
Obviously, you'll have to create all the cells at once, but you'll get the same scrolling performance as you do with nothing in the cells at all.
Need sample code?
Here's my latest iteration of a perfectly smooth-scrolling collection view with real-time video previews (up to 16 at a time):
https://youtu.be/7QlaO7WxjGg
It even uses a cover flow custom layout and "reflection" view that mirrors the video preview perfectly. The source code is here:
http://www.mediafire.com/download/ivecygnlhqxwynr/VideoWallCollectionView.zip
Unfortunately, UIKit will hit bottlenecks and drop frames when put under pressure for this use case. If you can muster refactoring code to use Facebook's AsyncDisplayKit / then this would be your best bet.
For objective-c / this project here is a good reference.
https://github.com/TextureGroup/Texture/tree/master/examples/ASDKTube
- (ASCellNode *)tableNode:(ASTableNode *)tableNode nodeForRowAtIndexPath:(NSIndexPath *)indexPath
{
VideoModel *videoObject = [_videoFeedData objectAtIndex:indexPath.row];
VideoContentCell *cellNode = [[VideoContentCell alloc] initWithVideoObject:videoObject];
return cellNode;
}
there is swift code available if you dig deep enough in github.

How to avoid hours being marked as link to calendar?

I am using a UIWebView to fetch a webpage that contains the log of musics previous played at my radio station. The original file is here. It appears on the app as follows: Image
How can I force it to stay with the hours in white and do not create a link to the calendar?
Thanks in advance
Since you're loading it in a UIWebView you can turn off data detection for the UIWebView:
UIWebView *webView = // Set this up as you wish
webView.dataDetectorTypes = UIDataDetectorTypeNone;
This is all described in the UIWebView documentation

webViewDidFinishLoad did finish loading but didn't finish showing content on screen

I'm using webViewDidFinishLoad a lot in my app and there's something about UIWebView that really bugs me, well, actually two things.
The first, when I load new content to a UIWebView I will see for half a second the last page that was loaded to the same UIWebView what will force me to "clean" the UIWebView using something like:
[_mainWebView stringByEvaluatingJavaScriptFromString:#"document.open();document.close();"];
before loading the new content.
The second issue I have and that's the main issue for this question is that if i'll load some new content to my UIWebView and do something like this:
[_mainWebView loadHTMLString:htmlString baseURL:nil];
...
- (void)webViewDidFinishLoad:(UIWebView *)webView {
_mainWebView.alpha = 1;
}
In some cases the UIWebView will show up white for half a second before showing up the content. I'm guessing that the content is already loaded into the UIWebView and that's why webViewDidFinishLoad:webView is firing but for small html pages showing to content takes loner than the actual load. Is there any workaround I can use to avoid the blank screen that is showing for a sec or so but still save that second?
I thought about animating the alpha from 0 to 1 but that solution feels kinda lame to me.
Try adding a javascript callback so you know when the web view contents have actually loaded: Javascript in UIWebView callback to C/Objective-C

Resources