Ios 名称中带有特殊字符的文件在主捆绑包中不可访问
我的iOS应用程序的主捆绑包中有许多PDF文件,在我的应用程序中,我访问这些文件,以允许用户选择它们并将它们附加到电子邮件中 这是我获取文件url的代码:Ios 名称中带有特殊字符的文件在主捆绑包中不可访问,ios,objective-c,mainbundle,Ios,Objective C,Mainbundle,我的iOS应用程序的主捆绑包中有许多PDF文件,在我的应用程序中,我访问这些文件,以允许用户选择它们并将它们附加到电子邮件中 这是我获取文件url的代码: NSString *pdfFilePath; NSRange whiteSpaceRange = [currentCountry rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]]; if (whiteSpaceRange.location
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文件的名称中有uu,在应用程序中,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];
}
(无效)视图加载{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
countrys=[NSArray arrayWithObjects:“英格兰和威尔士”,“苏格兰”,“北爱尔兰”,“比利时”,“加拿大”,“丹麦”,“法国”,“德国”,“香港”,“荷兰”,“爱尔兰”,“南非”,“新加坡”,“瑞典”,“瑞士”,“美国”,“阿根廷”,“澳大利亚”,零];
currentCountry=@“英格兰和威尔士”;
tableData=[[NSMutableArray alloc]init];
[自动加载日期];
}
-(作废)装货日期{
[tableData removeAllObjects];
NSString*plistFilePath=[[NSBundle mainBundle]pathForResource:currentCountry of Type:@“plist”];
NSDictionary*list=[[NSDictionary alloc]initWithContentsOfFile:plistFilePath];
NSArray*keys=[列出所有键];
NSSortDescriptor*sortOrder=[NSSortDescriptor sortdescriptor with key:@“self”升序:是];
NSArray*sortedkeys=[使用描述符的密钥SORTEDARRAY:[NSArray ARRAY WITHOBJECT:sortOrder]];
对于(int a=0;a<[sortedkeys count];a++){
NSArray*日期=[list objectForKey:[sortedkeys objectAtIndex:a]];
对于(int i=0;i<[日期计数];i++)
{
NSString*day=[dates objectAtIndex:i];
day=[day stringByAppendingString:@”“];
NSString*toAdd=[day stringByAppendingString:[sortedkeys objectAtIndex:a]];
[表数据添加对象:toAdd];
}
}
[_holidaytable重新加载数据];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)购买按钮:(id)发送者{
NSLog(@“用户请求删除广告”);
如果([SKPaymentQueue canMakePayments]){
NSLog(@“用户可以付款”);
SKProductsRequest*productsRequest=[[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
productsRequest.delegate=self;
[产品需求启动];
}
否则{
NSLog(@“由于家长控制,用户无法付款”);
//这称为用户无法付款,很可能是由于家长控制
}
}
-(NSInteger)pickerView中组件的编号:(UIPickerView*)pickerView
{
返回1;//对于一列
}
-(NSInteger)pickerView:(UIPickerView*)pickerView行数组件:(NSInteger)组件
{
返回[国家计数];//行数
}
-(NSString*)pickerView:(UIPickerView*)pickerView标题箭头:(NSInteger)组件行:(NSInteger)组件
{
return[countrys objectAtIndex:row];//如果是字符串
}
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row不完整项:(NSInteger)组件{
currentCountry=[countrys objectAtIndex:row];
[自动加载日期];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[表数据计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“SimpleTableItem”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:simpleTableIdentifier];
}
cell.textlab.text=[tableData objectAtIndex:indexath.row];
返回单元;
}
-(无效)显示工作表
{
MFMailComposeViewController*选择器=[[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate=self;
[picker setSubject:@“工作日计算器-公共假日”];
NSString*plistFilePath;
NSRange whiteSpaceRange=[currentCountry rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
if(whiteSpaceRange.location!=NSNotFound){
NSString*文件名=[currentCountry stringByReplacingOccurrencesOfString:@”“with string:@“”];
NSLog(文件名);
plistFilePath=[[NSBundle mainBundle]pathForResource:filename of type:@“pdf”];
}否则{
plistFilePath=[[NSBundle mainBundle]pathForResource:currentCountry of type:@“pdf”];
}
//plistFilePath=[[NSBundle mainBundle]pathForResource:currentCountry of type:@“pdf”];
NSLog(plistFilePath);
NSLog(@“上面的文件路径”);
NSData*fileData=[NSData dataWithContentsOfFile:plistFilePath];
NSData*myData=[[NSFileManager defaultManager]contentsAtPath:plistFilePath];
[picker addAttachmentData:fileData mimeType:@“application/pdf”文件名:currentCountry];
//填写电子邮件正文文本
NSString*emailBody=@“此电子邮件的附件是从工作日计算器应用程序购买的PDF”;
[picker setMessageBody:emailBody isHTML:NO];
[自我呈现ModalviewController:picker动画:是];
}
-(void)mailComposeController:(MFMailComposeViewController*)控制器未完成结果:(MFMailComposeResult)结果错误:(NSError*)错误
{
开关(结果)
{
案例MFMailComposer结果已取消:
NSLog(@“邮件已取消”);
打破
案例MFMailComposer结果已保存:
NSLog(@“已保存邮件”);
打破
案例MFMailComposer结果已发送: