-1

I am trying to create a dialog box in my iOS app (app is a PhoneGap application).

I am using the code from this post: how to implement a pop up dialog box in iOS

Below is the code that is in the link

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection" 
                                                message:@"You must be connected to the internet to use this app."
                                               delegate:nil 
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

I have put this code in AppDelegate.m in the method

- (BOOL)application:(UIApplication)...

The app runs but the dialog box does not show up.

What is the problem?


I have updated the code as below

The code below is in my appdelegate.m

//reachability code

if (networkStatus == ReachableViaWifi)
{
    while (true)
        //show progress bar
}
else
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
    [alert show];
}

The alert box is not showing up. What am I doing wrong?

Community
  • 1
  • 1
aagarwal
  • 134
  • 1
  • 2
  • 10
  • from which method of application you are showing AlertView? show your complete code. – nsgulliver Apr 24 '13 at 22:10
  • @nsgulliver isn't application the method? – aagarwal Apr 24 '13 at 22:21
  • AppDelegate is implementing a protocol called [UIApplicationDelegate](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html), this delegate has many methods you can see in your delegate. they start with **application** – nsgulliver Apr 24 '13 at 22:24
  • Here is the full line -(BOOL)application:(UIApplication*)application didFinishWithOptions:(NSDDictionary*)launchOptions – aagarwal Apr 24 '13 at 22:27
  • I have mentioned in my answer down, you could have a look on explanation. – nsgulliver Apr 24 '13 at 22:29

4 Answers4

1

You can show the UIAlertView in the AppDelegate if you have done everything right, but you can not exit the application using exit() method, it is not right practice in iOS to exit the app yourself. Basically your AppDelegate is implementing UIApplicationDelegate protocol, this protocol has many methods.

Try to show your AlertView in different methods in your AppDelegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
    [alert show];

    // other code... 
}

OR in

- (void)applicationDidBecomeActive:(UIApplication *)application {

   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
    [alert show];
}

Similarly you can show the alertview accordingly on your status change for the Reachability as you mentioned in the question.

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • What you mean by first sample is not working? second is working? try to move it after initializing the window. – nsgulliver Apr 24 '13 at 22:42
  • I never tried the second example. When I say the first sample is not working I meant that nothing happens. The alert box does not show up. – aagarwal Apr 25 '13 at 13:13
0

I suppose that is because your code is above the line [self.window makeKeyAndVisible];.

Try to move your code for alert after that line.

Dennis Pashkov
  • 934
  • 10
  • 24
0

You should add the code into your MainViewController.m (or other UIViewController .m file), and depending what behaviour you want the code should be added into -(void)viewDidLoad or -(void)viewDidAppear methods.

danypata
  • 9,895
  • 1
  • 31
  • 44
  • I want to be able to call the dialog in my appdelegate actually so how would I do that. From you answer what I understood was that I would put the UIAlerView *alert = ..... code in the -(void)viewDidLoad or -(void)viewDidAppear methods and I would call the dialog in my app delegate using [alert show];. Is my interpretation correct? – aagarwal Apr 24 '13 at 22:02
  • You can add the UIAlertView code inside the method: `- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions`. From what I read in your comments you added the code inside `-(void)applicationDidFinishLaunching:(UIApplication *)application` this method is used for early version of iOS for iOS 3.0+ you should use the first method. – danypata Apr 24 '13 at 22:53
0

For iOS alert dialog, in your AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection" 
                                          message:@"You must be connected to the internet to use this app."
                                          delegate:nil 
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
[alert show];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
[self.window makeKeyAndVisible];
return YES;
}

Since you mentioned you are using phonegap, they will handle the alert box for you on different platforms. You would use the following code in one of your pages html

function showAlert() {
    navigator.notification.alert(
        'You must be connected to the internet to use this app.',
        null,       
        'No network connection',            
        'OK'                  
    );
}
zimmryan
  • 1,099
  • 10
  • 19