Accessing UIWebView contents from UI Automation in Instruments - iphone

I have a UIWebView in my application which has links inside that when clicked cause different actions to be carried out in the application. I'd like to be able to test this with automated testing so it can be added to a continuous integration build. However, the documentation is rather sparse for UIAWebView and I've tried to no avail to get this to work.
Has anyone had any success interacting with links inside a UIWebView so you can check that their action is correct?

Couldn't find a way to iterate/access links in a UIWebView but would this work for you? Access the page source with:
NSString *source = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('html')[0].outerHTML"];
then look for the tags that you consider to be links (e.g. href=...)
Then you still need to be able to "press" those links. Is it enough that you navigate to the url given in the link? If so, just use loadRequest to do so. If you need more advanced behaviour (for example make sure correct javascripts are run), you'll need to do more magic to parse those and execute them with stringByEvaluatingJavaScriptFromString.

Related

Best way to hide content from UIWebview?

I have a UIWebView into a mobile website that I run, and would like to hide some links when it's seen by the WebView--but not mobile Safari. It seems like there's several approaches to this:
Evaluate custom Javascript to hide elements of a certain class.
Pass in a GET parameter so that the server does it.
Pass in a custom header so that the server does it.
Maybe something else...
To me it seems like they all have their tradeoffs. What's the best way of doing it?
If you want to do this, I'd say detecting the UA with JavaScript is probably the way to go because it will be added automatically, and you can concentrate on the website, rather than having to add code to both.
Also, you don't have to check every time a link is clicked to add a custom header or GET parameter.
What I do to make the distinction between UIWebview and mobile Safari is using the userAgent (a custom one for the webView). Based on the UserAgent I display different content for each "platform"

Embeddinig another website into a GWT application

I'm building a GWT application that looks great. At the end of the page I'd like to include a WordPress blog that I'm developing. In order to do that I want to use a com.google.gwt.user.client.ui.Frame so that an IFrame gets created, and the Wordpress just gets seamlessly embedded.
I've built this and it seems to work, although there's a problem with the size of the IFrame. I'm not sure how it's getting sized, but it's too small so that scrollbars are thrown onto it. This makes the the embedding not so seamless.
What should I be doing so that this second webiste can be seamlessly embedded into my GWT app?
The easiest solution is to make iframe as big as possible, and hope that there will be no unexpected scrollbars.
Also there is another way, if you have access to the innerDocument of iframe (if SOP doesn't stop you from doing this), you can compare its size against the size of iframe, and increase iframe height/width if necessary.
If blog is in another domain, you might try to use cross-document messaging to retrieve information about the size of document(this will not work in IE7,IE6 and you will have to put some additional JS on your blog).
Also you can include information from your blog without using iframes. You can simply pull RSS of your blog, parse it and display it in any way you like it.

How to Delete nodes from HTML in iOS

What I am trying to do is to load a webpage into in a UIWebView. The problem is that I need to do some preprocessing on the html before displaying it in the web view.
The UIWebview loadHTMLString is quiet slow when the html is big. I don't need to display the full page therefore i am trying to remove some html nodes before displaying it in the web view to speed up the loading time.
I don't think using regex for that is a wise idea. I checked out NSXMLParser and TFHPPLE but I couldn't find any way to remove nodes from the html tree using an XPath or something.
I know I could do that using Javascript but that won't solve my problem. I also don't have no control on the website so I can't edit in the webpage itself.
Is there something as easy as deleteNodeUsingXPath or something :)
Cheers and thanks a lot for your help in advance.
One possibility solution: do a proxy website which strips out unwanted stuff. The iphone accesses the proxy website URL. The proxy website loads from the original website, strips out unwanted stuff, and replies with the remaining stuff.
There is a tool called Objective-C-HTML-Parser that will do what you are looking for. The documentation is thorough, and the implementation is pretty straight-forward.
Basically, you take your HTML string and make an HTMLParser object that you can then manipulate however you want. It is a very powerful library that basically lets you do whatever you want with HTML with easy-to-use Objective-C APIs.
Good luck!

How do I implement a Floodlight tag into an iPhone app?

A client is asking me to insert a Floodlight tag into an Objective-C programmed iPhone app that is ready for submission to the App Store.
I did some Googling, and couldn't find anything about how to do this (it seems like you can you only add Floodlight tags via Javascript, but I'm not too sure). Can you do this, and, if so, how?
If nothing else, you can create an offscreen UIWebView and invoke the appropriate javascript with stringByEvaluatingJavaScriptFromString. Not a good solution, but if javascript is required it may be your only choice.
Instead of embedding a UIWebView right off the bat for this, you could try using NSURLConnection to request the URL normally inserted with the Floodlight iframe. It seems that unless the contents of the iframe (which should just be a transparent GIF) need to interact with the host page, simply performing and receiving the request should fulfill the purposes of tracking. Check the ToS, though, to see if they have any stipulations about this.

UIWebView - how to get visited links to display as visited?

I have a UIWebView in my iPhone app, which merrily shows web content, but it does not render visited links any differently to unvisited links. Ordinarily I'd expect it to use a different colour for visited links, but alas no.
Safari on the other hand deals with its links correctly. I assume that UIWebView is lacking a store of information about what URLs it has visited. It's reasonable to expect the delegate to supply this information, but there is no such API on the delegate, or anywhere else that I can see.
So, how can I get UIWebView to show visited links as visited?
I came to the conclusion that the only way to get this functionality is to fake it.
I got this working successfully by adding a JavaScript function to all the pages to apply a "visited" class to all links whose target is on a list that I pass in from Objective-C (with UIWebView's stringByEvaluatingJavaScriptFromString: method). Then I just have to maintain that list in the Objective-C world and always fire off that JS function in the web view delegate's webViewDidFinishLoad: method. The hardest bit was the JavaScript.
Aside from changing CSS properties in javascript by call stringByEvaluatingJavaScriptFromString: there is probably no supported way to do it in the official SDK. UIWebView is very limited compared to WebView on the desktop.