using ios device's local sources when developing a html application - iphone

I want to develop an application which works on xcode's uiwebview. But I want to use local sources instead of internet sources. For example I want to import some photos into my project and build it with that photos. Then I want user to download them with the application (which makes sense) (My question is here! :) I want that application to use photos which are downloaded within the application.
I'm hopefull that I can do it with a path changing* trick. But I'm not sure if it's the same for all devices. And I'm not sure what I should write about path.
here is what I mean :
Is it possible?
If it's possible, how can I determine "PATH_OF_FOLDER_IN_IPHONE" path?
Thx for all suggestions & helpful answers.

Add the image as a resource to your project.
Let say the name of the image is footer.png, you get the path with the following code:
NSString *footerPath = [[NSBundle mainBundle]
pathForResource:#"footer"
ofType:#"png"];
NSURL *footerURL = [NSURL fileURLWithPath:footerPath];
You will then replace the source path of the image tag in the HTML file with the full path.

Related

file path in iOS 6 gives me null

I'm using iPhone6 simulator, I'm trying to get a file (any extension pdf of html) by using any of this codes:
NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#.pdf", documentName] ofType:nil];
or
NSString *file2 = [[NSBundle mainBundle] pathForResource:documentName ofType:#"pdf"];
I'm sure that the file in exists in Resources folder and I'm not add the file, I download it from web programmatically and see it in this path
( /Users/myMac/Library/Application Support/iPhone Simulator/6.9/Applications/E60F22DD-7301-48EF-AB25-B9D42FA6AD49/myApp.app) I see the file, but the code does not see it
but this codes sometimes gives me null and some other times gives me the file.
Why is this happen and how to prevent this and make it always gives me the file.
consider that I uses
if(file != nil)
and want to make if file == nil try to open it by using any way.
Thanks in Advance.
Make sure your file is added to project's target. Choose your target then Build Phases->Copy Bundle Resources. The file should be there.
Sometimes it's worth cleaning your project and building from scratch. I have seen cases when file added wasn't copied to simulator/device until clean was performed.
a) If you download anything from the web then the content goes to the document directory of your application.
b) No other application can interact with the your application doc directory.
c) The Bundle path i.e. the groups and files section of your application is read only.
d) No one can sneak your applications PDF unless and until you have allowed access to the document directory of your application.
Hope this helps

openURL a local file or force UIDocumentInteractionController to use a specific app

I wish to open a file (stored locally in my app) with another app.
Currently, I am using openURL (there is a dedicated url scheme), and it works fine if I use a file hosted on the internet, but I would like to use a local file so:
a) it works offline
b) a lot of the time my users are either out of cell zone coverage, or roaming internationally
What I have tried so far:
I have not had any luck telling openURL to use a local file, I have tried a few approaches but they are all something like this
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"ext"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
[[UIApplication sharedApplication] openURL:fileURL];
also
NSURL *fileURL = [[NSBundle mainBundle] URLForResource: #"test" withExtension:#"ext"];
[[UIApplication sharedApplication] openURL:fileURL];
also manually using strings with different variations of localhost/ and file:// and var/mobile etc paths
nothing works (for me anyway)
So, I looked around SO and came across UIDocumentInteractionController
I am able to use UIDocumentInteractionController to let the user open my local file with the other app - however it always present multiple options of other apps to use and for example, one of the other apps can be 'Dropbox'.
I do not want to let the user download (or upload technically) a copy of my file for their use in other ways. It contains data that I would prefer not to make so readily available.
When the file is opened by my intended app (not made by me btw) it does not allow any kind of saving or access to raw data.
I realize that by including the file in my app anyone who is serious about obtaining it will be able to, I just don't want to flash a big menu saying 'Here it is if you want your own copy to make derivative work from'
Ideally, I could use openURL but I think it is because of the 'sandbox' that the other app doesn't respond - in Android I use mode_world_readable to declare the file as readable by other apps (therefore placing it outside the sandbox, and it doesn't allow other apps to write to it, just read) - is there anyway of doing the same with iOS?
Otherwise, if I could force UIDocumentInteractionController to use a specific app and not present the menu - that would be fine too.
Similar question asked a while ago
Sorry about the long read, any help is appreciated.
Edit:
I just received an answer from Apple Tech support and they told me that this is currently impossible (just after iOS 6 released)
Yes, you're limited because of the strict sandboxing on iOS. Here are some thoughts.
You can override the functionality of the UIDocumentInteractionController by instead subclassing QLPreviewController. You can then replace the standard bar button item that displays the "Open in" menu. Take a look at this post for one solution for subclassing QLPreviewController: QLPreviewController remove or add UIBarButtonItems
Of course, I believe the way inter-app sharing works is largely out of your hands. If an app has registered to be able to handle a certain type of file, it is going to display as one of the choices in the "open in" list whether you want it to or not. I don't believe you can filter which apps display in that list.
Here are two experimental ideas I've thought about but have never tried:
You could base64 encode the data from the file you're trying to pass along--which just converts binary to text--and hand that off as part of the custom URL you use to launch the other app. Then the other app can base64 decode that same data back into binary. The down side there is that there is a limit to the length of the URL which means the "file" you're sending would have to be pretty small.
Next, and I don't even know if this is possible, but I wonder if you could use some steganograhpy algorithm to embed the document data inside an image and then hand that off to the camera roll. Then, the other app could open the camera roll and decode the image back into the data again. ... Yeah. I know... obscure, but it might be fun to try to implement. ;-)
Not sure if any of that helps, but you did say "any help is appreciated". ;-)
Best regards.

