【问题标题】:How to download the mp3 file from webserver into my iPhone app?如何将 mp3 文件从网络服务器下载到我的 iPhone 应用程序中?
【发布时间】:2011-03-21 10:31:10
【问题描述】:

我在服务器上有一个 mp3 文件列表。

现在我希望当用户单击按钮时,所选的 mp3 文件应该被下载到他/她的 iPhone 中。

怎么做?

非常感谢任何与此相关的文章或教程的参考。

【问题讨论】:

    标签: iphone cocoa-touch ios ios4


    【解决方案1】:

    其中一种技术正在使用:

     NSMutableURLRequest
    

    您可以按如下方式初始化 URL 请求:

    NSURL *URL = [NSURL URLWithString:@"your server url"];
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:15.0];
    
    NSURLConnection *URLConneSync = [NSURLConnection connectionWithRequest:request delegate:self];
    

    还有 NSURLConnection 的代表:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    

    更新

    在 RecieveData Delegate 上,您可以执行以下操作:

     NSFileManager *fileManager;
     [fileManager createFileAtPath:@"Your Path" contents:nil attributes:nil];
    
      NSFileHandle *FileHandle = [NSFileHandle fileHandleForWritingAtPath:@"Your Path Again"];
    

    注意:您可以通过以下方式获取 iphone/iPad 沙盒路径:

     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *sDocumentsDir = [documentPaths objectAtIndex:0];
    

    【讨论】:

    • 这个怎么下载mp3文件?
    • 您需要在委托 didReceiveResponse 上创建一个新文件,并将数据附加到 deleaget didReceiveData 上的文件中。
    • 嘿,你能提供更多关于didReceiveResponse方法应该做什么的代码吗?
    • @PARTH:祝你好运,如果您需要任何进一步的帮助,请告诉我。
    【解决方案2】:

    检查Simple URL Connections 代码示例... 它解释了如何获取、放置和发布数据到服务器......

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多