0

我的 iOS 应用程序的主包中有许多 PDF 文件,在我的应用程序中,我访问这些文件以允许用户选择它们并将它们附加到电子邮件中。

这是我获取文件网址的代码:

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

如果文件名只有一个单词,例如当currentCountry = USA

但是,一些 pdf 文件的名称中有 _,否则在应用程序中 currentCountry 的位置使用空格处理,例如

currentCountry = England and Wales

在这种情况下,if 语句会触发并生成文件名,并给出正确的字符串 og "England_and_Wales" 但是文件路径失败并且 pdfFilePath 为空白

更新下面的完整代码,与此相关的所有代码:

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

文件:

在此处输入图像描述

4

0 回答 0