UIWebView with NavigationBar - ios

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.

Related

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.

Disable AJAX on only home page with jQuery Mobile

I am looking for a way to simply disable AJAX on ONLY the home page in my project. I am using MVC so there are links to the home page in many areas. I want AJAX for the entire site however not when loading the home page. The reason is because I am using a script to setup the UI dynamically, it contains objects and those objects load when the page loads as a hard refresh but no when jQuery Mobile transitions to the page. I am looking for a solution where I can add a Script to just the home page to disable AJAX whenever it is referenced. But still have AJAX for the rest of the solution. Its a very unique situation but I haven't found anything that could prevent ONE page from being loaded through AJAX. Does anyone know?
if I could I would like to do something like this only on the home page
<script>
$(document).bind(function(){
[some call to disable this page from being loaded with AJAX]
});
</script>
Thanks in advance.
No such thing exists. Think of AJAX like a little mini-browser: if the URL returns a response, it can access it. However, it might be possible to branch on the existence of a request header in your Home action and return something like a 403 Forbidden if an "AJAXy" request header is detected.
jQuery, for example, sends an X-Requested-With: XMLHttpRequest header along with its request. If this header is present, the request most likely came from an AJAX method in jQuery (or other libraries that potentially use the same methodology). You can also pass your own custom headers with the AJAX request, if you want. However, all of this is implementation-specific, so it pretty much depends on all developers on your team doing things a certain way. For example, if someone used a standard JavaScript XHR object to make the request, then the X-Requested-With header would be absent. Or, if you used a custom header, every developer would need to pass that header every time, or again, the logic would break down.
If your business rules say that the home page shouldn't be requested via AJAX, the best way to handle that is at a business level, i.e. policy. Anything you try to in the actual code could easily be worked around.

How can i do like the Pinterest unique modal process?

I have a question that
Point> I don't want to know generally function of modal, but like Pinterest
Question core > 1. Can i change the URL when i use ajax without "reloading"
Work flow >
1) On Pinterest, i click on a image
2) A modal window opens up, but the URL of the page also changes without "reloading"
even though it use ajax
I've known that when i using a modal function, it dosen't change the URL on the page,
but not on Pinterest
URL change e.x) rootURL -> otherURL
3) I click the background of the modal, the URL of the page also changes without "reloading"
and modal windows closed
URL change e.x) otherURL -> rootURL
So, If you have some solution or information, please let me know that.
Also I have red related question answers, but it's not enough. and don't want to get information obout HTML5 API cause i'm not going to provide HTML5 service
How can I duplicate Pinterest website's modal effect?
Pinterest like ajax loading
Changing the Url without a HTML call
Manage in the same url the Full HTML document and a JSON object (or a partial HTML response)
you can do like this http://mazzimo-in-london-eng.blogspot.com/2013/01/the-pinterest-url-sorcery-how-pinterest.html

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

ajax request changing url

I have a pager on a table using ajax and I would like each such request also to change the browser's url, so when I hit refresh button I won't skip back to first page. I was fighting the Url parameter of AjaxOptions, but it keeps winning over me. Please help.
Trim
You can safely change the URL past the hash mark without redirecting the page. However, the user can (in most browsers) navigate through these changes with the Back and Forwards buttons. This technique is usually called "history."
Because the technique is difficult to get working in all browsers, you'll want to use a framework. Take a look at http://www.mikage.to/jquery/jquery_history.html.
I can also recommend ExtJS's history stuff too. Take a look at this example:
http://www.extjs.com/deploy/dev/examples/history/history.html#main-tabs:tab2
Again, notice that not only does the URL change when the user does stuff, but changing the URL (via Back and Forward) also affects the page. This is good, awesome even, but means it must be done very carefully.
There is not really a quick and easy way to do this, here is an article on the topic. The problem is that not only does the Ajax have to generate the URLs, it also has to take those URLs into account when loading the page to get the appropriate content.

Resources