So I have a sort of complex problem. I’m working on an iPad app for a web application. We have already created a mobile-friendly version of the web app, but would also like to have a native app, so that’s the current project.
The app has a relational grid it displays with entities used for analysis. The way to add entities in mobile safari and other desktop browsers is to click a link on the top of the app, which opens a popup with a search box. The user searches for the topic, and then checks off which entities to add to the grid. The user then hits a button within the popup, and the window disappears and populates the grid with the selected entities.
However, I’m having a lot of trouble figuring out how to have the two windows communicate. As of now, I have a new UIWebView open up when that particular link is clicked, but the parent-child connection is not made like it is in the browser version. Therefore I can select entities to add, but when I hit the button, the entities are not added to the grid. I’m not sure how to get around this.
Another issue is that after the link at the top of the page is clicked, I attempt to add entities, and then I go back to the original page, the link that should trigger the popup doesn’t work anymore.
Any help would be appreciated.
If I understand correctly your issue, i.e., how to make to
UIWebViewsend messages one another, I would that by creating an intermediate controller.Your controller would act as a
UIWebViewDelegatefor bothUIWebViews and its responsibilities would be:creating the second
UIWebView(hosting the popup window) by intercepting-webView:shouldStartLoadWithRequest:navigationType:(what I think you are already doing);intercept the click on your popup window and then using
– stringByEvaluatingJavaScriptFromString:to retrieve whatever information it needs;pass that information to the first
UIWebViewby means of– stringByEvaluatingJavaScriptFromString:.I have no information as to what kind of data is passed back from the second page to the first one, so I cannot be more detailed.
Anyway, I think that using a second
UIWebViewfor the popup window will simply breaks the DOM parent/child relationship (because you have two of them), so you have to resort to some more manual processing or modify the design of your web app.EDIT:
After reading your comment:
UIWebView is really greedy as to touches. If intercepting clicks on links through
-webView:shouldStartLoadWithRequest:navigationType:is not enough, you can resort to overriding-(void)sendEvent:(UIEvent*)eventin your ownUIWindowderived class; but this is necessary only if you mix a UIWebView and a, e.g., UIScrollView or when you want to handle multi-touches; look here for more info;here an example of how you can execute javascript:
NSString *jsCall = [NSString stringWithFormat:@"yourFunction(%@);", arg];[webView stringByEvaluatingJavaScriptFromString:jsCall];You can use this in several different ways:
EDIT 2:
A test suggestion: put the following code in your delegate class:
The HTML contains the following snippet:
it will give you a first alert meaning that js parsing was ok, and a second meaning that the f function was called from webViewDidFinish. I did a simple text getting a web page and adding the javascript snippet to it, and it worked.