IOS gets various pits for local files

Source: Internet
Author: User

1. Whether: Txt,epub,pdf and other formats of the file, save to the local time, it is best to save into letters or numbers, do not save into Chinese characters, otherwise, in the file, due to the problem of coding, various watts

2. If the file name is actually saved as a Chinese character, the Transcoding method is:

[FileName                                                                       stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]; // no such processing will return nil];

3.pdf Pit: Because the file format returned in the background is DAT, you need to locally dynamically determine the file format, so when judging the PDF, there are a lot of problems

1. In the conversion between Nsurl and filepath, although FilePath is a string, but can not use url.absolutestring this method, should be used url.path this method, or the subsequent access to the file

When the string goes to the URL, this is:

[Nsurl Urlwithstring:_model.filepath]

2. Replace. dat in the file path with. pdf, no eggs:

[Afilepath  stringbyreplacingoccurrencesofstring:@ ". Dat" withstring:@ " . pdf "]

File name must be renamed

//get a local PDF file+ (Cgpdfdocumentref) Pdfrefbyfilepath: (NSString *) afilepath{//cfurlref Pdfurl = Cfbundlecopyresourceurl (Cfbundlegetmainbundle (), (__bridge cfstringref) [AFilePath LastPathCom Ponent], NULL, (__bridge cfstringref) @ "files");//cgpdfdocumentref pdfdocument = Cgpdfdocumentcreatewithurl ((cfurlref) pdfurl);//    cfstringref Path;    Cfurlref URL;    Cgpdfdocumentref document;        size_t count; //the PDF extension must be renamed before it can be taken to thensstring* aFilePath2 = [Afilepath stringbyreplacingoccurrencesofstring:@". Dat"Withstring:@". pdf"]; Nserror*error; //[[Nsfilemanager Defaultmanager] moveitematpath:afilepath2 topath:afilepath2 Error:nil];nsfilemanager* FileManager =[Nsfilemanager Defaultmanager]; if([FileManager Fileexistsatpath:afilepath]) {//get new resource path with different extension//copy it over[FileManager Copyitematpath:afilepath topath:afilepath2 error:&ERROR]; } Path=cfstringcreatewithcstring (NULL, [AFilePath2 utf8string], Kcfstringencodingutf    8); URL=Cfurlcreatewithfilesystempath (NULL, Path, Kcfurlposixpathstyle,0);    Cfrelease (path); Document=cgpdfdocumentcreatewithurl (URL);    Cfrelease (URL); if(Document = =Nil) {[FileManager copyitematpath:afilepath2 topath:afilepath Error:&ERROR]; }        //nsstring* FilePath = [Afilepath stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];//cfstringref Path;//cfurlref URL;//Cgpdfdocumentref document;//FilePath = [Afilepath stringbyreplacingoccurrencesofstring:@ ". Dat" withstring:@ ". pdf"];//path = cfstringcreatewithcstring (NULL, [FilePath utf8string], kCFStringEncodingUTF8);//url = cfurlcreatewithfilesystempath (NULL, Path, Kcfurlposixpathstyle, 0);//cfrelease (path);//document = Cgpdfdocumentcreatewithurl (URL);//cfrelease (URL);        returndocument; }

IOS gets various pits for local files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.