Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2010, 07:45 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Netherlands
Posts: 28
TwanB is on a distinguished road
Default 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
TwanB is offline   Reply With Quote
Old 02-20-2010, 08:33 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

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.
harrytheshark is offline   Reply With Quote
Old 02-20-2010, 08:47 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Netherlands
Posts: 28
TwanB is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
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
TwanB is offline   Reply With Quote
Old 02-20-2010, 08:51 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

No problem.
In your .h file, declare the NSURL:
Code:
NSURL * theURL;
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:
Code:
[theURL release];
harrytheshark is offline   Reply With Quote
Old 02-21-2010, 09:31 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Netherlands
Posts: 28
TwanB is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
No problem.
In your .h file, declare the NSURL:
Code:
NSURL * theURL;
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:
Code:
[theURL release];
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
TwanB is offline   Reply With Quote
Old 02-21-2010, 09:51 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

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
harrytheshark is offline   Reply With Quote
Old 02-21-2010, 10:12 AM   #7 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Netherlands
Posts: 28
TwanB is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
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];
	}
TwanB is offline   Reply With Quote
Old 02-21-2010, 12:34 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

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];
}
harrytheshark is offline   Reply With Quote
Old 02-21-2010, 12:47 PM   #9 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Netherlands
Posts: 28
TwanB is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
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?
TwanB is offline   Reply With Quote
Old 02-21-2010, 12:50 PM   #10 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

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.
harrytheshark is offline   Reply With Quote
Old 02-24-2010, 11:17 AM   #11 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Netherlands
Posts: 28
TwanB is on a distinguished road
Default

Quote:
Originally Posted by harrytheshark View Post
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
Code:
 nil
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
TwanB is offline   Reply With Quote
Old 02-24-2010, 02:27 PM   #12 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

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
harrytheshark is offline   Reply With Quote
Reply

Bookmarks

Tags
alert, appstore link, uialertview, uitwebview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
5 members and 324 guests
2Apps1Day, akacaj, SLIC, soohyun, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:08 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0