How do I access a file from Xcode’s “Supporting Files” group, in app?

点点圈 提交于 2019-12-08 19:44:48

问题


I have an app that I've almost finish now that emails, at the end of a data entry flow, two .pdf files. One of these is generated from the entered data, the other is a static file that will be the same in every instance.

The first pdf is generating fine, it's saved to the apps 'documents' folder, and I have successfully attached to to an email for sending. It's the second .pdf, one that I made externally to the app and am trying to add, that is causing me grief.

I've added it to the 'Supporting Files' folder within my Xcode project, but after much searching on here, and elsewhere online, I can't work out how to access that file to be able to add it as an attachment in the email. Having "downloaded" the app's data from the Organizer in Xcode, I can see that the file isn't there anywhere; presumably because Xcode can see that the file isn't being used anywhere, it's not adding it at build time (very much an assumption on my part), but I don't know how to reference it anywhere because I don't know where it will be to reference it!


回答1:


The Xcode does not generate any folders in your app bundle that corresponds to groups. Just make sure that your pdf is present in Copy Bundle Resources in your target's Build Phases. You then access your resource under [[NSBundle mainBundle] bundlePath] path.




回答2:


If you want to access an image, this code is a good sample as how to:

NSString* imageName = [[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"];
NSImage* imageObj = [[NSImage alloc] initWithContentsOfFile:imageName];



回答3:


In Swift you can get the bundle-resorce-path with the following function:

func currentresourcepath () -> String
{
    return (NSBundle.mainBundle().bundlePath + "/Contents/Resources/")
}


来源:https://stackoverflow.com/questions/7686420/how-do-i-access-a-file-from-xcode-s-supporting-files-group-in-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!