Downloading from FTP server iOS 7 - ios

I have tried this:
NSURL *url = [NSURL URLWithString:ftpURL];
NSData *data = [NSData dataWithContentsOfURL:url];
if (data){
[data writeToFile:dataPath atomically:(YES)];
}
But it doesn't seem to work... I have initialised dataPath, and ftpURL elsewhere..I know they aren't null. And data just returns null for some odd reason.
Does the above method support FTP download?
I have also searched on Google and StackOverflow regarding this and I couldn't find a solution that would help me.
I saw SimpleFTPSample and I thought the above method was easier.

Ok, I found out my problem, NSURL was returning null because I needed to stringByAddingPercentEscapesUsingEncoding my ftpURL string.
Thanks guys for the help anyway =]

Related

Display image on UIImageView using Web server image

I want to display an image on UIImageView calling the Web server image.
Because I want to replace and show it right away.
here's the code
NSURL *imageURL = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
webImage.image = [UIImage imageWithData:imageData];
The problem is...
The image is displayed well at first,
but if I replaced the image on the web server with another image.(The file name is the same, only the file is replaced) It didn't change and still displayed the first image.
Please help me...
The code in the question seems flawed, i.e. you apparently do a network request in the UI thread. Such approach tends to make your application UI unresponsive. However, if we boil down the question to "Why NSData returns outdated data for the same URL?" then the answer is because it has internal caching. The caching logic could controlled either by the the response headers from the server (e.g. Expires) or with NSDataReadingUncached option (to suppress any caching logic):
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];

Need to process a request to youtube inside an iOS app

If I take a browser and type http://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en into it, I get a bunch of text describing video with ID $id in a form of a file, that gets downloaded by a browser. The file contains a large string that is unique every time the request is performed.
Now, I need to gain access to that giant string inside iOS app.
Could you please tell me where to start digging? UIWebViews? Or maybe there's a simple solution?
Thanks in advance.
NSURL *url = [NSURL URLWithString:#"https://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *idString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

WCErrorDomain 7013 when trying to send image using Watch OS 2

I´m testing Apple Watch OS 2 and I´m trying to send a image from the application to the watch. According to Apple, I shall use WCSession transferFile to do this.
Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a simple dictionary of values. For example, use this method to send images or file-based documents.
for example:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
It all looks ok in the debugger, the path is correct and the file is accessible (checked with NSFileManager) and readable.
However, everytime I try I get a callback to the didFinishFileTransfer function, including an error:
Error Domain=WCErrorDomain Code=7013 "The operation couldn’t be completed. (WCErrorDomain error 7013.)"
Looking up the error:
WCErrorCodeFileAccessDenied
An error indicating that a file could not be transferred because it was inaccessible.
Available in watchOS 2.0 and later.
It seems the file is not accessible by the send function? I have tried things like resaving the file to another directory etc, but nothing seems to work.
Anyone got an idea?
The URL you are creating is not a fileURL. Try:
NSURL *path = [NSURL fileURLWithPath:string];
I managed to solve the issue!
It was because my path did not start with file://
The following code worked just fine:
NSString *string = [[NSBundle mainBundle] pathForResource:#"my_image" ofType:#"png"];
string = [NSString stringWithFormat:#"file://%#", string];
NSURL *path = [NSURL URLWithString:string];
[[WCSession defaultSession] transferFile:path metadata:#{#"meta1":#"meta2"}];
So it´s quite picky regarding the path.

Connect this NSSting with a String Variable

All I want is to change every time the NSString townLocation.
Because I take data from an API and I don't want to create different API for different location. Also I know that the "+" that I put on the link is not correct and there is not such think in Objective C but I want to make you understand what I want.
NSString*townLocation;
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://api.openweathermap.org/data/2.5/find?q="+townLocation+"&units=metric"]];
How I must do it ? Im sure you understand that I'm new at Objective C
Thank you
You only need to look into the most basic NSString documentation to find a method that will do that, stringWithFormat:.
NSString *urlString = [NSString stringWithFormat:#"http://api.openweathermap.org/data/2.5/find?q=%#&units=metric", townLocation];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
If you're new at Objective-C, a good place to find information like this is to simply search the internet or the iOS Developer Library for the class in question (in this case, NSString) to find a myriad of resources at your disposal. Another doc to check would be Formatting String Objects, which is linked in the stringWithFormat: section of the iOS Developer Library, to find more info about formatting strings.

'NSInvalidArgumentException', reason: 'data parameter is nil'

XCode newbie here and i have been trying to figure out why the following is happening...
With the way the code is currently written, i keep getting 'NSInvalidArgumentException'.
reason: 'data parameter is nil' in xcode. The url works fine on the browser.
When I remove the "filters=%7B%22region%22%3A%22CA%22%7D" part of the url, it works fine in Xcode, but when this section of the url is included, that's when I get the error message. I have tried using \" in replacement of the %22 but still nothing. Any suggestions are greatly appreciated.
NSURL *url = [NSURL URLWithString:[#"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL: url];
I also had the same problem:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
But I had solved it. The reason is my API URL is incorrect. Please check out your API URL. Good luck for you!
The code you have posted works without crashing, but the data object ends up being nil
At some point later in the code you access data. Since data == nil, the app crashes.
I suggest entering
http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi
and
http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi
into a browser to shed some light on the situation
UPDATE
The problem was encoding already encoded strings. (The %'s are being encoded to %25)
NSString *urlBase = #"http://api.v3.factual.com/t/restaurants-us?";
NSString *urlData = [#"q=peets+coffee&filters={\"region\":\"CA\"}&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",urlBase,urlData]];
NSData* data = [NSData dataWithContentsOfURL: url];
Works, because you are encoding a non-encoded string.
OR
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"]];
NSData* data = [NSData dataWithContentsOfURL: url];
Will work because as your post is, your string was already encoded properly
I had the same issue and I found one word in service name in upper case while actually it was in lower on server. I did fixed the name in URL and issue got resolved. Thanks.
I think the app crash at offline mode and you tried to NSJSONSerialization the data. Thanks
In my case, I forgot to add App Transport Security Settings in the .plist.
So just go to the .plist and add App Transport Security Settings, and then Allow Arbitrary Loads to yes.
I had the same problem. I found out that error was produced because the URL I was using to download some stuff using NSURLSession had become invalid. Removing the operation solved the problem for me.

Resources