How can i get the contents of javascript alert message on UIWebView? - ios

i've been trying to get a message of "javascript alert" on UIWebView.
although it's possible to get the one on Android WebView using by the "onJsAlert" event,
UIWebView doesn't have a kind of the method...
if anyone know the way to do it, please let me know.

You can't do it in simple way(ASAIK).
Let's hack it!
Try to re-define your alert function, remember to pass your alert message of the js alert in
I AM A SPECIAL URL FOR detecting
- (void) webViewDidFinishLoad: (UIWebView *) webView
{
[webView stringByEvaluatingJavaScriptFromString:#"window.alert = function(message) { window.location = \"I AM A SPECIAL URL FOR detecting\"; }"];
}
And get what you want in the alert url:
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = request.URL;
// look for our custom action to come through:
if ( [url.host isEqualToString: #"I AM A SPECIAL URL FOR detecting"] )
{
// parsing the url and get the message, assume there is a '='for value, if you are passing multiple value, you might need to do more
NSString* message = [[[[url query] componentsSeparatedByString: #"="] lastObject] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// show our alert
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle: #"My Custom Title"
message: message
delegate: nil
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[alertView show];
return NO;
}
return YES;
}

Related

Swift: Webview prompt dialog box to reload the current page when network is not available

I am new to iOS App development. I have a sample App which is written in Swift.
I am using a WKWebView in my web application.
However, when the mobile network/Wifi is not available. It is showing blank screen.
I would like to prompt a dialog box to refresh the current page to the user when
1. If network is not present
2. My server is not available to be accessed.
Use WebView delegate method to find network error.
-(void)webView:(UIWebView )webView didFailLoadWithError:(NSError )error
{
NSString *str = [NSString stringWithFormat:#"%li",(long)error.code];
if ([str isEqualToString: #"-1009"])//network unavailable error code
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Network Error" message:#"Internet or Wifi is not available. Please check your network connection & try again." delegate:self cancelButtonTitle:#"Retry" otherButtonTitles:nil];
[alertView show];
str = #"";
}
NSLog(#"%#",[super appUrl]);
}
Then retry in AlertView delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex==0){
if ([super appUrl]) {
NSURLRequest* appReq = [NSURLRequest requestWithURL:yourURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[self.webView loadRequest:appReq];
} }
}
P.S. use similar to work with swift.

iOS 7 UIWebView intercept alerts

In my app there's a UIWebView showing a web page. It sometimes displays errors in a UIAlertView that are really annoying. I would like to intercept this and show the errors in a more sophisticated way.
Is there a way where i can intercept the error message in a function and decide for myself what i want to do with it?
Thanks in advance!
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.

Display a loading indicator after user taps "Share" and before request is completed?

The issue I have is that after users tap on the "Share" button of the FBDialog, they do not get any visual feedback until the request completes or fail... and over 3G that can takes a while. During this time users do not know if they tapped "Share" correctly or not, and there's risk of posting content twice.
Is there a way I can get a callback so I can display a loading indicator during this time? Where should I put this piece in?
Thank you!
Below code you write to fbAgent.m and also obser code here u get one "PID" at that time u write to alert msg ....... user can understand to that msg was success posted Facebook.
I hope this helps.
- (void)request:(FBRequest*)request didLoad:(id)result {
if ([request.method isEqualToString:#"facebook.fql.query"]) {
NSArray* users = result;
NSDictionary* user = [users objectAtIndex:0];
NSString* name = [user objectForKey:#"name"];
// Calling the delegate callback
[delegate facebookAgent:self didLoadName:name];
} else if ([request.method isEqualToString:#"facebook.users.setStatus"]) {
newStatus = nil;
NSString* success = result;
if ([success isEqualToString:#"1"]) {
// Calling the delegate callback
[delegate facebookAgent:self statusChanged:YES];
} else {
[delegate facebookAgent:self statusChanged:NO];
}
} else if ([request.method isEqualToString:#"facebook.photos.upload"]) {
NSDictionary* photoInfo = result;
NSString* pid = [photoInfo objectForKey:#"pid"];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:#"success Uploaded Your image please check your FaceBook", pid] delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alertView show];
[alertView release];
fbbut.enabled=YES;
[fbact stopAnimating];
self.uploadImageData = nil;
self.uploadImageCaption = nil;
self.uploadImageAlbum = nil;
//[delegate facebookAgent:self photoUploaded:pid];
if(newStatus){
[self setStatus:newStatus];
}
}
}
Start your indicator when you click on button and stop on Delegate of Facebook function
- (void)dialogDidComplete:(FBDialog *)dialog
Hope it helps....

Show alert view when click on a link

I use formatted text in UIWebView, i have something like this:
0442059840
I need to show alert to the user when clicking on that link using the UIAlertView for confirmation before launching the telephone call. Is this possible? Thanx in advance.
Override the delegate method
webView:shouldStartLoadWithRequest:navigationType:
and do the cjhanges as in the code I added.
Following code may help you :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
NSString *urlString = [url absoluteString];
if([urlString isEqualToString:#"tel://0442059840"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Title", nil)
message:NSLocalizedString(#"Message", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK", nil)
otherButtonTitles:nil];
[alert show];
[alert release];
}
return YES;
}
Yes but you need to use undocumented method for this. Search on google or stackoverflow you will find it easily. Here is a link for helping you.
http://dblog.com.au/iphone-development/iphone-sdk-tip-firing-custom-events-when-a-link-is-clicked-in-a-uiwebview/

FBRequestDelegate didReceiveResponse how to find out for what request this response is

I am using facebook latest iphone sdk, I am making multiple request to facebook from different places in my app. For all of them didReceiveResponse and didLoad methods get called, it is very difficult to find out from didLoad method that for what request this response was so I am wondering if didReceiveResponse can help, can i retrieve some information in this method which will tell me what was the request for which I have got the response.
The way I do it is pretty much the same way Ziminji does it, but in didLoad method:
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(#"Facebook request %# loaded", [request url]);
//handling a user info request, for example
if ([[request url] rangeOfString:#"/me"].location != NSNotFound)
{
/* handle user request in here */
}
}
So basically you need only to check the url you sent the request to, and you can also check the parameters for that request. Then you can differentiate one from another.
All I am doing here is I am checking for some unique attribute in the response and relating it to the request, I know this is not the best way to do is, but this is what I have found so far, please let me know if any one is doing it any different
You could try something like the following:
- (void) request: (FBRequest *)request didReceiveResponse: (NSURLResponse *)response {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode == 200) {
NSString *url = [[response URL] absoluteString];
if ([url rangeOfString: #"me/feed"].location != NSNotFound) {
NSLog(#"Request Params: %#", [request params]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Facebook" message: #"Message successfully posted on Facebook." delegate: nil cancelButtonTitle: #"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
}
If you retain the request object as a property of your request delegate when you create it, you can check to see if it matches on the delegate method calls. For example:
- (void)queryForUserInfo {
self.userInfoRequest = [facebook requestWithGraphPath:#"me" andDelegate:self];
}
#pragma mark <FBRequestDelegate>
- (void)request:(FBRequest *)request didLoad:(id)result {
if (request == self.userInfoRequest) {
[self handleUserInfoResult:result];
}
}

Resources