iPhone Application Support Folder Include Files BEFORE Building

I have an iPhone app that programmatically gets a path to the Application Support Folder, tests for a file in the application support folder, and then either loads the file or creates a new one depending on the result. This is easy and there are a ton of tutorials on how to do this.
But I can't for the life of me find anything in the ios documentation or online about how to put a file in the Application Support Folder before ever building the app. I tried creating a Library/Application Support in my apps Xcode folder to no avail.
Specifically, I am making a game, and I want to include level packs in the game's Library/Application Support folder BEFORE I build and run the application. Preferably by dragging and dropping the files in Finder. Is this possible?
#Vimal Venugopalan
EDIT:
As Vimal mentioned, I could use [[NSBundle mainBundle] pathForResource:ofType:inDirectory:] method, but this gives a path similar to "~/MyApp.app/MyFolder/MyFile.plist". That is if "~" was the path to the app's home directory. Or more specifically "~" is the path returned by calling the NSHomeDirectory(); function. Where I want to put my files is in "~/Library/Application Support/MyFolder/MyFile.plist"
I want the files in this spot because I want to incorporate level-packs into my game. I want to include some level packs with the app download, and I would eventually like to have additional downloadable level-packs. Now the downloaded level packs definitely have to go in the "~/Library/Application Support/" folder (which I know how to do programmatically), so I would like to include the original level-packs in the same place before building and running the app. It would be so much simpler to have all my level-packs in one place!
You can add these files in the Project and access these files at runtime Xcode will copy them in the Copy Bundle Resource phase. This normally copies into the root of the bundle. To deal with directories see #CocoaFu's answer to this SO question.
Then in the code
NSBundle* bundle = [NSBundle mainBundle] will give you the main bundle
From this you look in directories using pathForResource:ofType:inDirectory: e.g.
NSString* path = [bundle pathForResource:#"file.xml"
ofType:nil
inDirectory:#"a"];
The methods are given in NSBundle class reference also see the Bundle Programming guide
Hope this solves your issue. If not please comment

pathForResource of file created within app

I've implemented a bit of functionality that uploads a file created within the app to an FTP location, however the 'pathForResource' returns an error where it cannot find the filepath for the resource.
This is the code i'm using:
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:#"ftp://79.170.44.42"]
toUploadFile:[[NSBundle mainBundle] pathForResource:self.htmlFilePath ofType:#"text/html"]];
I've been able to upload files that are packaged with my app, for example, files that are standard 'info.plist etc'. How would I be able to specify the location of a file that is created within the app? Do I need to give a full resource path instead of just referencing the object?
[[NSBundle mainBundle]pathForResource] will only work for files that you added to the XCode Project. You need to look for the file right where you stored it when it was created.

How can I link in both remote and local assets into a webview?

I'm loading HTML-formatted content into my app from a web service, then plugging that into a local HTML template that lives within the app. Now, I need to set the UIWebView's BaseURL to point at the remote server so that all relative image links will load. However, I'm also trying to link in some local assets (CSS, JavaScript). Thus far, I have not found any documentation on how to link in local assets without relying on the UIWebView's baseURL. I've tried injecting the absolute file path of my CSS and JS into my HTML template, but it hasn't worked... I don't know if that means that it doesn't work, or if I'm just doing it wrong.
Has anyone ever run into this scenario, and if so, how did you address it? Thanks in advance, I really appreciate any tips!
How are you injecting the local path? My approach would be:
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:#"script" ofType:#"js"];
NSURL *url = [NSURL fileURLWithPath:pathToFile];
NSString *injectString = url.absoluteString;
Although, I couldn't tell you if this actually works or not, since I have never tried it. The other option is to instead use the local path as the base URL, and inject the remote path for files instead. I'm not sure how your HTML is formatted, or what changes you'd have to make to do it that way, but that would much more likely work if this approach does not.