通过iTunes文件共享在App上共享多个文件(代码显示1个文件)

时间:2011-11-05 14:09:52

标签: iphone objective-c cocoa-touch itunes

我正在尝试使用iTunes文件共享文件共享多个文件。

这是当前的代码。

Delegate.h

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

    // Override point for customization after application launch.

    // file sharing trying
    {
        NSString *fileName = @"Test.mp3";
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];

        if (![fileManager fileExistsAtPath:documentDBFolderPath])
        {
            NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
            [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
        }
    }

    [self.window makeKeyAndVisible];

    return YES;
}

目前只有1个文件被共享。

由于

1 个答案:

答案 0 :(得分:0)

我假设你想在NSArray中添加一堆文件?然后像这样循环:

NSArray *names = [NSArray arrayWithObjects: @"foo", @"bar", nil];
for (NSString *fileName in names)
{
     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSError *error;
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];

     if (![fileManager fileExistsAtPath:documentDBFolderPath])
     {
         NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
         [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
    }
}