开发者

persisting launchOptions NSURL as global variable

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 didFini开发者_高级运维shLaunchingWithOptions:(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?


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 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