[TIMOB-2578] iOS: WebView event beforeLoad.url property
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Trivial |
Status | Closed |
Resolution | Duplicate |
Resolution Date | 2012-03-05T10:50:35.000+0000 |
Affected Version/s | Release 1.5.0 |
Fix Version/s | n/a |
Components | iOS |
Labels | n/a |
Reporter | Jérémy R |
Assignee | Neeraj Gupta |
Created | 2011-04-15T03:23:15.000+0000 |
Updated | 2017-03-22T20:36:11.000+0000 |
Description
When a user click on a link inside the WebView, the received event URL property isn't correctly setted.
Code:
webview.addEventListener('beforeload', function(e) {
Titanium.API.info(e);
});
Output:
source = "[object TiUIWebView]";
type = beforeload;
url =
"file://localhost/Users/myuser/Workspace/TitaniumWorkspace/MyApp/Resources/procedureHTML/06_07_2010/11_00.html";
}
Context:
I have a local HTML file with different type of links (towards PDF,
DOC, etc). I have to prevent some default behavior.
Note:
When also registering a "load" event, which is called just after,
the url is correctly configured.
SDK 1.5.0 GA & 1.5.1 nightly
Jeremy
While this may be a valid bug, you have not have explained this ticket thoroughly-enough to be useful. Would you follow http://developer.appcelerator.com/guides/en/contribute.html#submitting_bugs"> this guide and fill in the missing information, please?
Thanks
Please assign and increase priority of this ticket for iOS.
Anyone developing web browsing apps is going to eventually need the ability to examine the URL before loading in the web view for URL filtering and information pop-ups, etc.
Here's a Use Case:
In iOS create a WebView with TouchScreen enabled and it loads the URL defined at WebView creation. User touches the screen on a hyperlink and WebView starts to load a malicious or advertising site. The app creator can provide in the app a whitelist of trusted URLS (or domains) to prevent loading of such websites and alert the user that the site has been blocked. To do this the url property of the WebView must be updated at the 'beforeload' event or there is no way to detect the pending page load.
Per your workflow I've posted to the community Q&A for potential work arounds.
Thanks, Chris
In addition, there seems to be no way to prevent the webview from loading an URL, after inspecting it during the beforeload event.
On iOS, you'd normally use shouldStartLoadWithRequest in the webview proxy to do this. But I see from the code that Appcelerator unconditionally returns YES on that method.
To implement the sort of behaviour described in this ticket, I think it's necessary to provide some sort of bridge to that method - or to provide an additional API method to abort the current URL, usable within the beforeload event. Without that, some important functionality is crippled compared with native iOS apps.
Note that I've tried issuing stopLoading() from within beforeload, but it doesn't work - presumably because the event is asynchronous and so can't get to the request in time.
I presume there's an equivalent way of doing this on Android, but I haven't looked into it yet.
This would be a huge help. Being able to detect what URL the user is trying to load before it loads is a must for security and for app integration with webview.
I for one would like to capture a URL from a share button in webview and preform facebook, twitter etc functions with in the app rather than going to the web version of said social site.
Please fix the beforeload event to update the URL, also, being able to stop the URL from loading, like cancelLoad() or something would be great too.
THanks, keep up the good work!
Please Fix This!
I agree, getting before-load to work, or better yet a wrapper around shouldStartLoadWithRequest would be extremely helpful. Our current workaround is to use load and then reload the previous url immediately after performing the required actions, but this is less than ideal for many reasons.
Is there a fix/feature planned? Apologize if I missed a roadmap document somewhere.
I am developing an eCommerce application. I would like to do the following on iOS and Android. Currently testing on iOS.
-Jim
I am building a safe browser app for the iPhone and it requires validating the url before it is loaded. Seems 'beforeload' event and being able to call webView.stopLading() if the url is not allowed is fundamental in this case.
Any idea on when this problems will be fixed?
Keep up the great work!
Diego
Still not enough information in the ticket to be able to handle it.
I found this article, which contains a neat trick that will maybe help in cases where you're in control of the HTML and able to convert elements appropriately:
http://boydlee.com/appcelerator-titanium/capturing-titanium-events-from-webview-to-app-code.html"> http://boydlee.com/appcelerator-titanium/capturing-titanium-events-...
It's certainly what I'll be trying to work around this problem.
Let me elaborate on the problem: whenever using the "beforeLoad" event, the e.url returns the original url the webView was opened from, and not the url that the webView is going to load next. I've tried to look at TiUIWebView.m, and changed: NSDictionary *event = url == nil ? nil : [NSDictionary dictionaryWithObject:[url absoluteString] forKey:@"url"]; to: NSURL *currentURL = webView.request.URL; NSDictionary *event = url == nil ? nil : [NSDictionary dictionaryWithObject:[currentURL absoluteString] forKey:@"url"]; which gave better results, the link is now updated, but I always get the last link I was in, and not the next one. If I'm at page A and clicking to move to page B, I'll get A as the "currentURL".
At last! I created a fix for it :) I should have used the "shouldStartLoadWithRequest" method instead of the "didStartLoad" one! go to /Library/Application Support/Titanium/mobilesdk/osx/1.6.2 (or 1.7)/iphone/Classes and edit TiUIWebView.m in the "shouldStartLoadWithRequest" method, look for: if ([scheme hasPrefix:@"http"] || [scheme hasPrefix:@"app"] || [scheme hasPrefix:@"file"] || [scheme hasPrefix:@"ftp"]) { return YES; } and replace with: if ([scheme hasPrefix:@"http"] || [scheme hasPrefix:@"app"] || [scheme hasPrefix:@"file"] || [scheme hasPrefix:@"ftp"]) { NSString *urlExtension = newUrl.absoluteString; NSArray *urlChunks = [urlExtension componentsSeparatedByString: @"."]; urlExtension = [urlChunks lastObject]; if ([urlExtension isEqualToString:@"zip"] || [urlExtension isEqualToString:@"pdf"] || [urlExtension isEqualToString:@"doc"]) { if ([self.proxy _hasListeners:@"fileDownload"]) { NSDictionary *event = newUrl == nil ? nil : [NSDictionary dictionaryWithObject:[newUrl absoluteString] forKey:@"url"]; [self.proxy fireEvent:@"fileDownload" withObject:event]; } return NO; } return YES; } save it and clean the project in the titanium IDE (Project/Clean), and build again. Now, for every url with the extension of ipa or zip (you can add more, look at the code), you'll get the "fileDownload" event in Titanium :) I hope appcelerator will add this fix in the next sdk version :)
Ok, so everyone has agreed this is a bug and we have a solution for it. People are screaming for a fix (check Q&A). Why isn't this fixed yet? It's stopping us from building the apps we want. I have patched the code myself as described above but that shouldn't be necessary.
We cannot accept code submitted in JIRA tickets. If the original author Yaniv Nagar wants to submit a pull request for it and sign the CLA then we can look at the code.
What is the duplicate JIRA ticket, so I can follow that?
TIMOB-7849
Closing ticket as duplicate of the ticket that is mentioned above.