NSURL from NSBundle path returns nil - ios

So I'm trying to use a video as a background and I have a .mov file in my app and when I run this code:
NSBundle * bundle = [NSBundle mainBundle];
NSString * urlString = [bundle pathForResource:#"movie" ofType:#"mov"];
NSURL * movieURL = [NSURL URLWithString:urlString];
if (!movieURL) {
NSLog(#"Not valid");
}
I get Not valid in the console. I checked urlString and it is giving me a url and I'm positive that the file is named correctly and is not in a directory.
So the file is there and is copied into the source. Not sure why this is doing this.

You want to use:
NSURL *moveiURL = [NSURL fileURLWithPath:urlString];
Better yet, use:
NSURL *moveURL = [bundle URLForResource:#"movie" withExtension:#"mov"];

Related

Getting nil resource URL when trying to read data file from XCAssets

I am trying to read a video file saved as a data file in the XCAssets folder of my project. I am using the following code for this -
NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:#"login_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:videoFilePath];
self.avPlayer = [AVPlayer playerWithURL: fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
This code had been working for me for quite sometime with target SDK set to iOS 8. I recently changed to iOS 9, and this now gives me a nil path and url.
The resource login_video is stored as a data file in my XCAssets folder.
What am I doing wrong here?
Thanks!
there are two way to get your url
first if you want useXCAssets
NSDataAsset *ob = [[NSDataAsset alloc]initWithName:#"a"];
NSData *obdata = ob.data;
[obdata writeToFile:#"your path" atomically:YES];
NSURL *filepath = [NSURL fileURLWithPath:#"your path"];
AVPlayerItem *player = [AVPlayerItem playerItemWithURL:filepath];
second
copy the video to mainBundle of your project and get it with
NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:#"login_video" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:videoFilePath];
not in XCAssets folder.

Creating a MSSticker with a remote image

I am trying to figure out way to create MSStickers with images that are hosted on the web. I can create MSStickers with local images, e.g.:
NSString *imagePath = [[NSBundle mainBundle] pathForResource: #"image_name"
ofType: #"png"];
NSURL *imageURL = [NSURL fileURLWithPath: urlString];
MSSticker *sticker = [[MSSticker alloc] initWithContentsOfFileURL: stickerURL
localizedDescription: #"my-sticker"
error: &err];
But I cannot do something like this:
NSString *imageURLString = #"https://my-cdn/my-sticker.png";
NSURL *imageURL = [NSURL urlWithString: urlString];
MSSticker *sticker = [[MSSticker alloc] initWithContentsOfFileURL: stickerURL
localizedDescription: #"my-sticker"
error: &err];
No, it's not possible for the moment.
But you can do this, which is not that far from what you want:
Download the picture from your server
Store it on local directory of the device
Use the URL of this local file to create your sticker
Optional : If you don't need the image anymore, erase it from the directory

Show downloaded PDF file with webView ios

I'm making an iPhone app, that will download a PDF file and display it in a webView.
However my script will not show the downloaded PDF. It does download it and save it in Documents, but the webView will not show it.
Here's my script:
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"pdf"];
NSURL *urlen = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
From the official documentation on NSURL official documentation on NSURL.
Sending nil as the argument to fileURLWithPath: produces an exception.
The problem then is actually with [[NSBundle mainBundle] pathForResource:ofType:]. This is returning nil, rather than an actual path to a file.
The problem here is actually that [NSBundle mainBundle] refers to files that are bundle with your app. You need to look in your app's document directory, which is where it stores files it has downloaded.
This method will give you the path to your app's document directory:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Now, just append the file name to this path:
NSString *pdfPath = [documentsPath stringByAppendingPathComponent:#"3.pdf"];
And for good measure (because crashes are always bad), make sure the file exists as such:
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pdfPath];
And finish as such:
if (fileExists) {
NSURL *urlen = [NSURL fileURLWithPath:pdfPath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
} else {
// probably let the user know there's some sort of problem
}

UIWebView not loading local webpage

I have a super simple webpage that I just want to load a single local file. The file is /Resources/About.html
Here is my code:
NSString *urlPath = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
What's wrong?
The method URLForResource:withExtension returns an NSURL, not an NSString. You can use the result of that directly in your request.
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
NSString * urlPath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"html"];
NSString *content = [NSString urlPath encoding:NSUTF8StringEncoding error:nil];
[webView content baseURL:[NSURL urlPath]];
I bet URLForResource is returning nil (Can you confirm that it is not?). It will not perform a recursive search, and will fail if your "Resources" folder is an actual folder (blue in XCode) as opposed to a group (yellow in Xcode). Try using the main bundle's resourceURL and appending your path to the end of it.

Getting bundle file references / paths at app launch

Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using NSFileManager? The documentation is unclear in that regard.
Note: I only need the file URLs, I do not need to access the actual files.
You can get the URL of a file in the main bundle using
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeFile" ofType:#"jpeg"];
NSURL *url = [NSURL fileURLWithPath:path];
You can write this URL to, for example, a property list file in the Documents directory:
NSString *docsDir = [NSSearchForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [docsDir stringByAppendingPathComponent:#"Files.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url absoluteString] forKey:#"SomeFile.jpeg"];
[dict writeToFile:plistPath atomically:YES];
If you don't know the names of the files and you just want to list all the files in the bundle, use
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:NULL];
for (NSString *fileName in files) {
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
// do something with `url`
}
Or in Swift 4:
let url = Bundle.main.url(forResource: "FileName", withExtension: ".xyz")
Yes, you would get their path:
NSString *path = [NSBundle mainBundle] pathForResource:#"file1" ofType:#"png"];
NSURL *fileURL = [NSURL fileURLWithPath:path]
// save it as any other object or in a dictionary:
[myMutableDictionary setObject:fileURL forKey:#"file1.png"];
EDIT: to get the complete list of files, use the NSFileManager, get the path to the bundle itself, then walk each directory getting the files, making URLs, and saving them somewhere. There is oodles of code on SO how to walk a directory to do this. [You should update your question to be more specific on what you want, this was not made clear at all originally]
There's an API on NSBundle (documentation) available on 10.6 or iOS 4 to get an NSURL directly, rather than passing a path to the NSURL constructor:
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext;
It also has variants that take a subdirectory and localizationName, the same as its -pathForResource: equivalents.

Resources