gpt4 book ai didi

ios - 下载 pdf 文件

转载 作者:行者123 更新时间:2023-12-01 18:23:06 25 4
gpt4 key购买 nike

我正在尝试将 pdf 文件从服务器下载到设备。这是我正在使用的代码

- (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,其值为 http://topoly.com/optimus/irsocial/Abs/Documents/2009-annual-report.pdf
在运行应用程序崩溃时,仅在 NSData 上给出 SIGABRT 错误。它提供的唯一有用信息是

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSURL 长度]:无法识别的选择器发送到实例 0xc87b600”

如何解决这个问题?

最佳答案

由于您的 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

第 2 步:使用 FileDownloader.m 创建一个 .m 文件
#import "FileDownloader.h"

@implementation FileDownloader

@synthesize _request, downloadedData, fileUrl;
@synthesize delegate;

- (void)downloadFromURL:(NSString *)urlString
{
[self setFileUrl:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

self._request = [NSMutableURLRequest requestWithURL:self.fileUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0f];
NSURLConnection *cn = [NSURLConnection connectionWithRequest:self._request delegate:self];
[cn start];
}


#pragma mark - NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if([delegate respondsToSelector:@selector(downloadingStarted)])
{
[delegate performSelector:@selector(downloadingStarted)];
}

totalFileSize = [response expectedContentLength];
downloadedData = [NSMutableData dataWithCapacity:0];
}

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

if([delegate respondsToSelector:@selector(downloadProgres:forObject:)])
{
[delegate performSelector:@selector(downloadProgres:forObject:) withObject:[NSNumber numberWithFloat:([downloadedData length]/totalFileSize)] withObject:self];
}
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if([delegate respondsToSelector:@selector(downloadingFailed:)])
{
[delegate performSelector:@selector(downloadingFailed:) withObject:self.fileUrl];
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if([delegate respondsToSelector:@selector(downloadingFinishedFor:andData:)])
{
[delegate performSelector:@selector(downloadingFinishedFor:andData:) withObject:self.fileUrl withObject:self.downloadedData];
}
}

@end

第三步:导入文件 #import "FileDownloader.h"fileDownloaderDelegate在您的 View Controller 中

第 4 步:在 viewCONtroller 的 .m 文件中定义以下委托(delegate)方法
- (void)downloadingStarted;
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;
- (void)downloadingFailed:(NSURL *)url;

第 5 步:创建 FileDownloader 的对象并将 URL 设置为下载它。
FileDownloader *objDownloader = [[FileDownloader alloc] init];
[objDownloader setDelegate:self];
[objDownloader downloadFromURL:@"Your PDF Path URL here];

第 6 步:将文件保存在您想要的位置
- (void)downloadingFinishedFor:(NSURL *)url andData:(NSData *)data;方法。

关于ios - 下载 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16049162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com