I'm attempting to ping eBay's API using an HTTP request, however Xcode is giving me the following error
No known class method for selector stringWithFormat
What I want to do here is append whatever string is input in the searchField to the end of the url. What am I doing wrong? Appreciate any help!
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.nextButton) return;
if (self.searchField.text.length > 0) {
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL stringWithFormat:#"http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=***APP ID ****&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.12.0&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&sortOrder=PricePlusShippingLowest&paginationInput.entriesPerPage=3&itemFilter(2).paramName=Currency&itemFilter(2).paramValue=USD&itemFilter(3).name=ListingType&itemFilter(3).value=FixedPrice&keywords=%#", self.searchField.text]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
You first need to create a NSString containing the URL and then create an NSURL out of it.
NSString *urlString = [NSString stringWithFormat:#"http://example.com/",
self.searchField.text];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=***APP ID ****&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.12.0&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&sortOrder=PricePlusShippingLowest&paginationInput.entriesPerPage=3&itemFilter(2).paramName=Currency&itemFilter(2).paramValue=USD&itemFilter(3).name=ListingType&itemFilter(3).value=FixedPrice&keywords=%#", self.searchField.text]]];
You were missing a step, NSURL does not have a method called stringWithFormat.
Related
Im struggled with redirect to webpage after read NFC scan by objective C.
Here is my code
- (void) readerSession:(nonnull NFCNDEFReaderSession *)session didDetectNDEFs:(nonnull NSArray<NFCNDEFMessage *> *)messages {
for (NFCNDEFMessage *message in messages) {
for (NFCNDEFPayload *payload in message.records) {
NSLog(#"Payload data:%#",payload.payload);
}
}
}
I got solution by using VYNFC kit,
https://github.com/vinceyuan/VYNFCKit
if ([parsedPayload isKindOfClass:[VYNFCNDEFURIPayload class]]) {
text = #"[URI payload]\n";
text = [NSString stringWithFormat:#"%#%#", text, ((VYNFCNDEFURIPayload *)parsedPayload).URIString];
urlString = ((VYNFCNDEFURIPayload *)parsedPayload).URIString;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webview loadRequest:requestObj];
}
Actually I want to created application of shortening URL , i have used GoDady by creating account at http://app.x.co/ But My URL doesnot get shorten.
This is my key
#define kGoDaddyAccountKey #"b201137c009311e6984efa163ee12fa9"
This is actually The method that do work for Shortening URL
- (IBAction)shortenURL:(id)sender
{
NSString *urlToShorten = self.webView.request.URL.absoluteString;
NSString *urlString = [NSString stringWithFormat:#"http://api.x.co/Squeeze.svc/text/%#?url=%#",kGoDaddyAccountKey,
[urlToShorten stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
shortURLData = [NSMutableData new];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
shortenURLConnection = [NSURLConnection connectionWithRequest:request
delegate:self];
}
But I get error like This.
Quite Interesting, Please Help.
I am working on the google maps where I find the location based on the text entered in the
textfield.I have used NSConnection to find the location json and then I assign this json to
the property in connectionDidFinishLoading delegate method so that I can access the json
when it is required but unfortunately I am not getting the data in geocodeAddress
method(NSLog(#"geodata %#",geocode))
Can any one help me to fix this issue?
- (void)geocodeAddress:(NSString *)address withCallback:(SEL)callback withDelegate: (id)delegate
{
NSString *geocodingBaseUrl = #"http://maps.googleapis.com/maps/api/geocode/json?";
NSString *url = [NSString stringWithFormat:#"%#address=%#&sensor=false", geocodingBaseUrl,address];
NSLog(#"url=%#",url);
url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *queryUrl = [NSURL URLWithString:url];
NSURLRequest *request =[NSURLRequest requestWithURL:queryUrl cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
//NSURLRequest *request =[NSURLRequest requestWithURL:url];
downloaddata= [[NSMutableData alloc]init];
NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(#"geodata %#",geocode);
//[delegate performSelector:callback];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
alldata = [NSJSONSerialization
JSONObjectWithData:self->downloaddata
options:NSJSONReadingMutableContainers
error:&error];
if(error)
{
}
else
{
self.geocode =alldata;
//NSLog(#"geodata %#",geocode);
}
}
NSURLConnection's are normally asynchronous, so geocode isn't set until the connectionDidFinishLoading is called, which won't happen until sometime after geocodeAddress... has finished. In your connectionDidFinishLoading... you'll need to execute code to cause the UI to refresh with the now valid geocode data.
Alternatively you could use + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error to execute the request synchronously, but that would be bad since it would block the UI until the request completed.
I am trying to send a simple NSURLConnection request:
- (void) sendHTTPRequest:(NSString*)urlString
{
NSLog(#"SendHTTPRequest: %#", urlString);
#try
{
NSURL *fileURL = [NSURL fileURLWithPath:urlString];
// Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
receivedData = [NSMutableData data];
}
else {
// Inform the user that the connection failed.
}
}
#catch (NSException *e)
{
NSLog(#"Exception: %#", e);
}
}
It calls back to:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
with an error of: Connection failed! Error - The requested URL was not found on this server.
However, this url does work. I can access it with my browser with no issues.
What am I missing?
According to the NSURL Class Reference, fileURLWithPath:path is only used for valid system paths. For "Internet"-URLs, you are supposed to use [NSURL urlWithString:urlString];
today, I encountered a problem with NSURLConnection. I want to download the contents of the URL http://api.wunderground.com/api/fs3a45dsa345/geolookup/q/34.532900,-122.345.json. If I simply paste the URL into Safari, I get the correct response. However, if I do the same thing with NSURLConnection, I get a "not found" response. Here's the code I'm using:
NSURL *requestURL = [[NSURL alloc] initWithString:#"same url as above"];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:requestURL];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self
startImmediately:YES];
What's the problem here?
Make sure you're escaping any special characters in the URL string by sending it a stringByAddingPercentEscapesUsingEncoding: message, for example:
NSString *s = [#"some url string" stringByAddingPercentEscapesUsingEncoding:NSUTFStringEncoding];
NSURL *requestURL = [NSURL URLWithString:s];
EDIT
It turns out the web service request is failing because the User-Agent header doesn't get set by default. To set it, use an instance of NSMutableURLRequest rather than NSURLRequest to create the request, as shown below:
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myURL];
[myRequest setValue:#"My App" forHTTPHeaderField:#"User-Agent"];
Where is the delegate code? For async NSURLConnection there needs to be a delegate method to receive the returned data. Other options include sendSynchronousRequest: or if it must be async wrapping sendSynchronousRequest in a GCD block.