Using NSOutputStream outputStreamWithURL with a custom URLprotocol - ios

I'm writing a custom URL protocol (subclass of NSURLProtocol) to deal with local files that need a special way of reading and writing. No problem when reading: I can successfully create an URL like -for example- "my-funny-file-protocol://...." and all the things work fine.
The problem arises when I try to write to this kind of URL's.
In particular, I need to use an NSOutputStream, so I need to call something like:
NSString *urlString = [NSString stringWithFormat:#"my-funny-file-protocol://%#", path];
NSURL *url = [NSURL URLWithString:urlString];
NSOutputStream *writer = [NSOutputStream outputStreamWithURL:url append:NO];
No exception raises, no logs on the console but the result is always nil.
The same file works, if I use a standard file: protocol:
NSString *urlString = [NSString stringWithFormat:#"file://%#", path];
Why? In fact the NSURLProtocol documentation does not explain well my case.
Perhaps I'm missing someting in my URLProtocol implementation... but what?
Many thanks in advance,
Rob

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];

Pass an Url to image view through AFNetworkingLibrary

The project was created with xcode 4.3.
I have used AFNetworking Library (non-ARC) in this app.
NSURL *url = [NSURL URLWithString:urlstring];
[exhibitPortraitImageView setImageWithURL:url];
Here, when I print a NSLog value, am getting the URL but the imageview is not displayed.
How can I solve this?
Your code is fine, problem is with URL.
Your URL, don't have prefix, http://, I added it and the code is same as in quesiton.
NSString *urlstring = #"http://mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg";
NSURL *url = [NSURL URLWithString:urlstring];
[exhibitPortraitImageView setImageWithURL:url];
Here, you've three options to fix this,
1) If URLs are not coming from server (or some where else) you can fix it within the app, see how,
NSString *badUrlString = #"mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg";
NSString *goodUrlString = [NSString stringWithFormat:#"http://%#",badUrlString];
2) Or if its coming from server (or some where else) you can ask them to fix this from their side.
3) If its under 2nd option then, ask them if this will always happen (static) then you can also modify this from your side if server side developers not able to fix.
see this for more help, Check string containing URL for "http://"
Yes,
I also tried your url, it is working fine.
I loaded it as:
NSURL *correctURL = [NSURL URLWithString:#"http://mdb.scicloudsolutions.com:8001/sites/default/files/genesis-book-of-beginnings.jpg"];
_imgFromUrl.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:correctURL]];
And the result,

Playing movie downloaded with FTP in 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.

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?).

iOS dataWithContentsOfURL - what's happening behind the scenes

I've got some simple code (copy and pasted from SO) that loads a KLM (XML based) file into iOS's documents directory. I then display the loaded data on a map.
I realise that this is not a good way of downloading and saving the file - NSUrlConnection seems to be recommended so that the loading can be managed. But I'm new to all this and I'd like to understand what is happening in this case first.
Here's the code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.kml"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.domain.co.uk/kml-resource..."];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
NSURL *fileurl = [NSURL fileURLWithPath:filePath];
kmlParser = [[KMLParser alloc] initWithURL:fileurl];
.....
My questions are:
What happens while dataWithContentsOfURL is connecting/downloading - does the application just freeze and become unresponsive?
If I run my program in aeroplane mode the second time, it still seems to work. When does it decide it's OK to skip the download and writeToFile?
Does anyone know if it uses any caching between dataWithContentsOfURL and the server? ie. can I be sure that if I get a response, it is fresh data and hasn't just been sitting in safari/iOS's cache.
Many thanks
dataWithContentsOfURL is blocking method, so yeah, you shouldn't run that on main thread.
probably has internal timeout, but that is private... maybe 60 sec.
documentation doesn't say anything about caching, so I assume it doesn't cache at all.

Resources