Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TwanB

macrumors newbie
Original poster
Aug 12, 2008
6
0
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];
}
 
How is theURL defined?

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
 
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
You might wanna try retaining theUrl then instead of request.URL.
 
You might wanna try retaining theUrl then instead of request.URL.

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.
 
You might wanna try retaining theUrl then instead of request.URL.

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". :)
 
That wouldn't make any difference whatsoever for the code sample at hand.
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:.

OP, [UIAlertView initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles:] wants a variable arguments list for the otherButtonTitles parameter, similarly to [NSArray arrayWithObjects:], for instance.
Ah, I missed that issue the first time looking through the code. I'm surprised it doesn't create a compile-time warning.

Oh, and never name an OK button "OK". Name it "Go to the App Store". :)
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?
 
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.
 
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:@""]];
	}
}
 
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.
 
look i'm just giving an example! I figured that he could replace the URL...
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.

...and know to put the UIAlertView in an IBAction or something to call it to action.
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.
 
Granted, all of this is off topic, but since I consider the original poster's problem solved anyway... ;)

It might. If it is never retained and then accessed in a later method.

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.

--

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?

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:

The label on a push button should be a verb or verb phrase that describes the action it performs—Save, Close, Print, Delete, Change Password, and so on.

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. ;)
 
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:@""]];
	}
}

Thanks for all your help, it works!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.