How do I programmatically access files in an ipad movies folder? - ipad

I am looking for a way to programmatically access movies in my documents or movies directory of my iPad, rather than bundling them into the Resources directory. I am programming an iPad application that is heavily dependent on movies, and is intended to be an inhouse demo tool (it will be distributed to only a few iPads in the company). With the movies bundled in to the app, it's almost a full Gig in size. I can't find any way to automatically load a file without placing it in the Resources directory:
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:#"m4v"];
MPMoviePlayerViewController *thisplayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:path]];
[self presentMoviePlayerViewControllerAnimated:thisplayer];
Am I missing something, or is there a security-driven limitation that won't allow me to use the movies directory? Much apologies if I have brainfarted.

You can use the ALAssetsLibrary class to get programmatic access to the movies stored on the user's device. (Alternatively, you can use a media picker to let the user choose which asset to use; then you'll be given a URL which you can load the movie from.)

Related

Replace Data Container of an iOS .ipa File

I have an app that downloads a whole bunch of data from over 100+ APIs upon successful login. I successfully download the data, and then use iExplorer to extract the data container folders (Documents, Library and Tmp) from the fully loaded application.
I would like to take a blank version of the original app, in .ipa format, and insert those data container folders into that fully loaded .ipa file. Then I will be able to take this new fully loaded .ipa, and use a deployment software to deploy it to a bunch of local user's devices. So everyone will have this fully loaded app.
Please, has anyone done this? Please provide some feedback, and don't argue with my methodology, because this has be done this way due to requirements. Maybe there is a step I'm missing? I'm not sure.
With the source code in hand, you can run the app in the simulator (no need for iExplorer), wait for it to download all the files and browse to the folder on your computer where the app was installed.
From there you can put aside any files you want along with their respective folders. If you're using Coredata there should be a SQLITE database file there somewhere (typically in your Application Support folder) and this might be all you need but it is hard to tell without looking at your implementation details.
Once you have the files you need set aside, add them to the app bundle via Xcode and create code to check whether files already exist (in which case you don't want to replace them), and if not copy all files needed from the bundle into their respective folders.
Here's some semi pseudo-code for you:
NSDictionary *userPrefs = [[NSUserDefaults standardUserDefaults] objectForKey:self.email];
if (![userPrefs[kInitialSetupCompleted] boolValue])
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *destinationFilePath = ...
NSURL *seedFilePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:kCoreDataStoreName ofType:nil]];
NSError* err = nil;
if (![fileManager copyItemAtURL:seedPath toURL:destinationFilePath error:&err]) {
NSLog(#"Could not copy seed data. error: %#", err);
// Deal with error
} else {
// Set user defaults kInitialSetupCompleted to YES
}
}

Storing/loading PNGs from In App Purchase

