pass alert from UIWebView to iOS app - javascript

I have create an app, which works fine. The only thing i need is to pass the alerts from uiwebview to my iOS app.
i have this alert on my uiwebview
<div id="alerts" class="alerts">
<p class="alert-red">ok. come back again tomorrow, not now.</p>
i want this alert to transfer into my app and make it into an uialertview
UIAlertView *errr = [[UIAlertView alloc]initWithTitle:#"nil" message:#"ok. come back again tomorrow, not now." delegate:self cancelButtonTitle:#"Ok, Got it" otherButtonTitles:nil, nil];
[errr show];
any idea how to achieve this result? do i need NSNotification to listen when this alert show up on uiwebview?
I tried something like this
NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:#"var targetDiv = document.getElementById('alerts').getElementsByClassName('alert-red')[0];"];
NSLog(#"%#",theTitle);
So I can try to retrieve that message from "alert-red" but doesn't work.
I'm new to javascript and html

This is the typical workaround used to solve this problem:
in your webpage when you want to show this alert, run this javascript code:
window.location = 'custom_action';
then in objc implement shouldStartLoadWithRequest on your controller (and set yourWebView.delegate = yourController)
-(BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
// detect when the webview switches to this custom url
if([[[request URL] absoluteString] isEqualToString: #"custom_action"]) {
UIAlertView *errr = [[UIAlertView alloc]initWithTitle:#"nil" message:#"ok. come back again tomorrow, not now." delegate:self cancelButtonTitle:#"Ok, Got it" otherButtonTitles:nil, nil];
[errr show];
// this prevents the webview from actually trying to load the custom url
return NO;
}
// allow the url to load if its not your custom url
return YES;
}

Related

How to stop repeitive alerts in WebView runJavascriptAlert OSX

I've created the below runJavaScriptAlertPanelWithMessage function which displays a javascript alert, however I would like to prevent malicious websites from being able to constantly bring up alerts. I've tried various things but haven't had any luck, so was hoping someone may have an idea on how I can achieve this.
- (void)webView:(WebView *)wv runJavaScriptAlertPanelWithMessage:(NSString *)msg initiatedByFrame:(WebFrame *)frame {
NSString *host = [[NSURL URLWithString:[wv mainFrameURL]] host];
NSString *t = NSLocalizedString(host, #"");
NSString *defaultButton = NSLocalizedString(#"OK", #"");
self.currentJavaScriptAlert = [NSAlert alertWithMessageText:t defaultButton:defaultButton alternateButton:nil otherButton:nil informativeTextWithFormat:msg];
[currentJavaScriptAlert beginSheetModalForWindow:[[frame webView] window] modalDelegate:self didEndSelector:#selector(alertPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}

I would like to export the contents of ios web view as html

I am writing an app for ios to extract information from a webpage, however, the relevant pieces on the page are built by javascript. So when it is loaded by webview, the javascript is executed and the information displays no problem. If I try to load the page into a string by using the following method, the javascript is loaded, but not actually executed, therefore the string has no useful data in it.
NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:fullURL]];
NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
Is there another way besides loading the page into webview and exporting it from there? If not, how do you do that?
I'm not sure if there's another way outside of letting the UIWebView execute the JS and render the page, but if you do end up going this route, you could just grab the HTML of the whole page and pass that to the native end like so:
[dummyWebView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('html')[0].outerHTML;"];
Listening to the window.load event might be better to know when the page has finished going through all the JS
Good luck!
You set delegate to webView: self.webView.delegate = self; and implement UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *html = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.outerHTML"];
NSLog(#"html1 = %#", html);
// or use
NSString *html2 = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
NSLog(#"html2 = %#", html2);
}

Capture (and prevent) alert() modal in UIWebView [duplicate]

<script language="javascript">
alert("Hell! UIWebView!");
</script>
I can see the alert message inside my UIWebView but can I handle this situation?
Update:
I'm loading a web-page into my UIWebView:
- (void)login {
NSString *requestText = [[NSString alloc] initWithFormat: #"%#?user=%#&password=%#", DEFAULT_URL, user.name, user.password]; // YES, I'm using GET request to send password :)
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestText]];
[webView loadRequest:request];
}
The target page contain a JS. If user name or password is incorrect this JS show alert.
I have not any access to its sources.
I want to handle it inside my UIWebViewDelegate.
A better solution to this problem is to create a Category for UIWebView for the method
webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:
So that you can handle the alert event in any way that you'd like. I did this because I don't like the default behavior of UIWebView when it puts the filename of the source in the UIAlertView title. The Category looks something like this,
#interface UIWebView (JavaScriptAlert)
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;
#end
#implementation UIWebView (JavaScriptAlert)
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
UIAlertView* dialogue = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[dialogue show];
[dialogue autorelease];
}
#end
This seems to do it:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
JSContext *ctx = [webView valueForKeyPath:#"documentView.webView.mainFrame.javaScriptContext"];
ctx[#"window"][#"alert"] = ^(JSValue *message) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"JavaScript Alert" message:[message toString] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
};
}
Note: only tested on iOS 8.
If by "contain a flash" you mean the page you're loading into your web view has an Adobe Flash movie in it, you're out of luck, I'm afraid. Mobile Safari doesn't support Flash, and most likely never will.
In the general case, if you want JavaScript running in a web view to communicate with the native app hosting it, you can load fake URLs (for example: "myapp://alert?The+text+of+the+alert+goes+here."). That will trigger the webView:shouldStartLoadWithRequest:navigationType: delegate method. In that method, inspect the request, and if the URL being loaded is one of these internal communications, trigger the appropriate action in your app, and return NO.

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.

Inject objective c into UIWebView

I have made a login form in html/javascript to be injected into a UIWebView in my iPhone application. This all works really well and the login works. But when I press the login button it goes to the expected page within that view.
I was wondering if I could inject some objective c or by ways of a javascript do a modalView or dismissView to have upon the login have the page go to the application.
In the Application I have just made the UI of the webpage more user friendly.
So to kind of show what I am asking I have pasted some code.
NSString *myHTML = #"<form action="gotowebsite.com" onSubmit='return !validateLogin();'><input some textfield><input password field>";
Now I am imagining that the dismissal code will go into the onSubmit area.
Am I on a possibly good track??
Cheers Jeff
Implement UIWebViewDelegate in your class and on successful login redirect your page to a url like login://success
when you redirect your page, UIWebview will start loading request and the call the function written below.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
NSString *urlStr = [url absoluteString];
if([urlStr isEqualToString:#"login://success"]){ //same url which you gave for redirection
[self dismissModalViewControllerAnimated:YES]; // or do whatever you want to do on a successful login
}
return YES;
}

Categories

Resources