开发者

saving a pdf to the filesystem

How can i save a pdf from a url using NSData in my 开发者_如何学Ciphone resources, and then call those up after every app launch to load.?


Taking your question in three parts:

Downloading

In terms of downloading the PDF, the easiest means is via the NSString stringWithContentsOfURL:encoding:error: method (and other related methods in NSString) and the NSURLConnection class, which is capable of performing asynchronous background requests.

Both of the above linked class reference documents contain code samples (see the "Related sample code" section at the top of each). Additionally, there are 3rd party solutions available, such as the commonly used ASIHTTPRequest class.

Saving

Once you have the relevant NSData or NSString object, you can simply save it to a file within your apps document directory using NSData writeToFile:options:error: method or the NSString writeToFile:atomically:encoding:error:method, after getting the relevant base path via the NSSearchPathForDirectoriesInDomains foundation function as follows:

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];

Displaying

In terms of displaying the resultant PDF, you'll need to check out the CGPDFDocument Reference docs, although there are thankfully a few online examples that should help to illustrate how you can harness this class to display the PDF file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