Open Links from UIWebView in Safari

Discussion in 'iOS Programming' started by adriweb, Oct 25, 2010.

  1. adriweb macrumors newbie

    Joined:
    Oct 23, 2010
    Location:
    France
    #1
    Hi,

    I currently have a UIWebView at the bottom of my app that basically displays Google Adsense Ads.

    What I would like to do is open Safari when a user clicks the ad in the webview.

    I have already looked for that a lot, and the only thing I could find is a code like that :

    Code:
    (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        if (navigationType == UIWebViewNavigationTypeLinkClicked) {
            [[UIApplication sharedApplication] openURL:request.URL];
            return false;
        }
        return true;
    }
    So, when I add this to my code, I change the name "webView" by the name of what my UIWebView is ("pubadsense"), so it finally looks like that :

    Code:
    [pubadsense loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://adriweb.free.fr/pub.html"]]];
    	
    	- (BOOL)pubadsense:(UIWebView *)pubadsense shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    		if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    			[[UIApplication sharedApplication] openURL:request.URL];
    			return false;
    		}
    		return true;
    	}

    But when I want to compile, Xcode tells me :



    What am I supposed to do now ?

    Isn't the code I found working for this particular case ?


    Thanks !
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Don't rename delegate methods. It's called webView:shouldStartLoadWithRequest:navigationType: for a reason.
     
  3. adriweb thread starter macrumors newbie

    Joined:
    Oct 23, 2010
    Location:
    France
    #3
    Thanks, but when I leave "webView" like that :

    Code:
    	- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    

    Xcode tells me "webView" is undeclared....


    But the
    is still here .....
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You're not trying to define your delegate method within another method, are you?
     
  5. adriweb thread starter macrumors newbie

    Joined:
    Oct 23, 2010
    Location:
    France
    #5
    Hmm.... Maybe, I'm not sure...

    EDIT : Yes I think I am ... so Where and how should I declare it correctly ?

    (Yep, I'm probably a "noob" in iPhone developing, as I started few days ago...)

    This is what I have :

    [​IMG]

    Would giving the source help (I mean, in a .zip file)
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    How: you should declare it like any other method in the class.
    Where: doesn't really matter for delegate methods; wherever makes sense for you.

    I think you are so fresh to iPhone developing that I'm gonna suggest you step back from the real coding and go learn the basics of iPhone Development / Objective-C programming. Don't come back to this issue until you are comfortable with them.
     
  7. adriweb thread starter macrumors newbie

    Joined:
    Oct 23, 2010
    Location:
    France
    #7
    What's weird though is that I've seen several open-source projects that kind of did the same.... Well, I'll keep trying...

    Oh well, yes, you're 100% right, I actually wanted to build a little app with some parts taken from examples here and there, so, I will definitely learn the basics first, but it shouldn't take so long (I hope) considering I also develop in some other languages... :)


    Thanks for your help though !
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    "Kind of" doesn't count. Precision is important. I doubt those projects (don't be afraid to link to these kinds of things when discussing them) are violating the basic syntax of method implementation; if they are, that's bad cause they won't even compile, as you've run into.
     
  9. adriweb thread starter macrumors newbie

    Joined:
    Oct 23, 2010
    Location:
    France
    #9
    While I'm still on that... do you have an example of what I'm trying to do ?



    [​IMG]
    And by the way, I bought this book, so I'll dive into the basics as soon as possible (I'm a student and I actually have a lot of school things to do ...)
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Not off the top of my head, no. You may need to use some creativity to mold existing solutions to fit your needs.

    Make sure you understand Objective-C before you begin that book.
     

Share This Page