0

I'm doing a shop part for iOS application which has two page (first page for show list of voicePack in shop and second page for show detail of voice inside of voicePack).

when click on any cell in the voicePackList go to next page and in next page exists one button with name : DOWNLOAD that I want when I click on that button the voice downloaded and saved in document folder. this is the code that I made inside the button pressed processing:

    - (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];
}

and here are how I put the button below the list of voices:

- (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;
}

when I click the button and put some breakpoints, it's end after if (urlData) when I check the urlData and downloadUrl, it says:

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

anyone please help me to solve this.. I'll be really thank you for your help..

송중현
  • 1
  • 1

1 Answers1

0

First of all, you are fetching voice data synchronously which is not advisable as this hangs your main thread. You can use your main thread to show the loading overlay and allow user to cancel the download operation if it is taking too much of time. Please use below method to make an Asynchronous call rather.

Secondly, can you please ensure the URL you are pointing to is open on the network you are trying to access the files. If this is a local server, better put localhost rather than IP address.

- (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
    }];
}

Third, for your debugger issue, please take a look at this thread.

Community
  • 1
  • 1
Abhinav
  • 37,684
  • 43
  • 191
  • 309