I am passing url with some string value and NSInteger value in this url but when I put breakpoint on this and I tab on trace then it show me this message exc-bad-access in url I given bold please see that 'bold' I want to pass there value:
[ NSInteger day,NSInteger day1,NSString *fromDate1, NSString *fromDate1,NSString *OriginCode,NSString *DestinCode].
I get all value on url when I put the breakpoint but when I step into breakpoint my app crashes, why it crash? Help me. Where am I wrong?
-(void)sendRequest
{
stringWithFormat:#"http://www.google.com?AvailabilitySearchInputFRSearchView%24ButtonSubmit=Search%20For%20Flights%20&AvailabilitySeast=",day,day1,DestinCode,"2011-09","2011-09",OriginCode];
NSString *urlString = [NSString stringWithFormat:#"http://www.bookairways tickt.com/Sales/FRSearch.aspx?AvailabilitySearchInputFRSearchView%24ButtonSubmit=Search%20For%20Flights%20&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay1=**%i**&AvailabilitySearchInputFRSearchView%24DropDownListMarketDay2=**%i**&AvailabilitySearchInputFRSearchView%24DropDownListMarketDestination1=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth1=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListMarketMonth2=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListMarketOrigin1=**%#**&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_ADT=1&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_CHD=0&AvailabilitySearchInputFRSearchView%24DropDownListPassengerType_INFANT=0&AvailabilitySearchInputFRSearchView%24RadioButtonFlowSelector=FlightAndCar&AvailabilitySearchInputFRSearchView%24RadioButtonMarketStructure=RoundTrip&AvailabilitySearchInputFRSearchView%24discountPax=0&__EVENTARGUMENT=&__EVENTTARGET=&__VIEWSTATE=%2FwEPDwUBMGRkg4UKvNNb1NbM14%2F2n9zUxhNQ%2B%2BA%3D&errorlist=",day,day1,DestinCode,fromDate1,fromDate2,OriginCode];
//urlString=[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"************url:%#",url);
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(#"%#",webData);
} else {
}
}
make your url properly like this:-
NSURL *url = [NSURL URLWithString:[*yourstring* stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Harish this will help u to in creating the url check this our http://wiki.akosma.com/IPhone_URL_Schemes
like this
NSString *template = #"appigotodo://com.example.xyzapp/import?name=%#¬e=%#&due-date=%#&priority=%#&repeat=%#";
NSString *name = #"Buy%20some%20milk";
NSString *note = #"Stop%20on%20the%20way%20home%20from%20work.";
NSString *dueDate = #"2009-07-16";
NSString *priority = #"1";
NSString *repeat = #"101";
NSString *stringURL = [NSString stringWithFormat:template, name, note, dueDate, priority, repeat];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
Two things:
The URL has many % symbols that are not being used as placeholders. The % symbols that are not between '**' in your code need to be escaped like so: %%. In other words, SearchInputFRSearchView%24Button should be SearchInputFRSearchView%%24Button.
You are using %i to put integers into your string. You should be using %d instead.
Related
I need to know the difference between the following two methods of url connection?
What is the significance of these two methods?
Which method is preferred in what circumstances?
Method 1:
file.h
#import
#define kPostURL #"http://localhost/php/register.php"
#define kemail #"email"
#define kpassword #"password"
#interface signup : UIViewController
{
...
...
NSURLConnection *postConnection;
}
#property ...
...
#end
file.m
NSMutableDictionary *input=[[NSMutableDictionary alloc]init];
...
...
...
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString: [NSString stringWithFormat:#"?%#=%#", kemail, [input objectForKey:#"email"] ]];
[postString appendString: [NSString stringWithFormat:#"&%#=%#", kpassword, [input objectForKey:#"password"] ]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:#"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSLog(#"postconnection: %#", postConnection);
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: postString ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSLog(#"serverOutput = %#", serverOutput);
Method 2:
NSString *post =[NSString stringWithFormat:#"email=%#&password=%#",[input objectForKey:#"email"], [input objectForKey:#"password"]];
NSString *hostStr = #"http://localhost/frissbee_peeyush/php/login.php?";
hostStr = [hostStr stringByAppendingString:post];
NSLog(#"HostStr %#",hostStr);
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
NSLog(#"serverOutput %#", serverOutput);
What if i need to connect to url only using header information ?
Problem: for login and signup, these methods are perfectly fine but whenever i want to insert any string containing special characters such as #,/,_ etc, it is unable to perform any operation.
Guide me plz.
Even the method 1 you have implemented is incomplete as there are many delegates of NSUrlConnection which should be implemented to get the data , handling errors , proxy , authentication etc . It is more advisable to use the first method but not in the way you used.NSUrlConnection in asynchronous in behaviour so You Don't have to wait until the url is loaded.
NSUrlConnection Class Reference
The Second method simply hits the url as sson as it encounters the NSUrl parameter in the NSData . Moreover , you don't have much control over your web service interaction .
NSUrl Class Reference
To get the implementation of NSUrlConnection you can go through the
NSUrlConnection Tutorial
Url With Special Characters:-
[NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
i m calling default map app. from my application but for example when i select Portugal language on my iphone for current location NSURL return nil.
I already used UTF8encoding and percentage encapsulate as follows:-
NSString *encodedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
//where urlString has value http://maps.google.com/maps?saddr=Localiza\u00e7\u00e3o+actual&daddr=28.6522907,77.1929857
NSURL *URL = [NSURL URLWithString:encodedURLString];
[[UIApplication sharedApplication] openURL:URL];
i already try NSUTF8Encoding instead NSASCIIStringEncoding but nothing helped.
thanx for any help.
You are percent-encoding the entire URL.
This turns http://www.google.com into http%3A//www.google.com, which is a malformed URL.
From the NSURL Class Reference:
Return Value: An NSURL object initialized with URLString. If the string was malformed, returns nil.
Ergo, you are receiving nil.
What you want to do is this:
NSString *path = #"http://maps.google.com/maps";
NSString *query = #"saddr=Localiza\u00e7\u00e3o+actual&daddr=28.6522907,77.1929857";
query = [query stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString *url = [NSString stringWithFormat: #"%#?%#", path, query];
NSURL *URL = [NSURL URLWithString: url];
[[UIApplication sharedApplication] openURL: URL];
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude];
NSLog(#"urlString: %#", urlString);
NSURL *urlFromURLString = [NSURL URLWithString:urlString];
My log is : http://maps.google.com/maps/geo?q=53.872874,27.527790&output=csv&sensor=false&key=swizzlec hops
I can copy this url and paste to the browser and its ok, but urlFromURLString = nil. But, why?
I believe you need to use
stringByAddingPercentEscapesUsingEncoding:
on urlString before passing it to NSURL.
NSString *urlString = [[NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude] stringByAddingPercentEscapesUsingEncoding::NSUTF8StringEncoding];
Try
NSString *urlString = [[NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
I don't think you can have spaces in your URL
This is a better approach:
NSString* escapedUrl = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlFromURLString = [NSURL URLWithString:escapedUrl];
I'm doing an HTTP Post in my iphone app and one of the parameters I send to the server is a URL. The problem is that when I convert from an NSURL to an NSURLRequest, the string http://www.slashdot.org becomes http:/www.slashdot.org (one of the forward slashes is missing)
is there a way around this?
here is the code I'm using:
NSString *host = #"example.host.com";
NSString *urlString = [NSString stringWithFormat:#"/SetLeaderUrl.json?leader_email=%#&url=%#",localEmail,urlToPublish];
NSURL *url = [[NSURL alloc] initWithScheme:#"http" host:host path:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *jsonString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
I've used NSLog to see where it loses the '/' and it's on the fourth line:
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
thanks for taking the time to read!
You're not percent-escaping the query values before substituting them in to the string. I just did a little test, and found that if I set urlToPublish to "http://example.com", then NSURL would transform it into "http:/example.com".
This is because the query value contains special characters, which means you need to add percent escapes. At the very least you can use the mediocre -[NSString stringByAddingPercentEscapesUsingEncoding:] with the NSASCIIStringEncoding. Far better would be to use a different (and more complete) escaping mechanism, such as the one I suggest in this post.
In this case, stringByAddingPercentEscapesUsingEncoding: does not work, because it's a pretty lousy method. It works on an inclusive model, which means you have to tell it which characters you want percent encoded. (Under the hood, it's just calling CFURLCreateStringByAddingPercentEscapes()) This function basically asks you for a string that represents every character it's allowed to percent-encode (as I understand the function). What you really want is an exclusive model: escape everything except [this small set of characters]. The function I linked to above does that, and you'd use it like this:
NSString *urlToPublish = [#"http://stackoverflow.com" URLEscapedString_ch];
NSString *host = #"example.host.com";
NSString *urlString = [NSString stringWithFormat:#"/SetLeaderUrl.json?leader_email=%#&url=%#",localEmail,urlToPublish];
NSURL *url = [[NSURL alloc] initWithScheme:#"http" host:host path:urlString];
And then it will build your URL properly.
Here's another way you could do this (and do it correctly). Go to my github page and download "DDURLBuilder.h" and "DDURLBuilder.m", and then build your URL like this:
NSString *localEmail = #"foo#example.com";
NSString *urlToPublish = #"http://stackoverflow.com"
DDURLBuilder *b = [DDURLBuilder URLBuilderWithURL:nil];
[b setScheme:#"http"];
[b setHost:#"example.host.com"];
[b setPath:#"SetLeaderUrl.json"];
[b addQueryValue:localEmail forKey:#"leader_email"];
[b addQueryValue:urlToPublish forKey:#"url"];
NSURL *url = [b URL];
Here is some code apple use to get a NSURLRequest,
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
#Dave DeLong: I notice in the Apple "URL Loading System Program Guide" the example creating a connection and request does not use any escaping. The Url it uses is from a NSURL URLWithString:
I fixed it, this is what I had to do:
NSString *urlString = [NSString stringWithFormat:#"/SetLeaderUrl.json?leader_email=%#&url=%#",localEmail,urlToPublish];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
urlString = [NSString stringWithFormat:#"%#%#%#",scheme,host,urlString];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
I have made a project and when people put a keyword into UITextfield and click the button then my app will retrieve the data from website database.
It is not working here is the code:
- (IBAction) btnClickMe_Clicked:(id)sender {
NSString *kw = s.text; // <-----THIS IS THE UITextField
NSURL *url = [NSURL URLWithString:#"http://www.example.com/index.php?keyword=",kw];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[iMessageLabel loadRequest:request];
}
Any one could help me?
You aren't putting the keyword into the string. You need something like this:
NSString *kw = s.text;
NSString *encodedkw = [ky stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://www.example.com/index.php?keyword=%#", encodedkw];
NSURL *url = [NSURL URLWithString:urlString];
Then look at how to use NSURLConnection to figure out how to put the data into the label.