UIAlertView errors

Discussion in 'iOS Programming' started by TwanB, Feb 22, 2010.

  1. TwanB macrumors newbie

    Joined:
    Aug 12, 2008
    #1
    I'm trying to debug my app because the app closes when I click the link.

    I want an AlertView when you click a link to the AppStore, with the choises OK or Cancel, Cancel obviously doing nothing and OK opening the AppStore.

    Can anyone help me with this?

    The code:
    Code:
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    	
    	if ([request.URL.host hasSuffix:@".apple.com"]){
    		theURL = [request.URL retain];
    		UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Go to the AppStore?"
    														 message:@"" 
    														delegate:self 
    											   cancelButtonTitle:@"Cancel" 
    											   otherButtonTitles:@"OK"];
    		
    		[alert show];
    		[alert release];
    		return NO;
    	}	
    	return YES;
    }
    
    // defining action for pressing 'OK' and calling method theURL
    -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    	
    	// by clicking 'OK' go to the url
    	if (buttonIndex == 1){
    		[[UIApplication sharedApplication] openURL: theURL];
    
    	}
    		[theURL release];
    }
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. TwanB thread starter macrumors newbie

    Joined:
    Aug 12, 2008
    #3
    The app closes.

    When debugging I get the message 'EXC_BAD_ACCESS'
     
  4. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    Are you loading the url in a webview in your application or are you trying to launch the appstore app?
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  6. TwanB thread starter macrumors newbie

    Joined:
    Aug 12, 2008
    #6
    In my .h-file:

    Code:
    
    @interface MainMainMain : NSObject <UIApplicationDelegate> {
    	
    	IBOutlet UIWebView *mainView;
    	IBOutlet UIActivityIndicatorView *activityIndicator;
    	NSURL * theURL;
    }
    
    - (IBAction)refreshClicks:(id)sender;
    
    @property (nonatomic, retain) UIWebView *mainView;
    @property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
    
    @end
    
    
     
  7. TwanB thread starter macrumors newbie

    Joined:
    Aug 12, 2008
    #7
    I have a html-page in which the link is defined as: http://itunes.apple.com/nl/app/......
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    You might wanna try retaining theUrl then instead of request.URL.
     
  9. TwanB thread starter macrumors newbie

    Joined:
    Aug 12, 2008
    #9
    When I do that:
    Code:
    		theURL = [theURL retain];
    [code]
    
    The app still closes, only in the 'release' mode the simulator now does nothing with the link and stays in my app.
     
  10. Peter Maurer macrumors member

    Joined:
    Oct 9, 2008
    #10
    That wouldn't make any difference whatsoever for the code sample at hand.

    OP, [UIAlertView initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles:] wants a variable arguments list for the otherButtonTitles parameter, similarly to [NSArray arrayWithObjects:], for instance.

    So what you want to do is this:

    Code:
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Go to the AppStore?"
    message:@""
    delegate:self
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"OK"[COLOR="Red"], nil[/COLOR]];
    Oh, and never name an OK button "OK". Name it "Go to the App Store". :)
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    It might. If it is never retained and then accessed in a later method. But you do have a point with the improper parameters for otherButtonTitles:.

    Ah, I missed that issue the first time looking through the code. I'm surprised it doesn't create a compile-time warning.

    Why not? Seems perfectly acceptable given that the alert is prompting "Go to the AppStore? in it's title. Could you elaborate on why you think this should never be done?
     
  12. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #12
    to complicated

    your making this to complicated why not just do something like this:
    HTML:
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Go to the AppStore?" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    	
    	if (buttonIndex == 0) {
    
            }
    	if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString://appstoreURL]];
    		}
            }
    
    
    The way i see it thats much simpler and works.
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Works? Um, that won't even compile. Sorry. :(
     
  14. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #14
    Yes

    It will if you do something like this:
    HTML:
    - (IBAction)ShowAlert {
    	UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Go to the AppStore?" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    	[alert show];
    	[alert release];
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    	
    	if (buttonIndex == 0) {
    		
    	}
    	if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@""]];
    	}
    }
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Well, of course it will compile if you fix all the compile-time errors. :D

    Of course, your URL string is empty and so the openURL: isn't really going to work but...
     
  16. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
    #16
    hey

    look i'm just giving an example! I figured that he could replace the URL, and know to put the UIAlertView in an IBAction or something to call it to action.
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    Granted. But it doesn't really help them if it doesn't even work. You're better off giving them a working example and then explaining that they can replace a example hardcoded URL string with whatever URL they actually needed.

    If you read the OP, it looks like the link to the App Store app is contained in a UIWebView and thus an IBAction is going to be of little help in this case. I believe they are trying to intervene in the link clicking so that an alertView can be displayed first, before going to the App Store app.
     
  18. HarryWorksInc macrumors regular

    Joined:
    Feb 21, 2010
  19. Peter Maurer macrumors member

    Joined:
    Oct 9, 2008
    #19
    Granted, all of this is off topic, but since I consider the original poster's problem solved anyway... ;)

    Now that's Interesting. The original source code is this:

    Code:
    theURL = [request.URL retain];
    Your suggestion ("try retaining theUrl then instead of request.URL") boils down to this:

    Code:
    theURL = request.URL;
    [theURL retain];
    It seems to me the retain message is sent to the exact same object in both cases. I'm afraid I still don't see the difference.

    --

    I know Apple uses the occasional "OK" in their iPhone OS frameworks themselves, possibly because of a simple lack of space, but in my humble opinion, the Mac HIG give great general advice on buttons:

    So even though dialog boxes are somewhat of a special case, even though this is iPhone OS, and even if it's going to be slightly redundant, I strongly suggest making a button say what it actually does whenever there's room for it -- especially when your button does something as grave as quitting the currently running app to launch the App Store app.

    After all, you want your alert view to be as different as possible from the proverbial ...

    Code:
    Lorem ipsum dolor sit amet, consectetuer
    adipiscing elit, sed diam nonummy nibh euismod
    tincidunt ut laoreet dolore magna aliquam erat
    volutpat. Ut wisi enim ad minim veniam, quis
    nostrud exerci tation ullamcorper suscipit lobortis
    nisl ut aliquip ex ea commodo consequat.
    
    [ Yes ]    [ No ]    [ Cancel ]
    ... Windows dialog box from hell. ;)
     
  20. TwanB thread starter macrumors newbie

    Joined:
    Aug 12, 2008
    #20
    Thanks for all your help, it works!
     

Share This Page