1

In my app I have got a AMPathPopUpButton which returns the location of a file in a string(format : ~/Desktop/file.pdf). I would like to make a CGPDFDocument of this file. But my application seems unable to access the file because it is outside my app's bundle. So the CGPDFDocument is always nil. How would I use this file path string to create a working CGPDFDocument? Thanks for helping me!

some of my code: (MyName is my actual name of course)(the CFUrl is for example : file:///Users/MyName/Desktop/file.pdf)

 var document:  CGPDFDocumentRef!
 filepath = String("/Users/MyName" + String(filepath.characters.dropFirst()))
        let url = NSURL.fileURLWithPath(filepath)
        let CFUrl = url as CFURL
        document =  CGPDFDocumentCreateWithURL(CFUrl)
Eric
  • 1,210
  • 8
  • 25
  • Where are you trying it? Playground? Playground it is sandboxed you need test it with an actual project – Leo Dabus Dec 29 '15 at 18:11
  • I am trying it in an actual project, and I can get the file-path string converted to an NSURL right, but the CGPDFDocument is always nil – Eric Dec 29 '15 at 18:11
  • Are you using stringByExpandingTildeInPath? – Leo Dabus Dec 29 '15 at 18:12
  • No I am not, I will add some off my relevant code – Eric Dec 29 '15 at 18:13
  • For local resources you need to use NSURL initializer fileURLWithPath – Leo Dabus Dec 29 '15 at 18:15
  • You are hard coding your path. Where is the tilde? – Leo Dabus Dec 29 '15 at 18:20
  • Is your app sandboxed? If yes you might need appropriate access entitlements. And rather than the pretty weird `characters.dropSomething` you better use `URLByAppendingPathComponent` or `NSURLComponents ` or similar API. – vadian Dec 29 '15 at 18:25
  • http://stackoverflow.com/a/27722526/2303865 Take a look at the document directory url, get that line and change DocumentDirectory to DesktopDirectory – Leo Dabus Dec 29 '15 at 18:25
  • Then all you need is to use desktopURL.URLByAppendingPathComponent("file.pdf").path! – Leo Dabus Dec 29 '15 at 18:26
  • I will try that out, thanks a lot! – Eric Dec 29 '15 at 18:26
  • That code will only work if the file is located in the desktop folder, but it can be located about anywhere on the computer, because I use AMPathPopUpButton to choose the location of the file – Eric Dec 29 '15 at 18:28
  • I am showing you how to programmatically find the user's DesktopDirectory – Leo Dabus Dec 29 '15 at 18:30
  • Yes I understand, but it should also work if the file is located in the documents folder or somewhere else rather then the desktop. And I know where the file is located, I would like to know how to transform that into a working CGPDFDocument – Eric Dec 29 '15 at 18:31
  • Kkkk that's not the point. just being able to construct your url doesn't mean the path will have something there. You are using relative path the actual location will depend on the current directory – Leo Dabus Dec 29 '15 at 18:32
  • Could you explain why, because when I enter the path in for example Google chrome it does work? – Eric Dec 29 '15 at 18:34
  • The browser does it for you – Leo Dabus Dec 29 '15 at 18:36
  • So how would I use the filepath (~/Desktop/file.txt, ~/Documents/anotherfile.pdf, or something like this) to create a working CGPDFDocument? – Eric Dec 29 '15 at 18:38
  • Converting it to nsstring and using .stringByExpandingTildeInPath – Leo Dabus Dec 29 '15 at 18:42
  • But I would recommend using documentDirectoryURL.URLByAppendingPathComponent("file.pdf") – Leo Dabus Dec 29 '15 at 18:44
  • And use thatURL.path! – Leo Dabus Dec 29 '15 at 18:45
  • 1
    thanks a lot, a part of my app works now! – Eric Dec 29 '15 at 18:50
  • 1
    I fixed the other part too, it works! – Eric Dec 29 '15 at 18:55
  • You asked a useful question, Eric. Please consider completing the cycle and post your ultimate solution as an answer below. Best wishes! – ElmerCat Dec 31 '15 at 18:16

1 Answers1

0

Here is the final answer:

  1. first you convert the filepath string into a NSString and use a NSString function to complete the path like this:

    let path : NSString = filepath as NSString let thePath = path.stringByExpandingTildeInPath

  2. Now you can create a NSURL out of that, transform that into CFURL and make a CGPDFDocument:

    let url = NSURL.fileURLWithPath(thePath) let CFUrl = url as CFURL document = CGPDFDocumentCreateWithURL(CFUrl)

Eric
  • 1,210
  • 8
  • 25