02-20-2010, 07:45 AM
#1 (permalink )
Registered Member
Join Date: Feb 2010
Location: Netherlands
Posts: 28
Alert when leaving WebView for AppStore
I have an app using WebView. I want an alert (UIAlertView) when the link to the AppStore is clicked with 2 choices:
1) Open AppStore? (then open the native AppStore app)
2) Cancel (close alert and go back to my app)
I have this code to open the AppStore link from the WebView:
Code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.host hasSuffix:@".apple.com"]){
[[UIApplication sharedApplication] openURL: request.URL ];
return NO;
}
return YES;
}
Any help on this is very much appreciated!
Ciao,
T
02-20-2010, 08:33 AM
#2 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
Something like this:
Code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.host hasSuffix:@".apple.com"]){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Leave the app?"
message:@"Do you want to leave (appname) and visit the AppStore?"
delegate:self
cancelButtonTitle:@"Nah"
otherButtonTitles:@"Sure"];
[alert show];
[alert release];
return NO;
}
return YES;
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1){
//go to the url
}
}
You're going to need to save the url so the alertView method can call it.
Hope that helps.
02-20-2010, 08:47 AM
#3 (permalink )
Registered Member
Join Date: Feb 2010
Location: Netherlands
Posts: 28
Quote:
Originally Posted by
harrytheshark
Something like this:
Code:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.host hasSuffix:@".apple.com"]){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Leave the app?"
message:@"Do you want to leave (appname) and visit the AppStore?"
delegate:self
cancelButtonTitle:@"Nah"
otherButtonTitles:@"Sure"];
[alert show];
[alert release];
return NO;
}
return YES;
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1){
//go to the url
}
}
You're going to need to save the url so the alertView method can call it.
Hope that helps.
Thanks for the reply!
I'm fairly new to Obj.C. Could you help me with saving the URL to have AlertView call the method?
Thanks!
_Twan
02-20-2010, 08:51 AM
#4 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
No problem.
In your .h file, declare the NSURL:
Then when you call your UIAlertView, set the NSURL by doing:
Code:
theURL = [request.URL retain];
Then when you're finished with it, be sure to release it:
02-21-2010, 09:31 AM
#5 (permalink )
Registered Member
Join Date: Feb 2010
Location: Netherlands
Posts: 28
Quote:
Originally Posted by
harrytheshark
No problem.
In your .h file, declare the NSURL:
Then when you call your UIAlertView, set the NSURL by doing:
Code:
theURL = [request.URL retain];
Then when you're finished with it, be sure to release it:
hmzmmz, I feel kinda sheepish now.. still a lot to learn. (do I dare to ask?...) But how exactly do I declare the NSURL?
My .h-file looks like this:
Code:
@interface MainMainMain : NSObject <UIApplicationDelegate> {
IBOutlet UIWebView *mainView;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
- (IBAction)refreshClicks:(id)sender;
@property (nonatomic, retain) UIWebView *mainView;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@end
02-21-2010, 09:51 AM
#6 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
Where your IBOutlets are:
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
02-21-2010, 10:12 AM
#7 (permalink )
Registered Member
Join Date: Feb 2010
Location: Netherlands
Posts: 28
Quote:
Originally Posted by
harrytheshark
Where your IBOutlets are:
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
I tried that, but I get the error in my .m stating 'request undeclared'
Code:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1){
NSURL * theURL = [request.URL retain];
[theURL release];
}
02-21-2010, 12:34 PM
#8 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
You want to assign the URL when you pop up your uialertview, not afterwards.
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:@"Leave the app?"
message:@"Do you want to leave (appname) and visit the AppStore?"
delegate:self
cancelButtonTitle:@"Nah"
otherButtonTitles:@"Sure"];
[alert show];
[alert release];
return NO;
}
return YES;
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1){
//go to the url
}
[theURL release];
}
02-21-2010, 12:47 PM
#9 (permalink )
Registered Member
Join Date: Feb 2010
Location: Netherlands
Posts: 28
Quote:
Originally Posted by
harrytheshark
You want to assign the URL when you pop up your uialertview, not afterwards.
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:@"Leave the app?"
message:@"Do you want to leave (appname) and visit the AppStore?"
delegate:self
cancelButtonTitle:@"Nah"
otherButtonTitles:@"Sure"];
[alert show];
[alert release];
return NO;
}
return YES;
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1){
//go to the url
}
[theURL release];
}
Ah great. rooky mistakes indeed
How is it possible, that this works in the 3.1.3 simulator, but when I install it on my own iPhone device 3.1.3 the app closes when I click the link?
02-21-2010, 12:50 PM
#10 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
Check that you're not over releasing anything, if you have other UIAlertViews, make sure not to release "theURL" when they are dismissed.
You can also do a google for "NSZombieEnabled" which can help debug this kind of error.
02-24-2010, 11:17 AM
#11 (permalink )
Registered Member
Join Date: Feb 2010
Location: Netherlands
Posts: 28
Quote:
Originally Posted by
harrytheshark
Check that you're not over releasing anything, if you have other UIAlertViews, make sure not to release "theURL" when they are dismissed.
You can also do a google for "NSZombieEnabled" which can help debug this kind of error.
Thanks for all your help! I got it to work. You forgot the
I used the code like this:
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", nil];
[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];
}
To show you my gratitude, I'd like to give you a promo-code to my app, if you'd like that. IM me for that!
Ciao,
Twan
02-24-2010, 02:27 PM
#12 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,896
Oops, my bad!
I'm the UK and they don't work over here, so don't worry about it. I'm glad you got it working!
Tom
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,650
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun