2010-06-22 65 views
0

我已將我的應用程序與UTI關聯,以便用戶可以啓動KML附件。在我的通用應用程序的iPad應用程序代理中,我可以看到launchOptions,並從這些文件中獲取NSURL。我想把它作爲一個全局存儲,以便我可以從我的應用程序的其他地方訪問它,我使用一個名爲Engine的單例執行此操作。這是我的應用程序代表:堅持launchOptions NSURL作爲全局變量

- (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; 
} 

我的引擎類看起來是這樣的:

// 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 

我的問題是,當我嘗試從引擎訪問launchFile變量別處我的應用程序(從View控制器)調試器顯示Engine.launchFile的值。我正在訪問像這樣的變量:

- (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]; 
} 

任何幫助?

回答

0

你的代碼乍看之下看起來不錯 - 你可以在myEngine.launchFile上設置一個斷點嗎?只是爲了看看myEngine指向什麼?這應該確保你的單身代碼正在工作。

此外,你是否檢查過[launchOptions valueForKey:@「UIApplicationLaunchOptionsURLKey」]肯定會返回一個對象?你的意思是輸入:

[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey]; 

山姆

P您應該創建單一對象讀取answer to this question,還有,你應該把你的引擎班上有幾個覆蓋;)

+0

薩姆嗨, 我按照您的建議更改了我的密鑰。 我不確定我是否會在myEngine.launchURL上設置斷點(即使假設您的意思是myEngine.launchFile)。我在哪裏/如何設置這個,我將如何觀看它?當我從另一個應用程序(Mail)啓動它時,我不知道如何調試我的應用程序。 在設置myEngine.launchFile和View Controller後,我在應用程序委託中添加了以下日誌。在委託中,它報告正確的URL,在VC中它報告爲空。 NSLog(@「launchFile URL:%@」,myEngine.launchFile.absoluteString); 非常感謝您的幫助,謝謝! JP – 2010-06-22 16:45:04

+0

噢,我加了建議的覆蓋太... :-) – 2010-06-22 16:47:34

+0

你說得對myEngine.launchURL - 我的意思是myEngine.launchFile!我已經改變了這個問題,以使其他人更清楚:) – deanWombourne 2010-06-23 10:32:01