2012-05-09 41 views
0

我使用NSURLConnection的从服务器上下载的MP3文件,我的代码是使用NSURLConnection下载的内容不显示在我的iPhone上?

- (IBAction)download:(id)sender 
{ 
NSURL *url = [NSURL  URLWithString:@"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];  
    NSLog(@"%@", url); 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; 
    receivedData = [[NSMutableData alloc] initWithLength:0]; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; 


} 

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [receivedData setLength:0]; 
} 

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
[receivedData appendData:data]; 

} 

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    { 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    [connection release]; 

    } 

- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse 
    { 

    return nil; 

    } 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectoryPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"myFile.mp3"]; 

    [receivedData writeToFile:documentsDirectoryPath atomically:YES]; 

    [connection release]; 

    } 

当我点击下载按钮,将活动的指标开始动画和一段时间(我认为这是下载)后停止。但在此之后,我无法在我的iPhone磁盘上找到下载的文件。实际上这些文件在下载后存储在哪里 我编辑了我的info.plist来支持iTunes文件共享。

是他们对这段代码的任何错误?或者为什么我看不到下载的文件?

回答

0

我创建了一个类下载铃声,见第一RingtoneDwonloader.h文件

进口

@protocol RingtoneDownloaderDelegate - (无效)downloadingCompleted;

@end

@interface RingtoneDownloader:NSObject的 { NSMutableData * receivedData; NSDate * connectionTime; NSMutableString * diskPath;

id<RingtoneDownloaderDelegate>delegate; 

} @属性(非原子,保留)iddelegate;

- (void)createUrlRequestForDownloadRingtone:(NSString *)urlString; - (void)cancelDownloading;

@end

现在我RingtoneDownloader.m文件,

进口 “RingtoneDownloader.h”

进口 “AppDelegate.h”

@interface RingtoneDownloader(){ 的AppDelegate * _appDelegate; NSURLConnection * theConnection; } @end

@implementation RingtoneDownloader 
    @synthesize delegate; 
    -(void)createUrlRequestForDownloadRingtone:(NSString *)urlString{ 

_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

// create the connection with the request 
// and start loading the data 

theConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

if (theConnection) { 
    // Create the NSMutableData to hold the received data. 
    receivedData = [[NSMutableData data] retain]; 
} else { 
    // Inform the user that the connection failed. 
} 

}

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

{当服务器已经确定 //该方法被称为它
[receivedData setLength:0]; // long responseLength = [response expectedContentLength]; // NSLog(@“%ld”,responseLength); }

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

{ //追加新数据receivedData。 [receivedData appendData:data]; }

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

{ //释放连接,并且数据对象 [theConnection释放];
// receivedData被声明为其他方法实例 [receivedData release];

UIAlertView *alt = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"%@", [error localizedDescription]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
[alt show]; 
[alt release]; 
// inform the user   
NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 

}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 的NSLog(@ “成功接收的数据的%d字节!”,[receivedData长度]); //释放连接,数据对象

_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
NSString *soundFileName = _appDelegate.ringtoneURL; 
NSString *soundFileextension = [soundFileName substringFromIndex:([soundFileName length]-3)]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", _appDelegate.ringtoneText, soundFileextension]]; 
NSData *imageData = (NSData *)receivedData; 
NSError *err = nil; 
BOOL isWriten = [imageData writeToFile:filePath options:NSDataWritingAtomic error:&err];  

if(!isWriten){ 
    NSLog(@"Ringtone not saved %@", [err localizedDescription]); 
} 

[theConnection release]; 
[receivedData release];  
[delegate downloadingCompleted]; 

}

-(void)cancelDownloading{ 
[theConnection cancel]; 
[theConnection release]; 
[delegate downloadingCompleted]; 

}

现在我只好刚刚创建这个类的实例,并设置读音字创建委托。该代表向您提供有关您的铃声文件的信息是否成功下载。

在您的应用中使用iTunes文件共享,并将铃声文件复制到应用的文档目录中。

Set "Application supports iTunes file sharing" to YES in your info.plist 

用户现在可以通过itunes访问铃声并将其添加到他们的设备铃声中。

+0

此文件实际下载到哪里?文件目录?抱歉,我只是一个初学者,让我问一下如何从文档目录中检索文件?或者我可以下载文件到我的本地iPhone驱动器,以便我可以在下载后播放! –

+0

@NeerajNeeru,看到你的Mac文件中的文件,甚至你连接iPhone与Mac ...路径上的文件位置/用户/ mac用户名/库/应用程序支持/ iPhone模拟器/ 5.0 /应用程序/ urAppBundleID /文件 – Tirth

+0

@NeerajNeeru,是你也可以使用文件目录路径播放该文件给Audioplayer – Tirth

相关问题