I'm developing IAPs for my app. I'm able to create the IAP package, upload to iTunes Connect and download to my to app from the App Store sandbox, and read the contents. What I'm still unclear on though is where to copy the contents of the IAP to store/load them most efficiently.
The contents of the IAP package is a set of PNGs that the user can use to customize their appearance with while using the app. The user gets a default set of PNGs with the app itself.
A few couple questions this has led to:
Where is the preferred location for storing the PNGs from the IAP? Some tutorials recommend NSDocumentDirectory while others recommend NSApplicationSupportDirectory. What are the advantages of one over the other?
How can I load the PNGs without having to track which ones are in the main bundle and which are in the IAP directory? For example, the default PNGs in the main app bundle can be loaded with:
UIImage *newHat = [UIImage imageNamed:#"fancyHat.png"];
While the PNGs from the IAPs have to be loaded like this (assuming I've copied them to the Document directory):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *inAppImagePath = [documentsDirectory stringByAppendingPathComponent:#"iapTopHat.png"];
UIImage *newHat = [UIImage imageWithContentsOfFile:inAppImagePath];
Is it possible to reconcile these, so I don't have to track which PNGs are IAPs and which are in the main bundle and then use two separate methods to load them?
It seems like ideally I could copy the PNGs to the main app bundle so I could use UIImage imageNamed:. But that seems to not be allowed and not preferred anyway.
Any assistance or guidance is greatly appreciated.
Here is a good start for what Apple suggests for data storage locations: https://developer.apple.com/icloud/documentation/data-storage/index.html
You have multiple options for digging into the bundled vs purchased content. In your example, your image name is prefixed by something that could be used to differentiate categories of content, so you can use that to determine which loading mechanism to use. You could reverse the method you ponder in the post, that is to check for the existence of the asset (whatever the name) in the bundle first, and load from there or the purchased location if not found. You could also create symlinks into the bundle if you wanted to unify the sets "on disk", but this one is trickier as you'll need to make sure your symlinks get updated if the base App UUID changes (which it will and often) during development.

Make Documents Directory Files Visible To Another App Via UIDocumentsPicker

What I Have Done:
I'm developing an application where it transfers different type of files (Eg: Images, Word Docs, Videos) between iOS devices. I'm keeping those files inside the app. I have successfully implemented iCloud Drive to share files from other applications (Eg: Documents). This is how it's done,
UIDocumentPickerViewController *documentPicker =
[[UIDocumentPickerViewController alloc] initWithDocumentTypes:
#[(__bridge NSString *) kUTTypeContent,
(__bridge NSString *) kUTTypeData,
(__bridge NSString *) kUTTypePackage,
#"com.apple.iwork.pages.pages",
#"com.apple.iwork.numbers.numbers",
#"com.apple.iwork.keynote.key"]
inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
[documentPicker release];
Objective:
I want to make my application files (Eg: Images, Word Docs, Videos) store inside the folder "Documents/Files" to be accessible via another app (Like Documents).
R & D:
This is the reference doc I'm reading to achieve this. I manage to create a folder of my app by editing the info.plist. But couldn't display my files stored inside documents directory.
Questions:
Is it possible to make the files I have stored inside documents directory visible to another app via UIDocumentsPicker or something else?.
If it is possible, is there a much easier reference document for me to read? (The current one that I'm reading is a bit difficult to understand).
Thanks.

File operation not working on device

What could make a file operation that is working well on the simulator, to not be working on an iOS device?
When using [NSBundle mainBundle], and the file is found by FileManager, what could be the different reasons for adjacent file operations to have different outcome?
I am noticing this sometimes, and just want to get an idea of what to think about when this happens.
Seems like you are trying to read a file in your application bundle. You may get its path by code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"ext"];
//Then you can use NSFileManager to read/copy it
All files in application bundle are readonly. You may get more information from here:
http://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html#//apple_ref/doc/uid/10000123i-CH104-SW8
As #Aadhira said in the comment above, the simulator stores its files inside a bunch of folders on your mac, not in some sort of simulated main bundle/docs directory sandbox.
In order to get a static file from your main bundle you must create a path starting from [NSBundle mainBundle] and add path components onto it.

Offline html5 in a UIWebView

I will have to create an offline app from a received html5 file (sort of just display it in a UIWebView, I have no idea wether it will be multiple pages or just one). I have a very limited knowledge about html programming and such, and unfortunately due to a limited time frame I can't spend much time reading up about it. Are there any limitations to trying to display this offline in a webview or maybe other 'catches' that I need to be aware of?
Thank you in advance.
If you are not changing the contents of the webpage, you can embed the HTML and all files and you won't have to worry about HTML5 since it's all already included
So create the HTML file and keep it in a folder (let's call it "index.html" and the folder "code" for this example) and drag it into your Supporting Files folder in xcode. Click the "Copy items..." checkbox and "create folder references..."
Then use this code in your ViewDidLoad:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"
inDirectory:#"code"];
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
IF YOU WANT TO PULL A FILE OFF THE WEB:
to begin the HTML5 file, start it with
<!DOCTYPE HTML>
(seriously, that's all that's needed for "html5")
Then to make it so you can view the files offline, create a "manifest" file, so next add the line
<html manifest="example.manifest">
Then in a text editor list all the items to be included for offline (index.html, logo.jpg, page2.html, logo2.jpg, etc)
boom, done
These 2 links are good resources for offline content
[http://ofps.oreilly.com/titles/9781449383268/chapOfflineApplicationCache.html][1]
http://www.html5rocks.com/en/tutorials/appcache/beginner/
[1]: http://ofps.oreilly.com/titles/9781449383268/chapOfflineApplicationCache.html [removed from web]

Resources