简体   繁体   English

将文件下载到iPhone中的应用程序目录

[英]Download file to app directory in iphone

I am new to iPhone, 我是新来的iPhone,

I am currently developing an iPhone app and would like to implement the ability to download file from the internet. 我目前正在开发iPhone应用程序,并希望实现从互联网下载文件的功能。 I have created the UIWebView , but want to know the best way of capturing the files when they are linked to in the webview and then download them to a specified folder in the documents directory. 我已经创建了UIWebView ,但是想知道在链接到Webview时捕获文件并将其下载到documents目录中指定文件夹的最佳方法。

Here is my code snippet, 这是我的代码段,

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{ 
    [self.fileData setLength:0];
}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
 {
       [self.fileData appendData:data1]; 
 }

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
     [activityIndicator stopAnimating];
     activityIndicator.hidden=TRUE;
}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

    url = [request URL];

    //CAPTURE USER LINK-CLICK.
    NSString *file = [NSString stringWithString:[url absoluteString]];;
    NSURL *fileURL = [NSURL URLWithString:file];
    NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
    [NSURLConnection connectionWithRequest:req delegate:self];
     data = [NSData dataWithContentsOfURL:url];

            //Saving file at downloaded path.

        DirPath = [DestPath stringByAppendingPathComponent:[url lastPathComponent]];
        [data writeToFile:DirPath atomically:YES];

        UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"Download Complete !"
                                                                             message:nil delegate:nil 
                                                                   cancelButtonTitle:@"OK"
                                                                   otherButtonTitles:nil];
                        [Alert show];
                        [Alert release];



    return YES;   
}

Problem is where to write condition, if my downloading gets failed and also i am getting Warning in my log shows : “wait_fences: failed to receive reply: 10004003” 问题是在哪里写条件,如果我的下载失败并且我也收到警告,则我的日志中显示警告: “wait_fences: failed to receive reply: 10004003”

It seems like you're doing the same thing multiple times in this code. 似乎您在这段代码中多次做相同的事情。 For example, you create a new NSURLRequest object even though one was already passed inside of the delegate method? 例如,即使已经在委托方法内部传递了一个新的NSURLRequest对象,您还是创建了一个新的NSURLRequest对象? You also run the synchronous dataWithContentsOfURL method after creating a new NSURLConnection? 您还可以在创建新的NSURLConnection之后运行sync dataWithContentsOfURL方法吗? You also append some data into a property only to do nothing with that property? 您还将某些数据附加到属性中只是对该属性不做任何事?

What you probably want to do is create a new asynchronous NSURLConnection when the UIWebView should load. 您可能想做的是在应加载UIWebView时创建一个新的异步NSURLConnection。 From there, allow the UIWebView to load that page. 从那里,允许UIWebView加载该页面。 Inside of your delegate methods, instead of appending the data to some property, append the downloaded data to a file. 在委托方法内部,不要将数据附加到某些属性,而是将下载的数据附加到文件。 When the connection finishes downloading, present your alert informing the user that the data was downloaded and saved. 连接完成下载后,显示警报,通知用户数据已下载并保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM