Ios 下载pdf文件
我正在尝试将pdf文件从服务器下载到设备。这是我正在使用的代码Ios 下载pdf文件,ios,objective-c,Ios,Objective C,我正在尝试将pdf文件从服务器下载到设备。这是我正在使用的代码 - (id)initwithURL:(NSString*)remoteFileLocation andFileName:(NSString*)fileName{ //Get path to the documents folder NSString *resourcePathDoc = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]resource
- (id)initwithURL:(NSString*)remoteFileLocation andFileName:(NSString*)fileName{
//Get path to the documents folder
NSString *resourcePathDoc = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]resourcePath]stringByDeletingLastPathComponent]stringByAppendingString:@"/Documents/"]];
localFilePath = [resourcePathDoc stringByAppendingString:fileName];
BOOL fileExists = [[NSFileManager defaultManager]fileExistsAtPath:localFilePath];
if (fileExists == NO) {
NSURL *url = [NSURL URLWithString:remoteFileLocation];
NSData *data = [[NSData alloc] initWithContentsOfURL: url];
//Write the data to the local file
[data writeToFile:localFilePath atomically:YES];
}
return self;
}
其中remoteFileLocation是一个NSString,具有
运行应用程序时崩溃,只是NSData出现SIGABRT错误。它提供的唯一有用的信息是
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSURL length]:未识别的选择器发送到实例0xc87b600'
如何解决此问题?您的
remoteFileLocation
参数值实际上是NSURL
对象,而不是NSString
。仔细检查如何获取/创建remoteFileLocation
,并验证它确实是NSString
这段代码还有其他几个问题。创建文档目录路径的正确方法如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *localFilePath = [resourcePathDoc stringByAppendingPathComponent:fileName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:localFilePath];
if (!fileExists) {
NSURL *url = [NSURL URLWithString:remoteFileLocation];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
//Write the data to the local file
[data writeToFile:localFilePath atomically:YES];
}
似乎您的
remoteFileLocation
参数值实际上是一个NSURL
对象,而不是一个NSString
。仔细检查如何获取/创建remoteFileLocation
,并验证它确实是NSString
这段代码还有其他几个问题。创建文档目录路径的正确方法如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *localFilePath = [resourcePathDoc stringByAppendingPathComponent:fileName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:localFilePath];
if (!fileExists) {
NSURL *url = [NSURL URLWithString:remoteFileLocation];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
//Write the data to the local file
[data writeToFile:localFilePath atomically:YES];
}
由于您的PDF文件太大,所以如果您进行同步下载,下载时间将太长,因此我坚持您创建一个异步下载程序并使用它。我已经为相同的代码 步骤1:创建文件“FileDownloader.h”
#define FUNCTION_NAME NSLog(@"%s",__FUNCTION__)
#import <Foundation/Foundation.h>
@protocol fileDownloaderDelegate <NSObject>
@optional
- (void)downloadProgres:(NSNumber*)percent forObject:(id)object;
@required
- (void)downloadingStarted;
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
- (void)downloadingFailed:(NSURL *)url;
@end
@interface FileDownloader : NSObject
{
@private
NSMutableURLRequest *_request;
NSMutableData *downloadedData;
NSURL *fileUrl;
id <fileDownloaderDelegate> delegate;
double totalFileSize;
}
@property (nonatomic, strong) NSMutableURLRequest *_request;
@property (nonatomic, strong) NSMutableData *downloadedData;
@property (nonatomic, strong) NSURL *fileUrl;
@property (nonatomic, strong) id <fileDownloaderDelegate> delegate;
- (void)downloadFromURL:(NSString *)urlString;
@end
步骤3:在viewController中导入文件#导入“FileDownloader.h”
和fileDownloaderDelegate
步骤4:在viewCOntroller的.m文件中定义以下委托方法
- (void)downloadingStarted;
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
- (void)downloadingFailed:(NSURL *)url;
第五步:创建FileDownloader的对象,并设置URL以下载它
FileDownloader *objDownloader = [[FileDownloader alloc] init];
[objDownloader setDelegate:self];
[objDownloader downloadFromURL:@"Your PDF Path URL here];
第6步:将文件保存到所需位置
-(void)下载完成:(NSURL*)url和数据:(NSData*)数据代码>方法。由于您的PDF文件太大,因此如果您进行同步下载,下载时间将太长,因此我坚持您创建一个异步下载程序并使用它。我已经为相同的代码
步骤1:创建文件“FileDownloader.h”
#define FUNCTION_NAME NSLog(@"%s",__FUNCTION__)
#import <Foundation/Foundation.h>
@protocol fileDownloaderDelegate <NSObject>
@optional
- (void)downloadProgres:(NSNumber*)percent forObject:(id)object;
@required
- (void)downloadingStarted;
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
- (void)downloadingFailed:(NSURL *)url;
@end
@interface FileDownloader : NSObject
{
@private
NSMutableURLRequest *_request;
NSMutableData *downloadedData;
NSURL *fileUrl;
id <fileDownloaderDelegate> delegate;
double totalFileSize;
}
@property (nonatomic, strong) NSMutableURLRequest *_request;
@property (nonatomic, strong) NSMutableData *downloadedData;
@property (nonatomic, strong) NSURL *fileUrl;
@property (nonatomic, strong) id <fileDownloaderDelegate> delegate;
- (void)downloadFromURL:(NSString *)urlString;
@end
步骤3:在viewController中导入文件#导入“FileDownloader.h”
和fileDownloaderDelegate
步骤4:在viewCOntroller的.m文件中定义以下委托方法
- (void)downloadingStarted;
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
- (void)downloadingFailed:(NSURL *)url;
第五步:创建FileDownloader的对象,并设置URL以下载它
FileDownloader *objDownloader = [[FileDownloader alloc] init];
[objDownloader setDelegate:self];
[objDownloader downloadFromURL:@"Your PDF Path URL here];
第6步:将文件保存到所需位置
-(void)下载完成:(NSURL*)url和数据:(NSData*)数据代码>方法。GCD可用于海量文件。如果愿意,您可以在第二个线程上下载同步文件并发回主线程。您还可以使用操作队列
实际上,您还可以使用NSURLConnection中的委托方法来处理主线程上的回调。但是,定义自己的委托已经过时,因为您可以从NSURLConnection本身实现委托。GCD可用于海量文件。如果愿意,您可以在第二个线程上下载同步文件并发回主线程。您还可以使用操作队列
实际上,您还可以使用NSURLConnection中的委托方法来处理主线程上的回调。但是,定义自己的委托已经过时,因为您可以从NSURLConnection本身实现委托。谢谢您的回答。我现在正在使用GCD。这与使用NSURL连接有什么不同呢?您可以对小文件使用GCD,而您必须下载大文件才能使用NSURL连接,因为它具有要通知的委托方法。而GCD没有。谢谢@SURESHSANKE。我将很快在我的GIT上发布其他支持工具。你可以在那里查一下。谢谢你的回答。我现在正在使用GCD。这与使用NSURL连接有什么不同呢?您可以对小文件使用GCD,而您必须下载大文件才能使用NSURL连接,因为它具有要通知的委托方法。而GCD没有。谢谢@SURESHSANKE。我将很快在我的GIT上发布其他支持工具。你可以在那里查一下。