files with special characters in the name not accessible in main bundle

228 views Asked by At

I have a number of PDF files within the mainbundle of my iOS app, within my app I access these files to allow the user to select them and attach them to an email.

This is my code to get the file url:

    NSString *pdfFilePath;

    NSRange whiteSpaceRange = [currentCountry rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
    if (whiteSpaceRange.location != NSNotFound) {
        NSString *filename = [currentCountry stringByReplacingOccurrencesOfString:@" " withString:@"_"];
        NSLog(filename);
        pdfFilePath = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];
    }else{
        pdfFilePath = [[NSBundle mainBundle] pathForResource:currentCountry ofType:@"pdf"];
    }

    NSLog(pdfFilePath);
    NSLog(@"file path above");

    NSData *fileData = [NSData dataWithContentsOfFile:pdfFilePath];

This works fine if the file name is just one word for example when currentCountry = USA

However some of the pdf files have _ in their name, else where in the app the currentCountry is handled with spaces so for example

currentCountry = England and Wales

When this is the case the if statement fires and the filename is generated giving the right string og "England_and_Wales" however the file path fails and pdfFilePath is blank

UPDATE complete code below, all the code relating to this:

 (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    countrys = [NSArray arrayWithObjects:@"England and Wales", @"Scotland", @"Northen Ireland", @"Belgium", @"Canada", @"Denmark", @"France", @"Germany", @"Hong Kong", @"Holland", @"Ireland", @"South Africa", @"Singapore", @"Sweden", @"Switzerland", @"USA", @"Argentina", @"Australia", nil];
    currentCountry = @"England and Wales";
    tableData = [[NSMutableArray alloc] init];
    [self LoadDates];


}

- (void)LoadDates{
    [tableData removeAllObjects];

    NSString *plistFilePath = [[NSBundle mainBundle] pathForResource:currentCountry ofType:@"plist"];
    NSDictionary *list = [[NSDictionary alloc] initWithContentsOfFile:plistFilePath];

    NSArray *keys = [list allKeys];
    NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
    NSArray *sortedkeys = [keys sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

    for (int a = 0 ; a < [sortedkeys count]; a++){
        NSArray *dates = [list objectForKey:[sortedkeys objectAtIndex:a]];
        for(int i=0; i< [dates count]; i++)
        {
            NSString *day = [dates objectAtIndex:i];
            day = [day stringByAppendingString:@" "];
            NSString *toAdd = [day stringByAppendingString:[sortedkeys objectAtIndex:a]];
            [tableData addObject:toAdd];
        }

    }
    [_holidaytable reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)buybutton:(id)sender {
    NSLog(@"User requests to remove ads");

    if([SKPaymentQueue canMakePayments]){
        NSLog(@"User can make payments");

        SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
        productsRequest.delegate = self;
        [productsRequest start];

    }
    else{
        NSLog(@"User cannot make payments due to parental controls");
        //this is called the user cannot make payments, most likely due to parental controls
    }

}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1; // For one column
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [countrys count]; // Numbers of rows
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [countrys objectAtIndex:row]; // If it's a string
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    currentCountry = [countrys objectAtIndex:row];
    [self LoadDates];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Working Days Calculator - Public Holidays"];

    NSString *plistFilePath;

    NSRange whiteSpaceRange = [currentCountry rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
    if (whiteSpaceRange.location != NSNotFound) {
        NSString *filename = [currentCountry stringByReplacingOccurrencesOfString:@" " withString:@"_"];
        NSLog(filename);
        plistFilePath = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];
    }else{
        plistFilePath = [[NSBundle mainBundle] pathForResource:currentCountry ofType:@"pdf"];
    }

    //plistFilePath = [[NSBundle mainBundle] pathForResource:currentCountry ofType:@"pdf"];

    NSLog(plistFilePath);
    NSLog(@"file path above");

    NSData *fileData = [NSData dataWithContentsOfFile:plistFilePath];

    NSData *myData = [[NSFileManager defaultManager] contentsAtPath:plistFilePath];
    [picker addAttachmentData:fileData mimeType:@"application/pdf" fileName:currentCountry];


    // Fill out the email body text
    NSString *emailBody = @"Attached to this email is the PDF bought from the Working Days Calculator App";
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentModalViewController:picker animated:YES];
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

The files:

enter image description here

0

There are 0 answers