launch html file from web browser in carbon

Discussion in 'Mac Programming' started by printf, Nov 30, 2008.

  1. macrumors regular

    the title says it all. i'm trying to launch html-based help files in the same directory as my app.

    i've tried both LSOpenCFURLRef, as well as LSOpenFSRef, but can only get them to work with www urls but NOT local files like "file:///Developer/help.html". all i get is an OSStatus of -43, which doesn't seem to be in the range of the documented Launch Services result codes.

    anyone ever get this to work?
  2. macrumors regular

  3. macrumors regular

    hhas, thanks for your response and that link.

    i'd love to adopt that system, and probably will somehow in the future, but for the sake of time, and because this is the same system used in my win32 app, i need to just pop open the html files using the aforementioned technique.

    any idea how?
  4. macrumors regular

    -43 = file not found. Probably your URL is wrong. Post the relevant code if you want specifics.
  5. macrumors 6502a


    You need to get a CFURL reference to the html file to open, most likely in the app bundle. So first you get a reference to the main app's bundle, which you can then use to get the embedded HTML file.

    so do:

    CFBundle bundle = CFGetMainBundle();
    CFURL  = CFBundleCopyResourceURL( bundle, "Help", "html", "HelpContentFolder" );
    So "Help" is the file name minus extension, "html" is the file type extension (hurray for Unix/Windows!) and "HelpContentFolder" is the name of the folder in the Resources folder inside the app bundle that contains the Help files.

    App Bundle > Contents > Resources > HelpContentFolder

    Then its just a matter of: Pass that CFURL to LSOpenCFURLRef.
  6. macrumors regular

    here's both my attempts at this. i hard-coded the path in place of CFBundleCopyBundleURL(CFBundleGetMainBundle()), where my help files are stored just to eliminate any further points of failure. and i can assure you, that html file is in fact at that location.

    //attempt 1
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("file:///Developer/Projects/help.html"), kCFURLPOSIXPathStyle, false);
    OSStatus result = LSOpenCFURLRef(url,NULL);
    //attempt 2
    FSRef ref = {0};
    Boolean isDir = false;			
    OSStatus result = FSPathMakeRef((UInt8 *)"file:///Developer/Projects/help.html", &ref, &isDir);			
    and sayer, if i'm understanding you correctly that would require the help files to be 'inside' the app. i'd like the help to be external from the app so updates don't require a full download of the main app, and so the user can get to help without the app being loaded.
  7. Moderator emeritus


    In your examples you are passing the absolute string of a file URL instead of the absolute path.
  8. macrumors regular

    haha kainjow, i had JUST figured that out and came back to post my result!!! haha, thanks! (and i think you meant relative in the second part)

    note to self: read EVERY line of the api documentation!
  9. macrumors regular

    for posterity:

    CFURLRef fURL = CFURLCreateWithFileSystemPath(NULL, CFSTR("Help/help.html"), kCFURLPOSIXPathStyle, false); //Help resides in folder relative to app
  10. Moderator emeritus


    Hm I could be wrong, but I don't think that code will work all the time. Did you test it by running your app outside of Xcode (e.g. via the Finder)? Xcode sets the current working directory usually to the Debug/Release directory, but the Finder doesn't.
  11. macrumors regular

    awww crap, you're right - it doesn't work when launched from finder! how do i get around that then?
  12. Moderator emeritus


    Well if the Help folder is in the same folder as your .app you can use CFBundleCopyBundleURL() and manipulate it from there (as a URL, or convert to C-string, etc). But since we're dealing with a Mac app, the Help folder *should* be located inside the .app's Resources folder, and if that's the case you can use Sayer's recommendation of CFBundleCopyResourceURL.
  13. macrumors regular

    kainjow, does this theory apply to dylib files as well? i just noticed dlopen is failing if i pass in a relative path and run my app from finder - but it works when i run it in xcode..

    if so, that would seem contradictory to the point of a 'dynamically' linked lib, methinks
  14. macrumors regular

    i was able to fix the dlopen issue using CFBundleCopyBundleURL(CFBundleGetMainBundle()), however my question still stands.

    is the mac standard to package dylib's with the .app's bundle? if this is the case, i would greatly appreciate a link to documentation on this, so i can follow these best practices!
  15. Moderator emeritus


    Yes, you can link to a dylib inside your application bundle. You have to copy it to a certain folder (probably Contents/Frameworks), and then write a run script to use install_name_tool to set the path to relative based on the directory. I used to do this with a project but I don't have the script anymore.

Share This Page