Playing movie downloaded with FTP in iOS - ios

I have an application that downloads movies from an ftp-server and then plays them with an MPMoviePlayerController. However the movieplayer fails with MPMovieFinishReasonPlaybackError.
the code looks like this:
NSURL *url = [NSURL fileURLWithPath:[contents objectAtIndex:index]];
NSLog(#"url: %#",url);
self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:url];
the nslog gives this answer:
url: file://localhost/var/mobile/Applications/E8C9DFE8-9802-4EC1-B560-3EEE96E0AF5E/Documents/media/testfilm.mov
Does anybody have an idea on how to get the movie to play? If I add the movie to the project and use the following code the movie works.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"testfilm" ofType:#"mov"]];

You are using fileURLWithPath: for a remote file, that is wrong.
Use URLWithString: for remote files instead.
See the reference on that exact subject - particularly the parameters section;
URLWithString:
Creates and returns an NSURL object initialized with a provided string.
+ (id)URLWithString:(NSString *)URLString
Parameters
URLString
The string with which to initialize the NSURL object. Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808. (To create NSURL objects for file system paths, use fileURLWithPath:isDirectory: instead.)
Return Value
An NSURL object initialized with URLString. If the string was malformed, returns nil.
Discussion
This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘#’. Note that ‘%’ escapes are translated via UTF-8.

Related

NSURL set from variables

