views:

40

answers:

1

I have associated my app with a UTI so that users can launch KML attachments. In the iPad app delegate of my universal app I can see the launchOptions and from these I get an NSURL for the file being launched. I want to store this as a global so that I can access it from elsewhere in my app, I am doing this using a singleton called Engine. This is my App Delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    Engine *myEngine=[Engine sharedInstance];

    StormTrackIpad *newVC=[[StormTrackIpad alloc] initWithNibName:@"StormTrackIpad" bundle:nil];
    [window addSubview:newVC.view];

    NSURL *launchFileURL=(NSURL *)[launchOptions valueForKey:@"UIApplicationLaunchOptionsURLKey"];

    myEngine.launchFile=launchFileURL;

    /* Show details of launched file

    NSString *message =launchFileURL.absoluteString;
    NSString *title = [NSString stringWithFormat:@"Opening file"];             
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    */

    [window makeKeyAndVisible];

    return YES;
}

My Engine class looks like this:

//  Engine.h

#import <Foundation/Foundation.h>

@interface Engine : NSObject {
    NSURL *launchFile;
}

+ (Engine *) sharedInstance;

@property (nonatomic, retain) NSURL *launchFile;

@end

//  Engine.m

#import "Engine.h"

@implementation Engine
@synthesize launchFile;

static Engine *_sharedInstance;

- (id) init
{
    if (self = [super init])
    {
        // custom initialization
    }
    return self;
}

+ (Engine *) sharedInstance
{
    if (!_sharedInstance)
    {
        _sharedInstance = [[Engine alloc] init];
    }
    return _sharedInstance;
}

@end

My problem is that when I try to access the launchFile variable from the Engine elsewhere in my app (from a View Controller) the debugger shows the value of Engine.launchFile to be . I am accessing the variable like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    Engine *myEngine=[Engine sharedInstance];

    NSURL *launchFile=myEngine.launchFile;

     NSString *title = [NSString stringWithFormat:@"Opening file"];             
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:launchFile.absoluteString  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alert show];
     [alert release]; 
}

Any help?

A: 

Hi,

Your code looks OK at first glance - can you set a breakpoint on myEngine.launchFile = just to see what myEngine is pointing at? This should make sure that your singleton code is working.

Also, have you checked that [launchOptions valueForKey:@"UIApplicationLaunchOptionsURLKey"] definitely returns an object? Did you mean to type this :

[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];

Sam

P You should read the answer to this question on creating singleton objects, there's a few overrides that you should put into your Engine class ;)

deanWombourne
Hi Sam,I've changed my key as you suggested.I'm not sure I follow you on setting a breakpoint on myEngine.launchURL (even assuming you meant myEngine.launchFile). Where/how would I set this and how would I watch it? I don't know how to debug my app when I'm launching it from another app (Mail).I added the following Log both in the app delegate after setting myEngine.launchFile, and in the View Controller. In the delegate it reports the correct URL, in the VC it reports null. NSLog(@"launchFile URL: %@", myEngine.launchFile.absoluteString);I really appreciate your help, thanks!JP
Jon-Paul
Oh, I added the overrides suggested too... :-)
Jon-Paul
You're right about myEngine.launchURL - I meant myEngine.launchFile! I've changed the question to make it clearer for anyone else reading it :)
deanWombourne
Your NSLog tells me that you have got a valid myEngine in the delegate but not in the view controller. That seems really really odd to me :( I'll have a think about what's going on but I might be stumped!
deanWombourne
Can you try this log instead in both the delegate and the view controller? __NSLog(@"shared engine : %@", [Engine sharedInstance]);__
deanWombourne
Looks like they're both valid - output from both is shared engine : <Engine: 0x11c760> :(
Jon-Paul
rewrote the class and it got resolved, not sure what I'd done. Thanks for the pointers anyway!
Jon-Paul
No problem, glad you worked it out; I had run out of ideas of what could have been wrong!
deanWombourne