【问题标题】:How to download data from url contains json fileurl objective C如何从 url 下载数据包含 json fileurl 目标 C
【发布时间】:2015-12-13 20:47:53
【问题描述】:

我正在为 iOS 应用程序做一个商店部分,它有两个页面(第一页显示商店中的语音包列表,第二页显示语音包内的语音详细信息)。

当单击voicePackList 中的任何单元格时,转到下一页,并且在下一页中存在一个名称为: DOWNLOAD 的按钮,当我单击该按钮时,声音下载并保存在文件夹中。这是我在按钮按下处理中制作的代码:

    - (void)downloadingVoice {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Starting Download ...");
        NSString *downloadUrl = @"10.3.1.228:9000/files";
        NSURL *url = [NSURL URLWithString:downloadUrl];
        NSData *urlData = [NSData dataWithContentsOfURL:url];

        if (urlData) {

            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *voiceDirectory = [paths objectAtIndex:0];
            NSString *voicePaths = [NSString stringWithFormat:@"%@%@", voiceDirectory,@"voice.mp3"];

            dispatch_async(dispatch_get_main_queue(), ^{
                [urlData writeToFile:voicePaths atomically:YES];
                NSLog(@"Saved voice files");
            });
        }
    });
}

- (void)btnDownloadClicked:(id)sender {
    NSLog(@"Downloading Voice . . .");

    [self downloadingVoice];
}

这是我将按钮放在声音列表下方的方式:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 60.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    // if(tableView == self.shopTableView) {
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    UIButton *download = [UIButton buttonWithType:UIButtonTypeCustom];
    [download setTitle:@"Download" forState:UIControlStateNormal];
    [download addTarget:self action:@selector(btnDownloadClicked:) forControlEvents:UIControlEventTouchUpInside];
    [download setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    download.frame = CGRectMake(0, 0, 130, 30);
    [footerView addSubview:download];

    return footerView;
}

当我单击按钮并放置一些断点时,当我检查 urlData 和 downloadUrl 时,它在 if (urlData) 之后结束,它说:

2015-09-17 10:53:01.926 Selfie[87197:674517] Starting Download ...

(lldb) po urlData
error: Couldn't materialize: couldn't get the value of variable urlData: no location, value may have been optimized out
Errored out in Execute, couldn't PrepareToExecuteJITExpression
(lldb) po downloadUrl
error: Couldn't materialize: couldn't get the value of variable downloadUrl: variable not available
Errored out in Execute, couldn't PrepareToExecuteJITExpression

任何人请帮我解决这个问题..我会非常感谢你的帮助..

【问题讨论】:

    标签: ios objective-c json nsdata nsurl


    【解决方案1】:

    首先,您正在同步获取语音数据,这是不可取的,因为这会挂起您的主线程。您可以使用您的主线程来显示加载覆盖,并允许用户在花费太多时间时取消下载操作。请使用以下方法进行异步调用。

    其次,请确保您指向的 URL 在您尝试访问文件的网络上是打开的。如果这是本地服务器,最好输入localhost 而不是 IP 地址。

    - (void)fetchFilesAsynchronously {
        NSURL *myUrl = [NSURL URLWithString:@"http://10.3.1.228:9000/files"];
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:myUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
    
        // Add headers as per your need
        [urlRequest setValue:@"value" forHTTPHeaderField:@"key"];
    
        // Add body as per your need
        NSDictionary *body = @{@"key" : @"value"};
        NSData *requestBodyData = [NSJSONSerialization dataWithJSONObject:body options:NSJSONWritingPrettyPrinted error:nil];
        [urlRequest setHTTPBody:requestBodyData];
    
        [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *iResponse, NSData *iData, NSError *iConnectionError) {
            // Handle response here
        }];
    }
    

    第三,关于你的调试器问题,请看this thread

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-18
      • 2017-04-08
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多