Does stringWithContentsOfURL use the system proxy settings? - objective-c

I am using the following code to fetch the contents of a web page but it needs to follow all the network rules etc as defined in the user's system prefs
NSURL *url = [NSURL URLWithString:#"http://thetalkingcloud.com/static/ping_desktop_app.php?v=1.0"];
NSError *theNetworkError;
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&theNetworkError];
Specifically, does stringWithContentsOfURL connect using the system wide proxy settings? (if there are any defined)

I just did some tcpdumps between Safari and an Obj-C program using stringWithContentsOfURL. Safari respected my proxy while stringWithContentsOfURL did not.

Related

Reading jpeg from file fails with "no such file" but file is clearly there

I'm running into problems reading jpeg files from the file system and displaying the image in an NSImage. Here's a snippet of code:
NSError *myError;
NSString *path = #"file:/Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url
options:0
error:&myError];
_photoImageView.image = [[NSImage alloc] initWithData:data];
Running this code generates the following error upon calling dataWithContentsOfURL:
Error Domain=NSCocoaErrorDomain Code=260 "The file “1915brsts880804of.jpg” couldn’t be opened because there is no such file."
And, additionally:
{Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}
The file does exist, and if I copy the path and paste it into a web browser, the jpeg image is displayed as expected.
I'm guessing this is some kind of permission problem that exists to prevent applications from accessing the file system directly? I had a similar problem when attempting to open files selected from an Open Panel, which turned out to be a problem with running Open/Save Panels from sandboxes, so I turned off sandboxing to get that aspect of my application working.
Does this ring a bell for anyone? I'm kind of baffled...
If you want to create an NSURL using fileURLWithPath then you need to provide a path, not a URL. Remove the use of file:.
NSString *path = #"/Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL fileURLWithPath:path];
Or you can fix the file URL and use NSURL URLWithString. Use file:// before the absolute file path so you have 3 /:
NSString *fileURLString = #"file:///Users/jpurlia/Documents/Development/Test/1915brsts880804of.jpg";
NSURL *url = [NSURL URLWithString:fileURLString];

Issues playing local files with AVPlayer?

Basically, I'm writing a video file to the Application Support directory and then saving it's path:
NSString *guid = [[NSUUID new] UUIDString];
NSString *outputFile = [NSString stringWithFormat:#"video_%#.mp4", guid];
NSString *outputDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempPath = [outputDirectory stringByAppendingPathComponent:outputFile];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath]
// code to write a video to fileURL
I save the string path itself by calling [fileURL path];
Now later when I try to create an AVAssetItem from it, I can't actually get it to play.
EDIT:
Ok, so it seems the issue is the space in Application Support. I tried saving the file to just the Library directory and it worked fine.
So the question becomes, how can I play a video I save in the Application Support directory. I wasn't able to create a valid NSURL without escaping the space (when I tried it would return a nil NSURL) but it seems that the space/escaping doesn't allow it to play correctly.
Assume the NSURL to NSString conversion is required (unless it really should be avoided).
Also, side note, but if anyone could give some insight as to why this question was down voted so I can improve the quality of my questions, that would be appreciated. I don't understand?
While I am not informed enough to opine about whether this would change if I had used matt's recommended methods: URLForDirectory:inDomain:appropriateForURL:create:error: and URLByAppendingPathComponent: the actual issue here isn't converting an NSURL to NSString
It's that I'm saving the full URL ([fileURL path]). This is because the [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0]; dynamically changes and the full path won't always point me to the appropriate file.
Instead I need to save just the name of my file (in my case I needed to persist outputFile) and then later dynamically build the the full path when I need it.
The same exact process:
NSString *outputDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempPath = [outputDirectory stringByAppendingPathComponent:outputFile];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath];
worked just fine.
TLDR: Saving a full path doesn't work

NSURLRequest doesn't send cookies

