How does one download or fetch a file using iOS?
I want to fetch a plist file from a server and open it within the app.
Does there need to be a php web client on the server or how would this be done, keeping security in mind to?
NSURL *address = [NSURL URLWithString:#"http://yourwebsite.com/file.plist"];
NSString *fileString = [NSString stringWithContentsOfURL:address];
with this You can fetch file content in a string.
NSURL *address = [NSURL URLWithString:#"http://yourwebsite.com/file.plist"];
NSString *fileString = [NSString stringWithContentsOfURL:address encoding:NSUTF8StringEncoding error:nil];
after fetching you can display this content in UITextView.
[youtextview setText:fileString];
Related
I had used following code in my app but it opens new window and shows route fromcurrent location to destination.
code:
NSURL *url = [NSURL URLWithString:urlAddress];
[[UIApplication sharedApplication] openURL:url];
NSString *urlString = [NSString stringWithFormat:#"http://maps.apple.com/maps? daddr=%f,%f&saddr=%f,%f",12.8400,77.6700, 12.9610850,77.604692699999990];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];
but my requirement is to show route from current location to destination location without launching any url.
You need to request a directions for example from Google
http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true&mode=%#&departure_time=%ld
And then get the routes from received json and write the path on the map.
I think you could use a WebView and load that url in it.
You can use UIWebView and load the above URL in it
NSString *urlString = [NSString stringWithFormat:#"http://maps.apple.com/maps? daddr=%f,%f&saddr=%f,%f",12.8400,77.6700, 12.9610850,77.604692699999990];
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:escapedString];
[webveiew loadRequest : [NSURLRequest requestwithURL:url]];
Use google maps api. Go through their documentation. Its helpful. I had used it in my application.
I have done Authentication using Google authentication API.How can I access Google task values into table view and proceed
NSString *googleapi = [NSString stringWithString: #"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=bank&sensor=false&key=Your_API_Key"];
NSURL *googlePlacesURL = [NSURL URLWithString:googleapi];
NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
For more >> Working with Google Places API in iPhone
Edited Addition...
NSString *usergoogleapi = [NSString stringWithString:#"User_API_Key"];// change this api according to user.
NSString *googleapi = [NSString stringWithFormat: #"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=bank&sensor=false&key=%#",usergoogleapi];
Hope, this will help you...
I created an app which get the url of my youtube videos in the text field using GData Client Library. Now i want to shorten that url using bitly api.. But i don't have an idea about that.
if anybody done it before me, please tell me how you did it.
Thanks,
Chakradhar.
This is a quick and easy way of doing it.
You will need to register with bit.ly and obtain a login name and API key.
NSString *username = #"user";
NSString *apiKey = #"R_11111111111111";
NSString *url = #"yoururl.com";
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://api.bit.ly/v3/shorten?login=%#&apikey=%#&longUrl=%#&format=txt", username, apiKey, url]] encoding:NSUTF8StringEncoding error:nil];
Here is an iOS wrapper for bit.ly api https://github.com/st3fan/iphone-bitly
This has worked well for me, and since it is a synchroeous request there is a slight delay to fetch the link so you may want to display a Progress HUD:
NSString *accessToken = YOUR_ACCESS_TOKEN;
NSString *url = YOUR_URL;
NSString *bitlyRequest = [NSString stringWithFormat:#"https://api-ssl.bitly.com/v3/shorten?access_token=%#&longUrl=%#",accessToken, url];
NSString *bitlyResponse = [NSString stringWithContentsOfURL:[NSURL URLWithString:bitlyRequest] encoding:NSUTF8StringEncoding error:nil];
NSData *data = [bitlyResponse dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *bitlyDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *bitlyUrl = bitlyDictionary[#"data"][#"url"];
I suggest you start with theri API documentation.
I am developing Twitter API to my application. In my app, I want to display the tiny url with parsed xml link in UITexField as dynamically. statically, I could able to display tiny url in UITextField, but dynamically I am not able to display the tiny url. Please help me!
Code: statically(Working fine),
NSURL *url = [NSURL URLWithString"http://tinyurl.com/api-create.php?
url=https://gifts.development.xxxxx.edu/xxxx/Welcome.aspx?appealcode=23430"];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
Dynamically,
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", shtUrl];//shtUrl is parsed string
NSURL *url = [NSURL URLWithString:tiny];
NSString *link = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
In above, dynamically app running without warning and error, but url not displayed and when I am checking debugger, url and link both are show nil value. But shtUrl show whole url value as properly.
I tried with all NSURL class and instance methods and String methods also.
In this line of your code:
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", shtUrl];
there is a space after the 'api-create.php?'. This will result in a space in the formated string you are creating and will probably result in URLWithString: being unable to parse the url and returning nil.
Remove the extra space (assuming that it is really there and not just a cut-n-paste error) and see if that fixes the problem.
It's also possible that the shtUrl that you are building a tinyurl for contains special characters that would need to be urlencoded (i.e. percent escaped.) Try adding this:
NSString * encodedShtUrl = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)shtUrl,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 );
to encode the shtUrl, then use the encodedShtUrl when creating tiny:
NSString * tiny = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php? url=%#", encodedShtUrl];
See http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ for more about the escaping.
How do I get an NSArray of the NAMES of all files stored in a specific directory such as: "http://www.someurl/somedirectory/"?
That's really something that has to be generated server-side, then parsed on the device. I know Apache has an option where you can turn on Directory Indexes, so you could do that, then download the generated directory index and parse the HTML (using an NSXMLParser or some other parsing library), adding an NSString to an NSMutableArray every time you find a file name.
Should be something along these lines..
NSURL* url = [NSURL URLWithString: #"http://address.tld/path/files.txt"];
NSString* data = [NSString stringWithContentsOfURL: url];
if(data)
{
NSArray* files = [data componentsSeparatedByString: #"\n"];
for(NSString* filename in files)
{
NSLog(#"%#", filename);
}
}