Objective-C UIDocumentInteractionController Couldn't issue file extension for path - objective-c

I am having an issue with UIDocumentInteractionController, here is my code:
NSURL *URL = [NSURL fileURLWithPath:#"http://www.domain.com/pdf/35.pdf"];
//NSURL *URL = [[NSBundle mainBundle] URLForResource:#"35" withExtension:#"pdf"];
if (URL) {
// Initialize Document Interaction Controller
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
// Configure Document Interaction Controller
[self.documentInteractionController setDelegate:self];
// Preview PDF
[self.documentInteractionController presentPreviewAnimated:YES];
}
my console log says this:
Couldn't issue file extension for path: /http:/www.domain.com/pdf/35.pdf
and in my app it displays a gray background and says 35.pdf Portable Document Format (PDF)
what am i doing wrong?

An http URL is not a file URL.
This:
NSURL *URL = [NSURL fileURLWithPath:#"http://www.domain.com/pdf/35.pdf"];
needs to be:
NSURL *URL = [NSURL URLWithString:#"http://www.domain.com/pdf/35.pdf"];
But note that UIDocumentInteractionController expects a URL to a local resource, not an Internet URL. So you can't use the above URL at all.
You would need to download and save the PDF file locally first and then create a proper file URL to the local file.

Related

Working with two different baseUrl - AFHTTPSessionManager

I am using AFHTTPSessionManager to make api calls on the web. I have signleton object of session manager and it initiates the base url once. Occasionally, I am in a need of making api call with different baseurl. And its readonly in AFNetworking.h file.
What is the proper way of handing different baseurl here? Plese help.
+ (ApiClient *)sharedClient {
static ApiClient *_sharedClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:kTraktBaseURLString]];
});
return _sharedClient;
}
You can use the behaviour of +URLWithString:relativeToURL: method to override baseURL.
Matt mentioned it in Docs
For HTTP convenience methods, the request serializer constructs URLs from the path relative to the baseURL, using NSURL +URLWithString:relativeToURL:, when provided. If baseURL is nil, path needs to resolve to a valid NSURL object using NSURL +URLWithString:.
Below are a few examples of how baseURL and relative paths interact:
NSURL *baseURL = [NSURL URLWithString:#"http://example.com/v1/"];
[NSURL URLWithString:#"foo" relativeToURL:baseURL]; // http://example.com/v1/foo
[NSURL URLWithString:#"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:#"/foo" relativeToURL:baseURL]; // http://example.com/foo
[NSURL URLWithString:#"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo
[NSURL URLWithString:#"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
[NSURL URLWithString:#"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
So if you want to alter baseURL for single request, you can pass Absolute URL as a URLString argument to GET:parameters:success:failure: instead of URL path.
[manager GET:#"http://otherBaseURL.com/url/path" parameters:nil success:... failure:...]

How do I find the URL of a mp4 resource file?

Why can't I find a resource when it's an ".mp4"?
My code is:
- (IBAction)babelFishButton:(id)sender {
NSURL *url = [[NSBundle mainBundle] URLForResource:#"BabelFish" withExtension:#"mp4"];
MPMoviePlayerController *player =[[MPMoviePlayerController alloc] initWithContentURL: url];
[player prepareToPlay];
[player.view setFrame: self.imageView.bounds]; // player's frame must match parent's
[self.imageView addSubview: player.view];
[player play];
}
BabelFish.mp4 exists in the project navigator, and finder tells me it's in the app root folder, along with main.m, etc.
But the video does not play because url is nil (the file is not being found). I can paste in a copy of the file and rename it "BabelFish.txt", change the extension in the code and it finds the file.
Why won't it find "mp4" files? How do I get the URL of an mp4 file?
Jeff
Try this:
NSString *myUrlSTR = [[NSBundle mainBundle] pathForResource:#"BabelFish" ofType:#"mp4"];
Another thing make sure that your file is not only listed in your Project Explorer, but also included in your Copy Bundle Resources list.

Parsing NSURL in Objective-C

I have an NSTextView control that could potentially have links in it. How do I get the full url of the link?
Here is what I have so far
-(BOOL)textView:(NSTextView *)aTextView clickedOnLink:(id)aLink atIndex:(NSUInteger)charIndex
{
NSURL *htmlURL = [NSURL fileURLWithPathComponents:[aLink pathComponents]];
}
This gives me a URL that begins with file://localhost ... How do I get rid of that portion of the URL?
NSURL* url = [NSURL URLWithString:#"http://localhost/myweb/index.html"];
NSString* reducedUrl = [NSString stringWithFormat:
#"%#://%#",
url.scheme,
[url.pathComponents objectAtIndex:1]];

ipad:How to open keynote file on uiwebview

- (void)viewDidLoad
{
[super viewDidLoad];
[self loadDocument:#"mykeynote.key.zip" inView:webView];
}
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView1
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView1 loadRequest:request];
}
I received a error message after run this code "Sorry, this document can't be viewed."I am currently working keynote 09.How do i open the keynote file in uiwebview? Help me!
There's some information about it here:
http://developer.apple.com/library/ios/#qa/qa1630/_index.html
Specifically what you're looking for is this:
iWork '09 documents do not use a package format and must not be ZIP compressed.
So you shouldn't be compressing your keynote file. This was only required for '08 and earlier.
Since older iWork documents are packages, they are directories. You can use NSFileManager's attributesOfItemAtPath:error: function along with the key NSFileType to see if it's a directory (value should be NSFileTypeDirectory).
If the file is a directory you can use a library like ZipArchive to compress it.

How can I play an mp4 resource with AVPlayer?

I have included an mp4 resource in my project navigator. I want to play it in an AVPlayer. I know how to create an AVPlayerItem for the AVPlayer object, and so now I'd like to load the appropriate AVAsset into it pointing to my mp4 resource. The problem is that AVAsset only has an assetWithURL: method to create an AVAsset. I want an assetWithName: method, but that method does not exist. How can I play my mp4 file if I don't have a URL for it? If it's not possible to play via name reference, how can I get a file URL for my mp4 file?
To get the URL, use
[[NSBundle mainBundle] URLForResource:#"my_video" withExtension:#"mp4"]
It will probably be in your main bundle, you can get its URL with
- (NSString*) saveFilePath: (NSString *) add
{
NSString *filename = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource:add ofType:#"mp4"]isDirectory:NO]]];
return filename;
}
Copy this into your code and then do [self saveFilePath:#"Name of your file"] to get the URL.
Full code:
AVPlayer *player = [[AVPlayer alloc] initWithURL:[self saveFilePath:#"fileName"];
[player play];