I'm developing a newsstand application and use NSURLRequest to download issue assets.
NSArray *contents = [issue.tableOfContents objectForKey:kSNTableOfContentsContents];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSLog(#"HERE GO MY COOKIES");
for (cookie in [cookieJar cookies]) {
NSLog(#"%#", cookie);
}
for (NSDictionary *contentItem in contents) {
NSString *contentURL_string = [contentItem objectForKey:kSNTableOfContentsRemoteURL];
NSURL *contentURL = [NSURL URLWithString:contentURL_string];
NSString *fileName = [contentItem objectForKey:kSNTableOfContentsContentsURL];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:contentURL];
NKAssetDownload *asset = [newsstandIssue addAssetWithRequest:request];
[request release];
....
[asset downloadWithDelegate:self];
....
}
When the first for loop is executed my cookies appear to be in NSHTTPCookieStorage, but when actual requests are sent, there are no cookie information in headers. I use CharlesProxy to look that up. Could anyone please give some advice what might be causing this issue?
From this thread, the magic incantation appears to be:
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:
[cookieJar cookies]];
[request setAllHTTPHeaderFields:headers];
(Warning: untested code.)
This will convert your cookie jar into an array of cookies, then to an NSDictionary of headers, and finally, staple those headers to your request. This is comparable to doing it manually, as Adam Shiemke linked in the question errata, but much cleaner in my opinion.
As per the documentation, you may also want to check HTTPShouldHandleCookies to see if your default cookie policy is being used properly.
On iOS projects I found the ASIHTTPRequest very useful for this kind of problems. It does things like authentication and cookies a lot better that the build-in functions:
http://allseeing-i.com/ASIHTTPRequest/

Url encodings with greek characters

I get the html source of a page to a NSString like this
NSString* url = #"example url";
NSURL *urlRequest = [NSURL URLWithString:url];
NSError *err = nil;
NSString *response = [NSString stringWithContentsOfURL:urlRequest encoding:kCFStringEncodingUTF8 error:&err];
a part of the response is like : 2 \u00cf\u0083\u00cf\u0087\u00cf\u008c\u00ce\u00bb\u00ce\u00b9\u00ce\u00b1
How can i have the Greek characters shown as they should in the NSString response?
The encoding of the page is "charset=iso-8859-7"
Ahhh, I understand your question a little bit better now.
The Apple-supplied native implementation of NSString doesn't know what to do with iso-8859-7 encoding.
You have two options.
1)
Try requesting different encodings to [NSString stringWithContentsOfURL: encoding: error:] to see if one successfully loads. My first attempt would be with NSISOLatin1StringEncoding.
2)
I found a third party library (and NSString category extension) that does do iso-8859-7 conversion. But to get access to CkoCharset will cost you (or your client) $290 USD. It might be a worthwhile investment to save time & hassle.
https://chilkatsoft.com/charset-objc.asp
and documentation is here:
http://www.chilkatsoft.com/refdoc/objcCkoCharsetRef.html

Unable to retrieve certain pages using stringWithContentsOfURL

I am trying to get HTML files from the web, using stringWithContentsOfURL:. My problem is, sometimes it works but sometimes it doesn't. For example, I tried:
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
encoding:encoding1
error:nil];
NSLog(#"html = %#",string);
This works fine, but when I replace the URL with #"http://www.youtube.com/" then I only get "NULL". Is there anyone that knows what's going on? Is it because of YouTube having some sort of protection?
Google's home page uses ISO-8859-1 encoding (aka "Latin-1", or NSISOLatin1StringEncoding). YouTube uses UTF-8 (NSUTF8StringEncoding), and the encoding you've specified with your encoding1 variable has to match the web page in question.
If you just want the web page and don't really care what encoding it's in, try this:
NSStringEncoding encoding;
NSError *error;
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
usedEncoding:&encoding
error:&error];
NSLog(#"html = %#",string);
This method will tell you what the encoding was (by writing it to the encoding variable), but you can just throw that away and focus on the string.