Adding components to path - Objective C - iphone

I have a url say : www.gmail.com/index.php. I also have three different strings say aa, bb and cc. I want to append the three separate strings to the url in order to get a combined url in Objective C i.e I want www.gmail.com/index.php/aa/bb/cc .
Can someone help me out ?? I am new to objective C. Thanks.

If you are trying to use a URL for later use in retrieving data, etc., you're going to want an NSURL object when you're done. You can certainly create a URL by appending #"/%#" with the next element, but you should look at doing the following:
NSURL *originalURL = [NSURL URLWithString: #"http://www.gmail.com/"];
NSURL *aaNextURL = [originalURL URLByAppendingPathComponent: #"aa"];
NSURL *bbNextURL = [aaNextURL URLByAppendingPathComponent: #"bb"];
NSURL *ccNextURL = [bbNextURL URLByAppendingPathComponent: #"cc"];
This is a bit long-winded if you are adding static path elements, but if you are adding computed information it solves the problem of making sure there are appropriate slashes and allows you to use the URL construct directly.

Use stringByAppendingPathComponent

Related

Getting writeToFile:atomically: URL saved to

I'm saving some NSData with writeToFile:atomically: and then want to use that file very shortly afterwards. What is the easiest way to get the NSURL of the file I just saved?
[NSData writeToFile: atomically:] has a path parameter.
Just make the path parameter into a file URL and save that as an instance variable or property and you'll be all set.
Or even better, use [NSData writeToURL: atomically:] (i.e. convert your path into a URL to start with) and save that as a property or instance variable to be used later on.
You can convert a path into a file URL via [[NSURL alloc] initFileURLWithPath:].
And lastly, the real answer to the question you're trying to ask is: no, you can not divine the path or URL from an arbitrary "NSData" object. You'll have to save that information separately or alongside your data in order to keep track of where it came from.

Google maps in UIWebview shows driving directions not the map directly

I am loading google maps in UIWebview by providing the lat long co-ordinates for the Source and destination.But the problem is it shows the driving directions when it gets loaded.If we want to see the map then we have to click on the map button provided alongside the address.I need to directly show the map instead of driving directions when the web view gets loaded.
Can any one tell me how do I achieve this.
UIWebView *webView=[[UIWebView alloc]initWithFrame:webViewRect];
webView.delegate=self;
webView.scalesPageToFit=YES;
CLLocationCoordinate2D start = { 34.052222, -118.243611 };
CLLocationCoordinate2D destination = { 37.322778, -122.031944 };
//NSString *urlString=#"http://maps.google.co.in/";
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude, destination.latitude, destination.longitude];
NSLog(#"URL string-----> %#",googleMapsURLString);
NSURL *url=[NSURL URLWithString:googleMapsURLString];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.
Just change your URL line
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, destination.latitude, destination.longitude];
with the below code
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f&output=embed",start.latitude, start.longitude, destination.latitude, destination.longitude];
The only thing added is output=embed which forces the web to open the map directly without opening the screen which asks for filled input box for source and destination.
For more clarifications you can also check out all the parameters that are used in google map from Google Map Parameters
Above will solve your first query.
And regarding second query
i.e. Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.
Sorry no idea
NSString *ll = [NSString stringWithFormat:#"%f,%f",
self.placemark.location.coordinate.latitude,
self.placemark.location.coordinate.longitude];
ll = [ll stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [NSString stringWithFormat:#"http://maps.google.com/?q=%#", ll];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
In perusing the Google Map Parameters documentation, linked above, I came across the answer to the second part of this question.
Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.
The Directions portion of the parameter space appears to allow multiple destinations to be routed in sequence by appending "+to:" clauses to the daddr component of the query arguments.
For instance:
https://maps.google.com/maps?saddr=Ferry+Plaza+SF&daddr=Coit+Tower+to:Union+Square
shows a route beginning at the Ferry Plaza in San Francisco which then goes to Coit Tower, then Union Square, in that order.
Read about the 'daddr=' parameter in the above-linked Directions section of that wiki for further info.
In general, you can figure out what Google Maps URLs should look like through parameter inspection. E.g.:
Go to maps.google.com and query for a map with the routes, destinations, etc. that you are interested in.
When you have a map that looks like what you want, click the link icon (looks like a chain) to get a copy of the URL that corresponds to the parameters you've specified.
Paste this URL into the browser address bar.
Proceed to parse out parameters from the query string to determine which parts correspond to what on the map.
Cross-reference with pages like the mapki.com link above if you aren't sure what a parameter is doing.
You should be able to deduce formulation of new kinds of queries using this process.

NSURLConnection is not downloading files with spaces in the name

I am working on loading images into a gallery on the iPhone and running into an issue. It is apparent that something in the script isn't happy with spaces being in filename's when trying to download the images off of the internet.
This is the connection line.
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]] delegate:self];
And I pass an NSString in the NSURL. It works on all photos that don't have spaces.
Example of evil photos:
thumbs_WJ (16).jpg
thumbs_WJ (25).jpg
Now I know I could go back and update all the photos, update the database, and change the script so it doesn't add spaces anymore...but we are talking about thousands of photos.
And suggestions?
you need to do string:
str =[str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Use NSString's stringByAddingPercentEscapesUsingEncoding: method on the text you want to include as an argument.
From Apple docs:
stringByAddingPercentEscapesUsingEncoding:
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
If your string is a "normal" string, you can use NSUTF8StringEncoding as encoding. Otherwise, specify the encoding your string is in.
Just replace spaces with "%20" in your urlstring.

NSURL doesn't seem to want to accept my querystring

Hi there I have this code
NSLog(#"%#",URLRequestQueryString);
NSString *sendToServerString = [NSString stringWithFormat:#"http://mydomain.co.uk/req.php%#",URLRequestQueryString];
NSURL *sendToServer = [[NSURL alloc] initWithString:sendToServerString];
NSLog(#"%#",sendToServer);
NSLog(#"%#",sendToServerString);
URLRequestQueryString is just a standard querystring that I have built up throughout the script.
The first NSLog works fine and outputs a proper querystring (if I copy and paste it into a browser then the page will load and run correctly.
This is also the case when I output sendToServerString it correctly outputs the URL with querystring (which I can also copy and paste into a browser).
However sendToServer ouputs (null). If I remove the querystring it will correctly output the domain and path.
Any idea why this happens? How can I sort it?
Thank you.
NSURL *sendToServer = [NSURL URLWithString: [sendToServerString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
Could be what you are looking for. :)
You need see NSString reference.
A Section named "Working with URLs".
Under this section has two method
stringByAddingPercentEscapesUsingEncoding:
stringByReplacingPercentEscapesUsingEncoding:

get element value

I have a NSString like that? (in iPhone application)
NSString *xmlStr = "<?xml version=1.0 encoding=UTF-8>
<information>
<name>John</name>
<id>435346534</id>
<phone>045635456</phone>
<address>New York</address>
</information>"
How I can get elements value?
(Do i need convert to XML format and get elements value? or split string? any way please tell me?)
Thank you guys.
If you want to use split string, you can use tokenization of strings using "componentsSeparatedByString" method. This is a more cumbersome method of course, rather than the recommended XMLParser
To get the name.
NSArray *xmlStr_first_array = [xmlStr componentsSeparatedByString: #"<name>"];
NSString *xmlStr_split = [NSString stringWithFormat:#"%#",[xmlStr_first_array objectAtIndex:1]];
NSArray *xmlStr_second_array = [xmlStr_split componentsSeparatedByString: #"</name>"];
NSString *name = [NSString stringWithFormat:#"%#",[xmlStr_second_array objectAtIndex:0]];
The most obvious solution is to use an XML parser to retrieve the values from each element.
You could use the excellent TBXML for this task. It provides a really simple interface where it wouldn't take more than a few lines to retrieve the desired values. The drawback to using this small library is that it (as far as I know) loads the entire XML data into memory. In this particular case, however, that is not problem at all.
There's of course also the option of using the NSXMLParser, though this is an event-driven parser, and thus a bit less simple to use.
Your string is in xml format already and you need to parse it to retrieve data. There're several options available - for example you can use NSXMLParser class or libxml library.
Edit: XMLPerformance sample project shows how to use both approaches and compare their performance.