从远程url从ios应用程序下载PDF

从远程url从ios应用程序下载PDF,ios,iphone,objective-c,ios7,ios7.1,Ios,Iphone,Objective C,Ios7,Ios7.1,我正在使用下面的代码从URL下载PDF,但我得到了以下输出以及一个通知和错误 我的AppDelegate.h @interface FileDownload2AppDelegate : UIResponder <UIApplicationDelegate , NSURLConnectionDataDelegate> { NSMutableData *receivedData; } @property (strong, nonatomic) UIWindow *win

我正在使用下面的代码从URL下载PDF,但我得到了以下输出以及一个通知和错误

我的AppDelegate.h

@interface FileDownload2AppDelegate : UIResponder <UIApplicationDelegate , NSURLConnectionDataDelegate>
{
        NSMutableData *receivedData;
}

@property (strong, nonatomic) UIWindow *window;

@end
获取输出:

lockdownd[53] <Notice>: 00281000 __copy_itunes_value_block_invoke: com.apple.mobile.iTunes.store/downloaded-apps -> (null)
lockdownd[53] <Notice>: 01a63000 __copy_itunes_value_block_invoke: com.apple.mobile.iTunes.store/downloaded-apps -> (null)
networkd[79] <Warning>: Analytics Engine: double ON for app: com.aasare.FileDownload2
backboardd[28] <Error>: HID: The 'Passive' connection 'FileDownload2' access to protected services is denied.
PDF downloaded
receivedData : (null)
receivedData : (null)
Succeeded! Received 0 bytes of data
lockdownd[53]:00281000\u复制\u itunes\u值\u块\u调用:com.apple.mobile.itunes.store/download-apps->(空)
lockdownd[53]:01a63000\u复制\u itunes\u值\u块\u调用:com.apple.mobile.itunes.store/download-apps->(空)
networkd[79]:分析引擎:为app:com.aasare.FileDownload2打开两次
backboardd[28]:HID:对受保护服务的“被动”连接“FileDownload2”访问被拒绝。
PDF下载
receivedData:(空)
receivedData:(空)
成功!接收到0字节的数据

尝试更改为direceivedata,如下所示:

if(!receivedData)
    [receivedData setData:data];
else
   [receivedData appendData:data];

您可能应该在某个地方实例化
receivedData

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    receivedData = [NSMutableData data];
}
您应该附加接收到的数据

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

您下载了两次该文件,但完全放弃了第二次下载

此代码块已下载并保存数据:

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://code.google.com/p/androidnetworktester/downloads/detail?name=1mb.txt"]];
    [NSURLConnection connectionWithRequest:request delegate:self];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"/Users/awsuser8/1.txt"];
    NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://code.google.com/p/androidnetworktester/downloads/detail?name=1mb.txt"]];
    [thedata writeToFile:localFilePath atomically:YES];
这在主线程上以同步方式发生。在下载过程中,应用程序不会执行其他操作。如果你的下载量很大,或者你的连接速度很慢,你的应用程序可能在启动之前就被终止了

不要这样做。将文件保存代码放入
连接IDFinishLoading:
。这是处理网络请求的异步方式。这就是你应该如何处理与网络相关的一切

此外,要写入的文件不存在<代码>写入数据:不创建目录。您可能应该删除整个
/Users…
路径组件。或者自己创造

e、 g:


在“didReceiveData”方法中,您同时使用set和appendData。仅使用appendData和check@user2071152已尝试但得到相同的输出下载数据需要任何身份验证吗?正如我们可以观察到的错误“backboardd[28]:HID:被动”连接“FileDownload2”对受保护服务的访问被拒绝。”。如果是,您可以使用“didReceiveAuthenticationChallenge”方法提供凭据。此错误消息是否会导致任何问题?我以前从未见过,但我只是检查了一下。“我的所有应用”都会出现此错误。他们中的一些人生活在应用商店中,部署在数千台设备上。此错误出现在来自应用商店的生成上。我使用crashlytics进行崩溃报告。目前我没有看到与此错误相关的任何问题。可能保存为暂时忽略它。如何消除此错误:HID:对受保护服务的“被动”连接“FileDownload2”访问被拒绝。我没有进行下载投票,但如果您设置
receivedData=nil
,您的代码将变为
[nil setData:data]。有点没用。好吧,我明白了。如果答案是错的或没有用,否决票是对的。但我只想知道提高自己的原因。谢谢
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://code.google.com/p/androidnetworktester/downloads/detail?name=1mb.txt"]];
    [NSURLConnection connectionWithRequest:request delegate:self];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"/Users/awsuser8/1.txt"];
    NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://code.google.com/p/androidnetworktester/downloads/detail?name=1mb.txt"]];
    [thedata writeToFile:localFilePath atomically:YES];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://code.google.com/p/androidnetworktester/downloads/detail?name=1mb.txt"]];
    [NSURLConnection connectionWithRequest:request delegate:self];
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    receivedData = [NSMutableData data];
}


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

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error {
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *directoyPath = [documentsDirectory stringByAppendingPathComponent:@"Users/awsuser8"];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:directoyPath]) {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:directoyPath withIntermediateDirectories:YES attributes:nil error:&error]) {
            NSLog(@"Can't create directoy %@", error);
        }
    }
    NSString *localFilePath = [directoyPath stringByAppendingPathComponent:@"1.txt"];
    if (![receivedData writeToFile:localFilePath options:NSDataWritingAtomic error:&error]) {
        NSLog(@"Can't write file. %@", error);
    }
}