WebApp force link to open in Safari?

Discussion in 'iOS Programming' started by gencon, Dec 4, 2010.

  1. gencon macrumors newbie

    Aug 24, 2010

    Firstly I hope it's ok to post a WebApp question here, the only dedicated iPhone WebApp forum I could find is very inactive (about 1 post a week).

    I've developed an iPhone WebApp which searches a large DVD collection held in a MySQL database. It works well and looks very professional once added to -and launched from- the iPhone's Home Screen with the status bar set to black and the Safari navigation buttons at the bottom gone.

    For each DVD on the results page(s), a link is present to load the relevant IMDb (Internet Movie Database) page. This is causing a problem. The initial IMDb page loads in the WebApp but then many (but not all) subsequent links (EG. clicking on the IMDb page's link to 'Full Cast') will open Safari with that page, leaving the WebApp. What I would like is for that to happen immediately, so when clicking on the IMDb link from my WebApp, that immediately opens the link in Safari. Using target="_blank" does not do this. Is there any way I can force the WebApp to open a link in Safari?

    Many thanks.
  2. yaniv92648, Dec 5, 2010
    Last edited by a moderator: Dec 6, 2010

    yaniv92648 macrumors member

    Oct 26, 2009
    I'm not sure i understood fully...

    [[UIApplication sharedApplication] 
    openURL:[NSURL URLWithString:@"www.google.com"]];
    the code above opens www.google.com in safari.
  3. gencon, Dec 6, 2010
    Last edited by a moderator: Dec 6, 2010

    gencon thread starter macrumors newbie

    Aug 24, 2010
    Err no, it is a WebApp, so it's HTML/Javascript, what I need is a way to force a WebApp page to force the iPhone to open a link with Safari.

    Is that possible?

  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
  5. gencon thread starter macrumors newbie

    Aug 24, 2010
    No. It's just a PHP page added to the iPhone home screen (using Safari's 'Add to home screen' feature) and utilizing meta tags to turn the status bar to black, prevent user zooming, and such like.

  6. domness macrumors 6502a


    Jan 14, 2008
    Sheffield, UK
    You're already in Safari mate… Do you mean force it to open in a new "Page/Tab"? I always thought target=_blank worked fine though.
  7. gencon thread starter macrumors newbie

    Aug 24, 2010
    Well yes and no. If you close the webapp and start Safari, the webapp's page is not shown as one of the Safari pages. Though I take the point that it is an instance of Safari.

    What happens with some links within the webapp - if it moves to a domain other than the webapp's domain - is that some links when pressed will close the webapp and Safari will auto start up showing the page of the link that's just been pressed. It is that behaviour I want to force to happen as soon as the webapp's domain is left by clicking the IMDb link on my results page. Unfortunately "target=_blank" does not make this happen.

    Does that make it more clear?

    Thanks all.
  8. robbieduncan Moderator emeritus


    Jul 24, 2002
    target=_blank not working seems to be a known problem (I did a quick Google search). What happens if you assign a random number to the target? You could do this via Javascript.
  9. gencon, Dec 8, 2010
    Last edited: Dec 9, 2010

    gencon thread starter macrumors newbie

    Aug 24, 2010
    Using Javascript to open the links was a very good idea, it made me try a whole load of different linking methods. I've managed to use a combination of linking types in order to achieve the desired behaviour.

    Thanks for your help everyone.
  10. vargatron macrumors newbie

    Dec 8, 2010

    Can you post what you used? I'm facing a similar dilemma, window.open(url) seems to just go right into the same app and not launch safari...

  11. gencon thread starter macrumors newbie

    Aug 24, 2010
    Ok, here is how I ended up getting it to work.

    I have 2 ways of specifying a link in the WebApp.

    For links that I want to open within the WebApp and not open in Safari, I use this:

    <a onclick="parent.location='http://url.com/page.html'">Link</a>
    For links that I want to open in Safari, I use the standard HTML link:

    <a href="http://url.com/another_page.html">Link</a>
    That's it. Simple once you know how.

  12. JeffRutan macrumors newbie

    Jun 5, 2003
    San Diego
    This is useful information! Thanks!

    However, I am looking for a way to do kind of the opposite of this...

    From javascript I want to launch a link in Safari without using an <a> tag in the HTML.
    I have been using this: window.location.replace (sMyLink);
    to launch inside my stand-alone webApp, but have not yet found a way to force sMyLink into Safari.
    What I have done is dynamically create a <a> tag for the user to tap inside a custom confirmation dialog. This works, but is not always what I need.

    Anyone know how to get javascript running in a stand-alone webApp (launched from the Home Screen and not running inside the actual Safari web browser app) to directly launch Safari with a given link?

  13. Sykte macrumors regular

    Aug 26, 2010


    You embed it like any other html application.

    <script type="text/javascript">
    function helloWorld() {
    document.innerHTML = 'Hello World';

Share This Page