我已將我的應用程序與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];
}
任何幫助?
薩姆嗨, 我按照您的建議更改了我的密鑰。 我不確定我是否會在myEngine.launchURL上設置斷點(即使假設您的意思是myEngine.launchFile)。我在哪裏/如何設置這個,我將如何觀看它?當我從另一個應用程序(Mail)啓動它時,我不知道如何調試我的應用程序。 在設置myEngine.launchFile和View Controller後,我在應用程序委託中添加了以下日誌。在委託中,它報告正確的URL,在VC中它報告爲空。 NSLog(@「launchFile URL:%@」,myEngine.launchFile.absoluteString); 非常感謝您的幫助,謝謝! JP – 2010-06-22 16:45:04
噢,我加了建議的覆蓋太... :-) – 2010-06-22 16:47:34
你說得對myEngine.launchURL - 我的意思是myEngine.launchFile!我已經改變了這個問題,以使其他人更清楚:) – deanWombourne 2010-06-23 10:32:01