Reload a UIWebView every request? - ios

I have a UIWebView and I'm trying to get links to open in Safari. The usual code works, just not with m.youtube.com. So I'm trying a different approach - is there a way of, every time the webView is loading, the page is completely reloaded but with the new URL instead? This is the only solution I can think of.
Here is the question I'm referencing:
Open in Safari from UIWebView (using youtube.com)
Thanks.

Related

UIWebView with NavigationBar

Imagine there is a list of posts and if you click a postItem, then a detailed post view shows up. For example,
/index
[post1, post2, post3 ...]
post1 is clicked
/posts/1
Hello! I am a post!
Also, the website that will be included inside the UIWebView is a single page web app, which makes things a little tricky. Since the webapp renders parts instead of redirecting, navigation doesn't necessarily make another web request. For regular websites that make a new url request for every navigation, I can use either webViewDidStartLoad or shouldStartLoadWithRequest, having one ViewController per webpage. A naive solution for my case would be forcing a fresh redirect for every navigation, using window.location = url.
However, that loses the performance boost coming from rendering specific parts. A better way to do it is attaching a navigationItem such as title and back button dynamically when the user moves away from the index page. The problem is, how do I detect whether the user moved to /posts/1 from /index if I am not doing a full redirect?
Thank you so much in advance!
For your last idea to bind the title and back button you could use the following, but you will need to modify the website code.
You can communicate with your native controller from the webview using a custom protocol / scheme of url.
The idea is to make a call like window.location = 'myapp://thisismyaction/thisisanarg' when you need to update.
Once the call is made you intercept it with -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType.
In this method you parse your request to detect if it start with myapp:// and you parse the line to get your action and your args. The syntax is up to you like regular queryString or else as in my example. You do your update and the title based on what you parse and you return NO to prevent loading a non-existant URL.
To make sur that this call is only happening from webview and not a browser, you could inject some javascript object when you webview finish loading with stringByEvaluatingJavaScriptFromString method from the webview, and check if the object is defined before making the call from the javascript.

Using history.back() in UIWebView but loaded pre page contains very old data

IOS 8.0+
My app use UIWebview to visit a forum site.
The forum front page contains two tag: default one is 'Recommend', another one is 'Newest'. Using ajax to load post data When user switch to 'Newest' tag, at the same time the page runs the follow codes to save state:
window.history.pushState(null, title, url);
After that if user is interesting with some post, he/she would click link and jump to a new page to view detail of the post. In this page, I use follow code to let user have a way to go back to 'Newest':
history.back(-1);
Now interesting thing happened, go back to 'Newest' tag but post data is old.
After some tests, I found the data is always be the first time loaded after installed the app. This bug will not happen in Safari or Android app.
I believe it should have some relationship with webView cache.But why UIWebview does't use new cache? And how to
solve this problem?
Oh no...nobody meet this problem?
My current solution is:
use js code to auto refresh the page when go back.

URL change but not request webview ios

everyone.
I am building an hybric app that using web and Xcode. First time, I use a webview to open web app in which it contains a menu (on main screen). I use shouldStartLoadWithRequest to get request URL before it request. Then when user touch a button on that menu, I see that URL changes but shouldStartLoadWithRequest method not get called, I need to get URL in that method before it requests to know where it will navigate. Can anyone explain for me? I have no knowledge about web. Thank you very much.
As I understand it, the function shouldStartLoadWithRequest gets called only for urls which is going to load content or, load new webview frame.
There are few kind of urls which might be assigned to javascript that doesn't actually load a page, may not work. Do you need to intercept it before the link navigation occurs.
Also it would help provide better answers if you gave details of the action (<a href="..."> or javascript) that the manu link will perform.
I can not figure out what is the problem but the following is my trick:
I detect that the URL changes but it does not request data. I use UITapGestureRecognize to detect when user taps to webview and perform an action after delay 0.01s to get URL. At this time, URL change to new URL of new page. After get URL I implement corresponding to that URL.

UIWebView : Load percentage information

I need to get information on loaded data percentage when UIWebView loads page.
I know that UIWebView does not provide corresponding API.
I would like to know is there any workaround ?
One approach is to use an external framework (ASIHTTP, AFNetworking) that gives you some progress information to get the data, then load it into the UIWebView using – loadHTMLString:baseURL:.
ASIHTTP, although not actively developed anymore, has a very nice class for your case:
ASIWebPageRequest - download complete webpages, including external resources like images and stylesheets. Pages of any size can be indefinitely cached, and displayed in a UIWebview / WebView even when you have no network connection.

Why are Google search results in UIWebView not triggering webViewDidFinishLoad?

This is no doubt a more generic issue regarding web pages, but it is easily demonstrated with Google content.
When entering some search criteria on Google's home page the results are not triggering the webViewDidFinishLoad method. The same problem occurs when the coloured Google logo is replaced with some artwork linked to a feature page.
I suspect the page is not being fully loaded due to some javascript or ajax code, so is there a way of detecting this?
This is because Google are using AJAX to do this, the webViewDidFinishLoad delegate method is called when effectively a new resource is loaded into the webview - the equivalent to a page load in your browser, since Google are AJAXing this stuff, it isn't causing that method to get triggered.
There isn't any way to detect when parts of a page are loaded (javascripts, style sheets or ajax responses).
One thing you could be able to do is execute a line of javascript onto the web view that tells you the height of the page, by waiting until the height of the page changes you could know when a result has occurred, you will have to devise some other - more smart check of course when a search query is modified, but my idea would work for the initial google logo screen to the first results page.
Executing that JS every few seconds should be enough.
Hope that helps

Resources