User Interaction Enabled Javascript - javascript

I am writing a Javascript application and am going to wrap it in a native iOS application. I would like to block user interaction on the UIWebView containing the JS app for a second or two following an event.
Normally I would use the self.webView.userinteractionenabled = NO but the event is triggered in Javascript. How can I block the user from interacting with the web view?
Guessing return false on a touch event of some sort? It's scrolling that I want to block.
Thanks!

When the event occurs in your Javascript code you can send a message to the native wrapper by using the following method:
Set up the following UIWebViewDelegate method (don't forget to set the delegate for the UIWebView):
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:#"block"]) {
// do the your blocking code here
return NO;
}
return YES;
}
Now when your event happens, call the delegate method from your javascript code:
window.location.href = "block://";

Related

When does UIWebView start recognising Javascript code

// MARK: - UIWebViewDelegate
func webViewDidFinishLoad(webView: UIWebView) {
let initResponse = self.stringByEvaluatingJavaScriptFromString("initialize('','','',\(isPreview))")
if (initResponse != "") {
isLoaded = true
}
println("\(viewType) initResponse \(initResponse)")
}
In my program, I am attempting to initialise UIWebView by calling a Javascript function. UIWebView is loaded with a local template file.
Apparently, the webview still does not respond to my javascript command even though webViewDidFinishLoad is called.
Therefore, I think there is another moment when web view can actually start accept Javascript calls. In my opinion, it is when document is ready for my locally loaded html file.
But then, how do I know when my UIWebview is ready to receive Javascript code?
For iOS/UIWebView, please have a try on this
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (![[webView stringByEvaluatingJavaScriptFromString:#"document.readyState"] isEqualToString:#"complete"])
return;
// Now it's ready
}
and for OS X, this one webView:didFinishLoadForFrame: is what you want.

webViewDidFinishLoad not calling when having javascript content in webpage

I'm using UIWebView in a project. Sometimes it doesn't call the webViewDidFinishLoad method. Because the web page have some javascripts. The method
- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
is getting call but webViewDidFinishLoad doesn't. I want to catch that method. Because I'm start an animation when the webview start loading. Then I want to stop this animation when it finished. It's not working with websites having javascript content. Any one have an idea please?
Thanks
webViewDidFinishLoad method gets called when the UIWebView has finished loading the url, in your case since you are calling a javascript, it doesn't load your content, it just calls a javascript function in your already loaded webview. But YES you can catch the javascript actions in the same method you stated as below
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *url = [[request URL] absoluteString];
if(navigationType == UIWebViewNavigationTypeLinkClicked){
if([url rangeOfString:#"SOMETHING"].length > 0 ){
//if your action returns something appended in your url like the anchors work
//DO YOUR STUFFS
}
}
return TRUE;
}
apparently, the webview in ios checks specifically for "iframe" tags in the html's dom and in case it finds, it creates events based on that tag as well. therefore, a simple however a patchy solution would be adding the following code in the part of the dom that is changing (for example, in the actual template of each section):
<iframe></iframe>
did the trick for me..

Touches in a UIWebView

I'm developing an iOS app with a UIWebView instance.
If the user touches an object in a webpage shown by the web view, how can I extract metadata regarding the object touched (such as an "id" for an HTML element) over in Objective C land?
[I'm not interested in whether the web view was touched or not, I'm only interested in what part of the page was touched and being able to act on this.]
You probably need to do this in JavaScript land. In your webview set up some javascript to monitor the click (e.g. element.onclick = function() { ... }, or use jQuery if that's easier).
Now you can call out to the native code in your app by using a made up url and then intercepting it using the webview delegate, e.g.
//in your web page, in javascript
myDiv.onclick = function() { document.location.href = 'http://madeupdomain/' + this.id};
//webview delegate in cocoa
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *URL = [request URL];
NSString *host = URL.host;
if ([host isEqualToString:#"madeupdomain"])
{
NSString *theDivID = URL.path;
//now do something based on the div id value
return NO;
}
return YES;
}
This is essentially how frameworks like PhoneGap communicate between the webview and the native code.

iOS SQL Database access from Javascript

I am creating an iPhone/iPad app for the app store. What I'd like to do is create it using HTML5/Javascript through a UIWebView so I can later use the HTML5/Javascript code for a web version as well (this is a requirement). I would like to have an offline database (SQLite) that basically ships with the app - this would be the content for the app and the .sqlite file would exist in the /Supporting Files/ folder of my XCode Project.
My question is, how can I run queries and get data back from this database from Javascript? Is this possible?
You would have to build your own bridge or use one of the many HTML-based app frameworks available for iOS (PhoneGap, Appcelerator, etc).
If you want to go down the path of building your own bridge, you should check out UIWebView's -stringByEvaluatingJavaScriptFromString:, and the combination of UIWebView's -loadRequest and UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: using a custom protocol (e.g., com.mycompany.myapp.1.0://) that you check for in the delegate method.
EDIT:
Here's some sample code:
Your HTML should have something like this:
Do Query
or:
Do Query
<script>
function doQuery() {
window.location = "myapp://doQuery";
}
</script>
Then your webview delegate can capture a click there by:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)req (UIWebViewNavigationType)navType {
if ([[req URL] scheme] == #"myapp:") {
if ([[req URL] host] == #"doQuery") {
// Do the query here.
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"latestResult = %#; queryReturned(latestResult);", queryResultAsJSON]];
} else if ([[req URL] host] == #"doSomethingElse") {
// Do something else.
}
return NO;
}
return YES;
}

How to received alert() action from javascript into Cocoa application via WebView

I was develop some Cocoa application with WebView. Now I can evaluate javascript to HTML file by using evaluateWebScript:
But I need to receive an alert() event from javascript to display in Cocoa's NSAlertSheet.
How to do with cocoa development ?
You need to set an object as the WebUIDelegate of the WebView (using the setUIDelegate: method) and in that object implement the ‑webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: delegate method. This method will be called when a page loaded into the WebView calls the alert() JavaScript function.
In your implementation of the delegate method, you should display an alert. The alert should:
display the exact message string that is passed in to the method
indicate that the message comes from JavaScript
contain only one button, an OK button
Here is a basic example:
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
{
NSAlert* jsAlert = [NSAlert alertWithMessageText:#"JavaScript"
defaultButton:#"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:#"%#", message];
[jsAlert beginSheetModalForWindow:sender.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
}
You can try to override JavaScript alert function on window. In your custom alert you can redirect to predefined url, e.g. app://alert/something%32is%32wrong. That kind of redirect can be handled by UIWebView through webView:shouldLoadRequest:.
PS: I didn't try it :)

Categories

Resources