2
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
}


NSString *dbFilename = @"CoordinatesDatabase.sql";
NSString *userPath = [[CoordinatesAppDelegate applicationDocumentsDirectory] stringByAppendingPathComponent: dbFilename];
NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: dbFilename];
NSLog(@"userPath %@", userPath);
NSLog(@"srcPath %@", srcPath);
NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL foundInBundle = [fileManager fileExistsAtPath: srcPath];
BOOL foundInUserPath = [fileManager fileExistsAtPath: userPath];
BOOL copySuccess = FALSE;

NSError *error;

if(foundInBundle)
{
    if(!foundInUserPath)
    {
        NSLog(@"don't have copy, copy one");
        copySuccess = [fileManager copyItemAtPath: srcPath toPath: userPath error: &error];
    }
    if(!copySuccess)
    {
        NSLog(@"Failed with message: %@'.", [error localizedDescription]);
    }
}
else
{
    NSLog(@"Some error");
}
return YES;
}

foundInBundle によると、CoordinatesDatabase.sql はアプリケーション バンドルに存在します。「copySuccess = [fileManager copyItemAtPath: srcPath toPath: userPath error: &error];」ができないのはなぜですか? 成功する?

それが生成するエラーは、「操作を完了できませんでした。Cocoa エラー 4」のようなものです。私のグーグルによると、Cocoa Code Error 4 は NSFileNoSuchFileError を意味しますが、アプリバンドルに CoordinatesDatabase.sql があることは確かです。

または、CoordintesDatabase.sql がアプリ バンドルに存在するというのは間違っていますか?

4

1 に答える 1

0

アプリ バンドル内のファイルを作成、削除、または変更することはできません。ファイルを適切なドキュメントまたはキャッシュ フォルダーに書き出す必要があります。また、iOS4 と iOS5 の間でルールが変更されたため、これも少し複雑です。

また、間違ったフォルダーにファイルを作成したためにアプリが拒否されることもあります。最終的に、実行している OS のバージョンに応じてドキュメント フォルダーの作成を処理するメソッドを作成しました... ユーザーが電話の OS をアップグレードしたときに、古いフォルダーから新しいフォルダーへのデータの移行をサポートします。あなたも同じことをするべきです。

この問題に対処する他の質問をここで確認することをお勧めします: iOS PhoneGap アプリは localStorage を使用しているため拒否されました

于 2012-10-10T15:44:53.273 に答える