If I load request with large content in UIWebView, app becomes unresponsive after it finishes downloading content and starts rendering it. The loaded document also has some Java script content which will be executed on document load.
Also, app crashes if I minimize the app by pressing home button.
How to solve these issues?
Take a look at http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial
In "Grand Central Dispatch in Practice" section describes your problem and a best-practice solution.
Best regards!
Related
//The point
I gradually load many objects (UIWebView) to the memory and after a while it crashes. Please Help
//Details
I have a UIScrollView with horizontal paging, wherein each page (with a separate view controller) contains a UIWebView with disabled user interaction. At first only webview #0 on page 0 gets loaded and as I advance through the pages the app gradually sends requests to load specific data. Downloaded data stay in memory.
After I load about 20 Webviews from internet source, my app freezes and crashes. If I use a dummy html string stored on the device, this happens after I load about 50 webview. But the problem persists.
I fixed much of the leaks and am only left with Mallocs. Since the problem does not occur with low quantities of UIWebviews Loaded I assume that I have to change my code to say 3 webviews and reuse them. But I don't know if that'd fix the issue. I have developed several apps before which don't crash and this is a unique case.
I store pointers to webviews and some other elements inside every Webview (such as labels) in arrays. The arrays get incremented in count with every new page loaded.
I use xcode 4.3 and a jailbroken iphone 4 to develop. I turned off requiring signatures in my xcode as a result of which the app gets copied onto my phone yet fails to attach to process. I cannot debug on my phone. My app works well in simulator but crashes a while after I launch it on the phone by tapping the icon.
From what I am able to describe as so, what do you think might be the problem?
Thanks
Yes, certainly having too many UIWebViews will cause a crash due to low memory. I'm not sure what your reason is for having multiple UIWebViews, but I would follow through with your idea to reuse a smaller amount of them instead of creating several.
Based on your device not crashing when you use a small amount, I would say that the problem lies in the use of too many UIWebViews and not in a compatibility problem with your jailbroken device, for example.
WebKit is notorious for its memory hogging. It mallocs large blocks of memory and fails before giving you the opportunity to release other resources. One UIWebView is bad enough. I've had this problem in apps with as few a 5 UIWebViews, let alone 50.
There are a couple of things you can try. One, if the web pages aren't too complicated, you could use a replacement view such as DTCoreText to render them. If you have to have webviews, you could cache the various HTML contents and re-render using one UIWebView.
You only need to use three UIWebView's for paging. Lets say the user is on page 2 (page 1 is the left page and page 3 is to the right). When the uses swipes to page 3, you could just display the web view currently on page 1 on page 3. You need to re-load the proper html when the user swipes between the pages.
With some caching of the web content, speed shouldn't be a problem once the stuff is downloaded.
I have a UIWebView with some links in it. When I click the link, there's a delay of about 2-3 seconds before the link is loaded. Is this how it's suppose to be or is there a setting I can change?
Thanks.
In iOS5 the UIScrollView belonging to a UIWebView has been exposed so that you can change its behavior. So to remove the click delays you can simply do:
[webView.scrollView setDelaysContentTouches:NO]
This way you don't have to use any javascript libraries to remove the delay.
As a bonus, you can make the scrolling in a UIWebView feel a bit more native by changing the decelerationRate:
[webView.scrollView setDecelerationRate:UIScrollViewDecelerationRateNormal]
If you mean that after clicking the link there is a delay before the the link is highlighted, this has to do with how the touches are handled by the UIWebView. I doubt that this can account for the whole 2-3 secs delay, but anyway refer to this post to remove it.
If you mean that after clicking on a link it takes 2-3 seconds until you can see the page rendered, then it is (unfortunately) normal. UIWebView is known for being slow at rendering even a simple page.
Check if you are calling the load method on main thread or not. If you are testing it on Simulator then calling a method on secondary thread takes some time to execute rather than crashing or throwing an exception. Replace such call (if any) by performSelectorOnMainThread method.
I've a big problem with the iPhone application that I'm developing.
When you run the application you see a image-stream from different users.
Actually, when you start my app or you click the refresh button you see a modal round rectangle with "Refreshing..", when finish the refreshing, you can see all images. But this takes too times, specially for slow connections.
Now checkout this screenshot from different apps:
The applications are Nuji, Picplz and Instagram.
When you run these application you see instantaneously the stream and when you scroll down and the other images get focus, the image start loading and you see an Activity Indicator.
I want something like this, I want an activity indicator while images loading and maybe, for increase application speed, start loading images on focus and stop loading the others.
I hope I explain myself well.
Please if you can help me.
Thank you very much and sorry for my english.
Bye.
You might look into the Three20 framework on Github https://github.com/facebook/three20. Using that framework you can get that capability for free, using TTImageView.
Updated: Also I thought of another place I've seen this capability. The developers at enormego offer a solution too. http://developers.enormego.com/view/what_if_images_on_the_iphone_were_as_easy_as_html
My app loads webpages off the internet using a UIWebView. However I'd like to modify the experience so the user won't perceive it to be a webpage. What are some of the things I can do? and how to do it?
examples:
- Show the page when everything is loaded?
- Fade in effect?
Suggestions please.
It is possible to monitor the delegate callbacks from UIWebView to determine when the page has finished loading, and then perform whatever visual effect you want at that time. If the web view is most or all of your view controller, you could defer pushing your view controller until that time, or you could fade in the view as you mentioned.
However, even when reading HTML from the local disk, there is a noticeable lag. Waiting for remote content to load will have even more latency. You also must deal with the realities of an unreliable network: sometimes, your content will never arrive.
Once the first page is loaded and displayed, though, your options will be limited. If you try to make it appear that it is a fully native application, it's going to be hard to explain the latency or why it doesn't run when the network is unavailable (airplane mode, no cell or Wi-Fi).
I assume you are wanting to display the page as if it is a resource contained within your app (and not something streaming from a browser?)
In this case, download the webpage to a local file and display it only once fully downloaded.
I provide sample code in this answer that you can easilyy adapt for a html page (rather than a pdf document)
Lets suppose I am creating an application for the iphone with a webView down at the bottom of the window (the other part of the screen has a button and the user can interact with it).
I don't want the webView to stop the user from interacting with the other part of the UI when the webView loads a new url. From my limited testing through the iphone simulator, I haven't been able to determine IF it already behaves this way. Most of my web sites load pretty fast.
I seem to be able to load new requests and click the ui button while that happens.
So, again, do I need to worry about threading in this case?
No, you do not. The iPhone threads a great deal of the UI components behavior, or schedules them for you in the main run loop in such a way that you rarely need to be concerned, the UI elements will be available for user interaction.