A pretty simple question i am sure, but i don't know how to go about doing such a thing. At current i $post variable 1 and variable 2 to the server and return some son data this is pretty slow but it worked, then it all went down hill when i started to have execution issues on the server side. Rented server so can't change php.ini file. So i have came up with this idea as a work around.
Would it be possible to direct to my download source, if the link itself contained variables collected from my label.text rather than writing each individual link out for each group (127 to be precise). For instance variable1 would equal "hell" and variable to would equal "red" therefore my NSURL would point to www.test.com/hell/red.php
Is this possible or is there some other way of doing this?
NSURL *url = [NSURL URLWithString:#"http://www.test.com/$variable1/$variable2.php"];
So now i know that this is possible, is some form or way, how can i resolve the following errors that i am receiving? For what i understand i simply can't have a / between the two variables and i can't have the .json file extension included on the end of the url.
If you require anymore code don't hesitate to ask.
Thank you very much in advance for any help!
The string you are creating the NSURL with is a normal NSString. To use variables in a NSString, you can use:
[NSString stringWithFormat:#"http://www.test.com/%#/%#.php", variable1, variable2];
%# is a placeholder for a string. variable1 and variable2 must be NSStrings
The line that creates could look like this:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.test.com/%#/%#.php", variable1, variable2]];
Or:
NSString * urlString = [NSString stringWithFormat:#"http://www.test.com/%#/%#.php", variable1, variable2];
NSURL * url = [NSURL URLWithString:urlString];
You can try this one:
NSString *urlString = [NSString stringWithFormat:#"http://www.test.com/%#/%#.php", var1, var2];
NSURL *url = [NSURL URLWithString:urlString];

NSURL URLWithString returns nil

I am stuck in a very obvious method of NSURL class URLWithString I am passing a string and while creating URL it returns nil, my string is perfect. When ever I uses same string in browser then it is working fine. I am using following method to convert NSString to NSURL:
NSURL *url = [NSURL URLWithString:urlString];
//urlString is my perfect string
I have also tried to encode my string first by using following
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// using this line my output string when I log url is "url%10%10%10%10%10%10..."
%10 becomes the suffix of my url with around 100+ repetition.
If any one has idea what %10 is or what can be done to overcome this problem.
here is my urlString:
Use below code to resolve your problem.
NSString *str = msgDetail[#"thumbnail"];
NSString* webStringURL = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:webStringURL];
yes what was wrong in my string were many unidentified characters due to copying from internet site, if anyone of the reader facing this issue can copy and paste your string as see the count. And confirm.
Thanks
Try below solution
NSURL *url = [NSURL URLWithString:[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Check it..!

+URLWithString: returns nil

Im using QLPreviewController to view word, excel,pdf etc. The problem is the file is in the server so I need to download it from a certain URL.
I'm using this code:
NSURL *dLurl = [NSURL URLWithString:[NSString stringWithFormat:#"%#",DLPathStr]];
NSData *data = [NSData dataWithContentsOfURL:dLurl];
for downloading files, It works for the PDF file but in the word and excel files it doesn't work I'm not able to download anything.
I fixed it by using this code it doesn't download anything because the URL is wrong because it contains spaces.
NSString *str = [DLPathStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *dLurl = [NSURL URLWithString:[NSString stringWithFormat:#"%#",str]];

IOS - How do I add segments to NSURL?

Suppose you have a URL that looks like http://localhost:5867 and your wanted to append '/something' to the url. How do I append segments to this URL?
I am trying this:
//_baseURL is NSURL* and documentid is NSString*
NSURL* url = [_baseURL URLByAppendingPathComponent:documentid];
Oddly.. xcode(4.6.2) will not output the resulting URL to console.
NSLog(#"%#", [url absoluteString]);//nothing output
Thanks!
Edit
I am working in a workspace. The code that I am debugging is a static lib which is in the workspace. Could this be the culprit of all this weirdness??
Fixed
I think the reason I was having problems was because my workspace was not configured correctly and I was actually executing an older version of my static lib. This explains why I wasn't getting NSLog'ing and why the result were unexpected. I found this post which helped me understand the workspace and how to configure it for a static library. Thanks a lot for everyone's time!
There is a class message,
+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL
So,
NSURL *base = [NSURL URLWithString: #"http://localhost:5867"];
NSURL *child = [NSURL URLWithString: #"something" relativeToURL:base];
NSURL *base = [NSURL URLWithString:#"http://localhost:5867"];
NSURL *url = [base URLByAppendingPathComponent:#"something"];
NSLog(#"%#", [url absoluteString]);
This works for me and prints http://localhost:5867/something, so check your variables (maybe one of them is nil or an empty string?).

NSURL URLWithString:relativeToURL: is clipping relative url

I'm trying to implement an iOS app, which uses RestKit. In all examples I've seen so far the following code is used to create the URLs:
NSURL *baseURL = [NSURL URLWithString:#"https://api.service.com/v1"];
NSURL *relativeURL = [NSURL URLWithString:#"/files/search" relativeToURL:baseURL];
But then [relativeURL absoluteString] will return https://api.service.com/files/search.
So I tried a few examples:
NSURL *baseURL1 = [NSURL URLWithString:#"https://api.service.com/v1/"];
NSURL *baseURL2 = [NSURL URLWithString:#"https://api.service.com/v1"];
NSURL *baseURL3 = [NSURL URLWithString:#"/v1" relativeToURL:[NSURL URLWithString:#"https://api.service.com"]];
NSURL *relativeURL1 = [NSURL URLWithString:#"/files/search" relativeToURL:baseURL1];
NSURL *relativeURL2 = [NSURL URLWithString:#"/files/search" relativeToURL:baseURL2];
NSURL *relativeURL3 = [NSURL URLWithString:#"/files/search" relativeToURL:baseURL3];
NSURL *relativeURL4 = [NSURL URLWithString:#"files/search" relativeToURL:baseURL1];
NSURL *relativeURL5 = [NSURL URLWithString:#"files/search" relativeToURL:baseURL2];
NSURL *relativeURL6 = [NSURL URLWithString:#"files/search" relativeToURL:baseURL3];
NSLog(#"1: %#", [relativeURL1 absoluteString]);
NSLog(#"2: %#", [relativeURL2 absoluteString]);
NSLog(#"3: %#", [relativeURL3 absoluteString]);
NSLog(#"4: %#", [relativeURL4 absoluteString]);
NSLog(#"5: %#", [relativeURL5 absoluteString]);
NSLog(#"6: %#", [relativeURL6 absoluteString]);
And this is the output:
1: https://api.service.com/files/search
2: https://api.service.com/files/search
3: https://api.service.com/files/search
4: https://api.service.com/v1/files/search
5: https://api.service.com/files/search
6: https://api.service.com/files/search
So the only example returning what I want is #4. Can anyone explain why?
I read [RFC1808] which defines the normative algorithm for resolving relative URLs
2 issues:
the relative url may not start with a / or it is considered to be absolute:
Step 4: If the embedded URL path is preceded by a slash "/", the
path is not relative and we skip to Step 7."
when the base url ends with a non-slash. everything from the last slash on is skipped
Step 6: The last segment of the base URL's path (anything
following the rightmost slash "/", or the entire path if no
slash is present) is removed and the embedded URL's path is
appended in its place. The following operations are
then applied, in order, to the new path:"
so that explains it. the baseURL needs to end in a / and the relative url shouldn't start with a /
You have two problems here:
Firstly, the string /files/search is an absolute path since it starts with a slash. Resolving it against any existing URL will ignore the existing path.
Secondly, https://api.service.com/v1 does not have a trailing slash to indicate it's a directory. Any strings resolved against it will always ignore the v1 portion.
To conclude, you need that combo of a relative path — files/search — and directory base URL — https://api.service.com/v1/.
Another example:
//it's right
NSURL *url = [NSURL URLWithString:#"getValidNumber" relativeToURL:[NSURL URLWithString:#"http://dns.test.com:22009/service/"]];
//error
NSURL *url = [NSURL URLWithString:#"getValidNumber" relativeToURL:[NSURL URLWithString:#"dns.test.com:22009/service/"]];
//error
NSURL *url = [NSURL URLWithString:#"/getValidNumber" relativeToURL:[NSURL URLWithString:#"http://dns.test.com:22009/service"]];
`
The 'http://' is necessary to use this method.

Resources