dataWithContentsOfURL returns nil for smb URL - objective-c

I have a file path like so:
smb://servername/Folder/Folder/FTP/ANC/ANC.pdf
and I am trying to read this file:
NSURL *url = [NSURL fileURLWithPath:#"smb:///servername/Folder/Folder/FTP/ANC/ANC.pdf"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
but urlData is returning nil. Why? The path is 100% correct.

Related

replace in NSURL

How can I do a string replace in NSURL?
I tried stringByReplacingOccurrencesOfString but it works with NSString.
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
I want to replace image url
http://example.com/image1.jpg
to
http://example.com/img1.jpg
NSURL has an absoluteString method that you could use like so
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
You could also directly operate on the NSString from the dataList as well:
NSString *urlString = [dataList[indexPath.item][#"image"] stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
NSString *webStringURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
imageURL = [NSURL URLWithString:webStringURL];
Hope This will help you.
You are going to replace the last path component of the URL so use the dedicated API of NSURL:
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSURL *newURL = [[imageURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:#"img.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL: newURL];
stringByReplacingOccurrencesOfString could cause unexpected behavior.

NSData dataWithContentsOfURL erroneous?

Trying to parse simple URL with NSData but fails:
NSURL* url = [[NSURL alloc] initWithString:#"http://192.168.2.105:80"];
NSData* data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
Result is
error = Cocoa error 256
What reason?
Check out this:
NSURL* URL=[NSURL URLWithString: #"http://www.google.com"];
NSURLRequest* req=[NSURLRequest requestWithURL: URL];
NSData* data=[NSURLConnection sendSynchronousRequest: req returningResponse: nil error: nil];
That's a short example, I recommend to check out the response and the eventual error.
EDIT
You were right about using dataWithContentOfURL, the code that I posted does the same thing.
But it's the URL to be wrong.You should pass something like http://www.site.org .

read local html file and show on webview

I have been able to read a file called 'exerciseA.htm' from a live url. but now i need to bring the file locally add it to the project and read it from there. I have dragged the file into xcode to add it. Now i need to read its contents to a string, however the following doesnt seem to work:
NSString* exerciseAPage = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"exerciseA" ofType:#"htm"] encoding:NSUTF8StringEncoding error:&error];
[exerciseWebViewA loadHTMLString:exerciseAPage baseURL:nil];
Is this the correct way to load local htm file to a webview?
I do it the following way (Which is basically the same as you specify):
NSURL *url = [[NSBundle mainBundle] URLForResource:nameOfHtmlFile withExtension:#"html"];
NSError *error;
NSString *contentString = [NSString stringWithContentsOfURL:url encoding:NSStringEncodingConversionAllowLossy error:&error];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:contentString baseURL:baseUrl];
Hope this helps
Alex

Images & CSS not loading in iPhone UIWebView

When my UIWebView loads my index.html the CSS and JavaScript references show as broken. What is wrong with the code below? (I verified that appUrlString is pointing to the correct directory)
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#""];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
NSString *appUrlString = [NSString stringWithFormat:#"%#/web/", [[NSBundle mainBundle] resourcePath]];
NSURL *appUrl = [NSURL URLWithString:appUrlString];
[_appWebView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:appUrl];
This is what shows in the simulator.
You should use
NSURL *appUrl = [NSURL fileURLWithPath:appUrlString];
because it's a local filePath. In your version 'appURL' was giving back nil.
So no baseurl got set.

Uploading Photo with ASIHTTPRequest

I'm using ASIHTTPRequest to upload a photo with PHP. The PHP side is proven to work(I'm using it for android), and I'm trying to get the iOS component to work.
Here is my upload:
NSString *myurl = #"http://mydomain.tld/php/upload.php?casenum=";
myurl = [myurl stringByAppendingFormat: casenumber];
NSString *fixedURL = [myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSString stringWithFormat:#"%#",fixedURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *imageData = UIImageJPEGRepresentation(image1.image, 90);
[request setData:imageData withFileName:#"myphoto.jpg" andContentType:#"image/jpeg" forKey:#"file"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(#"Response: %#", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request startAsynchronous];
Ive found this on the internet and when I run it, it fails with the error 2012-01-17 10:52:16.939 MyApp[30373:707] Error: NSInvalidArgumentException
From many of the documentation and examples I've found online, this should work, but it isn't, as you can see with the exception. Any help at ironing out the kinks? If you need any other information, I'll gladly post it.
There would appear to be a couple of errors in the way that you're producing the NSURL. Firstly, it's only the parameters that need escaping, not the whole URL, so
NSString *myurl = #"http://mydomain.tld/php/upload.php?casenum=";
myurl = [myurl stringByAppendingFormat: casenumber];
NSString *fixedURL = [myurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
should be
NSString *myurl = #"http://mydomain.tld/php/upload.php?casenum=";
NSString *escapedCasenumber = [casenumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
myurl = [myurl stringByAppendingFormat:escapedCasenumber];
Secondly, you're then trying to assign an NSString to an NSURL, so
NSURL *url = [NSString stringWithFormat:#"%#",fixedURL];
should be
NSURL *url = [NSURL urlWithString:myurl];
Finally, the second argument passed to UIImageJPEGRepresentation should be a float value between 0.0 and 1.0, so I'm guessing
NSData *imageData = UIImageJPEGRepresentation(image1.image, 90);
should be
NSData *imageData = UIImageJPEGRepresentation(image1.image, 0.9);
If it still doesn't work after these changes, then do as JosephH suggests and use the debugger to identify exactly which line is causing the exception