2011-11-05 54 views
0

我试图用iTunes文件共享文件共享多个文件。使用数组,为iTunes加载声音到应用程序

这是当前的代码。

Delegate.m

- (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个声音文件被共享。

感谢

+0

此代码拷贝从单个文件的绑定到文档目录。你遇到问题的代码在哪里?你想做什么? – jrturton

+0

我想分享超过1个文件。我有5个mp3,我想分享,但代码只共享1个mp3。谢谢 –

回答

0

把每个文件名到一个NSArray:

NSArray filesToShare = [NSArray arrayWithObjects:@"Test1.mp3",@"Test2.mp3",@"Test3.mp3",@"Test4.mp3",@"Test5.mp3",nil]; 

for (NSString *filename in filesToShare) 
{ 
    ... Your existing code goes here, 
} 

很明显,你会移动每次是相同的部件之前的for循环(获取路径,创建文件管理器等等)。

+0

嗨,我试过了代码。但是这些项目现在不在iTunes中显示。该应用程序显示,但没有项目。 –

0

虽然不是您的问题的答案,但向iTunes添加歌曲通常由Scripting Bridge完成。

你应该在终端中运行这个生成iTunes.h头文件:

sdef /Applications/iTunes.app | sdp -fh --basename "iTunes" 

然后在你的代码中添加这样的事情:

NSString *file = @"/path/to/test.mp3"; 
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier: @"com.apple.iTunes"]; 
iTunesTrack *track = [iTunes add: [NSArray arrayWithObject: [NSURL fileURLWithPath: file]] to: nil]; 
相关问题