iOS 7 UIWebView intercept alerts - ios

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.

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.

How to show an alert in ios for paper out error from zebra printer?

I'm trying to show an alert from my app. When my app is successfully connected with zebra printer and then if the printer doesnot have paper at the time of printing.. i want to show an alert in my app about the paper out error... please sunbmit your answers if anybody knows...
What exactly you are looking for? Do you want to know the code for how to create and show an alert? or do you want to interact with printer and get the status so as to show alert?
If you are looking for code that shows an alert here you go..
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Printer Warning" message:#"Printer running out of paper" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
have u used this block?
void (^completionhandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *print, BOOL completed, NSError *error)
{
if (!completed && error)
{
NSLog(#"%#", error.localisedDescription);
}
};
if any error occurs, the error will be logged and u can also use it in AlertView.
if(!completed && error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.localisedDescription cancelButtonTitle:nil otherButtons:#"OK", nil];
[alertview show];
}

Twitter.framework - twitterd session interrupted, restarting

Got a problem with the new Twitter.framework that I haven't been able to find a solution for yet.
Here is my code:
if ([TWTweetComposeViewController canSendTweet]){
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter addImage:tweetImage];
[twitter setInitialText:initalString];
[twitter addURL:url];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if (result == TWTweetComposeViewControllerResultDone) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Tweeted"
message:#"You successfully tweeted"
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
});
} else if (result == TWTweetComposeViewControllerResultCancelled) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Twitter"
message:#"Tweet has been canceled"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
});
}
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissModalViewControllerAnimated:YES];
});
};
[self presentViewController:twitter animated:YES completion:nil];
[twitter release];
}
Seems to be the standard way of implementing this although I made the addition of queuing the UI stuff on the main thread. The addImage, setInitialText and addURL parameters are all good. In, fact this works most of the time. The problem I am having is that occasionally when the TWTweetComposeViewController is alloc'd init the app freezes and I can see "twitterd session interrupted, restarting... " in the console. The app will hang sometimes for only a few seconds but more often it will hang for unreasonable amount of time (20 - 30 secs or more), I will get numerous of these messages and then the twitter controller will finally slide up. Occasionally, as well, it will just hang and never come back.
Was wondering it anybody has see this problem before or has any ideas on a solution to this problem?
Thanks in advance.
I never add these problems with the twitter view controller. I used my code pasted in this post: https://stackoverflow.com/questions/9314308/can-twtweetcomposeviewcontroller-tweet-sheet-rotate-to-landscape
You can try it, just change it to "self" when you dismiss or present in modal view, as I'm using a different view controller.

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/

Showing an alert with Cocoa

I have been using VB.NET for a while and I switched to Xcode 4.
In the VB.NET environment, I used to type the following command in a button:
if TextBox1.text="" then
MessageBox.Show("You can't leave the textbox empty!, "Error!")
else
Label1.text = TextBox1.text
That is just an example. In Xcode, I want to do the same thing except for I want to have a Pop-Up Alert (in the iPhone) instead of the MessageBox in VB.NET.
Any Ideas?
Here you go
if ([TextBox1.text isEqualToString:#""]){
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"You can't leave the textbox empty!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
} else {
Label1.text = TextBox1.text;
}

Resources