How to submit a form in a UIWebView loaded with HTMLString:baseURL:? - iphone

oHi,
I'm displaying a html page which contains a form with some input text fields and a submit button.
If i display the page using loadRequest: everything works fine.
If i display the same page using loadHTMLString:baseURL: the submit button is not working anymore.
The adress which is supposed to be called is :
http://..../lire.php?id=33570#bloc_commentaire
If i log the adress it tries to reach i got :
applewebdata://BF6F3D92-9F36-40CA-A3EB-BCD3F14852B6#bloc_commentaire
Do you have any idea of what i should do to be able to post a form loaded statically using loadHTMLString:baseURL: ?
Thanks,
Vincent

The problem occurs because your web view doesn't know where to post the data because the html is loaded from a string and not a specific web server.
Make sure you are setting baseURL correctly in your call to
[webView loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL]
Details:
When the webview finds a relative url in the html, it joins it with the baseURL to form an absolute url which can be used to submit or click on. For instance:
Given http://www.test.com/foo/ as baseURL and a relative url tag lke click me
When the user clicks on the link, the view will make a request to http://www.test.com/foo/bar/hello.php

Related

How to trigger camera from a remote html page loaded in childbrowser in ios ?

I need to access camera from a remote html page. I load this html page through child browser . Need to open camera or any native view on a button click on html file..
No sure if this is the best/most elegant possibility, but it should work:
By click on button load an specific URL.
The UIWebView has delegate methods to inform you, when a page finished loading.
In the – webViewDidFinishLoad: method check if the loaded URL was your specific button URL, if YES show camera/any other native view.

Opening URI in overlay, not main page

On my page, overlays are loaded by inserting their content with jQuery and then fading in.
What I want to do is this:
When you click to open an overlay, an URI is loaded (e.g. news/12, where news is the category and 12 is the id of the item to load).
Except, instead of loading it in body, it should be loaded in the overlay.
In other words, I want to achieve something like on facebook, where you open an overlay, the url changes, but the main page stays the same.
I'm guessing you need ajax for this, but I have no idea whatsoever how to do it.
Thanks
It sounds like you want to use the new history.pushState(...) and history.popState(...) browser API.
This SO post might help you out: Change the URL in the browser without loading the new page using JavaScript
Use Boxy. See http://onehackoranother.com/projects/jquery/boxy/tests.html#
AJAX example:
<a href='#' onclick='Boxy.load("test-1.html");'>Test 1</a>
See this question: Ajax - How to change URL by content
I solved it thanks to Lethargy's answer.
.pushState() is exactly what I need to have the URL reflect the contents of the overlay that is dynamically created with jQuery.
With some tweaking around and debugging I managed to get it all working.
Now my overlays (or popups, dialogs, whatever) are search engine ready, and the url is copy-pastable for users :)

Is it possible to call a URL without a visible view?

Is it possible to load a URL without a visible view?
Basically I'm trying to submit some information to my server by a php page. It works from my webViewController, which has a working view, but not from my appDelegate, which doesn't have a visible view.
I recommend you use ASIHTTPRequest to submit the information to your php page.

How to push a view from a web page?

Suppose my app will load a local web page which is stored in my app bundle. I want to push the view or present a view when the user click a link or an image inside that web page. How to do this? Let's make this be general question: how to communicate with my app from a local web page?
Thank you.
Use "webview.request.URL.absoluteString" to get the request-string of the clicked link in the delegate-method "webViewDidFinishLoad:" or "webViewDidStartLoad:".
then you can scan this url for some special substring.
For example you could make a link like ".../index.php?iPhone_action=abcdef".
In the delegate-methods you can check if the link has the substring "?iPhone_action=" and if it does, then put the part of the link, whick follows "?iPhone_action=" in a NSString.
(in our example it would be "abcdef").
Depending on the value of this NSString you could fire a action in your app.
There is an undocumented method to catch the event when you click on a link in UIWebView. You can do whatever you want to do in that method.Search on google/stackoverflow for it. Or see my answer in this stackoverflow post.
Show alert view when click on a link

How to tell the difference between an iframe loading vs. entire page loading in a UIWebView?

In the [webView:shouldStartLoadWithRequest:navigationType:] event, how can you tell the difference between an iframe that's loading content vs. the entire page loading content? It seems like the both call this same event with the same arguments, the only difference being the URL that's going to be loaded, so there is no way to tell the difference between the two.
I've thought of a few ways to tell the difference though:
Parse the source code of the page and store a list of all the iframe src attributes, assume that if the url loading is one of those in my list it's an iframe. Of course this assumption might be incorrect if it actually does navigate to the page.
Same as above, but run some JavaScript on the page and to get the iframe src attributes.
Using JavaScript, set up hooks for whenever a user tries to navigate to a page, similar to http://niw.at/articles/2009/02/06/how-to-enable-the-popup-window-on-uiwebview/en
Using JavaScript, set up hooks right before an iframe tries loading data (not sure how I would do something like this).
Modify each iframe's src attribute to include a special string at the end of it's src attribute e.g. "#iframe-loading". Load the UIWebView with this modified source code. Although this will only work for iframes that existed on the original page as it was queried the first time, not those that are loaded dynamically, e.g. via JavaScript.
Let the page load normally by returning YES in the [webView:shouldStartLoadWithRequest:navigationType:] event, and once it's done loading, see if the WebView's URL changed or not... if it did that means it was a page redirect if not it probably means it was an iframe that was loaded.
Using JavaScript, override the window.location property setter to run my own code before it will actually change the window's location. I could then communicate with Objective-C to let it know that the next load is actually going to be a redirect. Is this the only way to redirect in JavaScript though?
What's the best way to do this? Can you think of any ways I haven't thought of? Do iframes have a special event/property I could manipulate with JavaScript to help me out?
Thanks
Update: It's not as simple as just checking the navigationType. Although clicking a link to open up a new page will show up as navigationType = 0, a JavaScript redirect (changing window.location) will show up as navigationType = 5. An iframe load also shows up as navigationType = 5. So when navigationType = 5, you don't know if the entire page changed URLs via JavaScript or if it is simply an iframe loading on the same page.
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
BOOL isFrame = ![[[request URL] absoluteString] isEqualToString:[[request mainDocumentURL] absoluteString]];
}
I just used this method:
Let the page load normally by returning YES in the [webView:shouldStartLoadWithRequest:navigationType:] event, and once it's done loading, see if the WebView's URL changed or not... if it did that means it was a page redirect if not it probably means it was an iframe that was loaded.
I've successfully used the HTTP "referer" (the actual header is misspelled) field to detect this. This should either be the URL of the main page (available from the webview) or something else -- probably a frame.
NSString *referer = [request.allHTTPHeaderFields objectForKey:#"Referer"];
NSString *currentPage = [webView.request.mainDocumentURL absoluteString];
BOOL isFrameLoad = [referer isEqualToString:currentPage] == NO;