【问题标题】:ios - transfer In App Purchase downloaded file to Document folderios - 将 In App Purchase 下载的文件传输到 Document 文件夹
【发布时间】:2014-06-15 03:01:20
【问题描述】:

我成功测试了我的应用内购买功能并下载了内容。但是,我不确定将其传输到 Document 文件夹的正确方法。我用代码测试过:

    -(void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads
    {
        for (SKDownload *download in downloads)
        {
            switch (download.downloadState) {
                case SKDownloadStateActive:
                    NSLog(@"Download progress = %f",
                          download.progress);
                    NSLog(@"Download time = %f",
                          download.timeRemaining);
                    break;
                case SKDownloadStateFinished:

                    [self downloadFromURL:download.contentURL];///<--This is the function.
                    break;
            }
        }
    }

 -(void)downloadFromURL: (NSURL *) temporaryURL {

        NSString *folderName = [[temporaryURL path] lastPathComponent];
        NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *folder = [pathArr objectAtIndex:0];

        NSString *filePath = [folder stringByAppendingPathComponent:folderName];
        NSURL *fileURL = [NSURL fileURLWithPath:filePath];

        NSError *writeError = nil;
        NSData *downloadData = [[NSData alloc] initWithContentsOfURL:temporaryURL];
        [downloadData writeToURL: fileURL options:0 error:&writeError];

        if( writeError) {
            NSLog(@"Error in writing file %@' : \n %@ ", filePath , writeError);
            return;
        }
        NSLog(@"File successfully downloaded. Url is %@",fileURL.absoluteString);
        //myFileURL = fileURL;

        NSFileManager *filemgr;
        NSArray *filelist;
        int count;
        int i;

        filemgr =[NSFileManager defaultManager];
        filelist = [filemgr contentsOfDirectoryAtPath:folder error:NULL];
        count = [filelist count];
        NSLog(@"file count %i",count);
        for (i = 0; i < count; i++)
            NSLog(@"%@", filelist[i]);
    }

我从上面的代码中得到一个@"File successfully downloaded. Url is %@",但NSLog(@"%@", filelist[i]); 没有给我任何内容。这里还缺少什么?我发现了this link 但不确定如何合并到我的代码中。任何指针都会很好。

【问题讨论】:

  • writeToURL:options:error: 返回一个 BOOL,在使用错误之前检查它。文件路径是否符合您的预期?

标签: ios download in-app-purchase nsdocumentdirectory


【解决方案1】:

让我们检查一下:

  -(void)downloadFromURL: (NSURL *) temporaryURL {
      // at this time, file is downloaded successfully???
      // I think you should check here
      NSURL * temporaryURL;
      NSFileManager *fm = [NSFileManager defaultManager];
      BOOL existed = [fm fileExistsAtPath:temporaryURL.path];
      if(existed) // file is existed 
      else        // file is not existed == maybe download fails
  }

【讨论】:

  • 我试过你的代码。结果还是一样。控制台给我"File successful...",文件夹还是什么都没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2017-02-13
  • 2016-11-23
  • 2016-03-15
相关资源
最近更新 更多