how to fix xcode warning "Expression result unused" - iphone

I am doing a registration process with my app where the user enters in a number that I check against my db.. anyway long story short where I pass code into my NSString *startURL have a warning I cannot get rid of, it says
"Expression result unused"
have you ever experienced anything like this and if so how do I fix it?
-(void)startRegConnect:(NSString *)tempRegCode{
//tempRegCode = S.checkString;
NSLog(#"tempRegCode from RegConnection =%#",tempRegCode);
NSString *code = [[NSString alloc] initWithString:tempRegCode];
//urlstart string
NSString *startURL = (#"http://188.162.17.44:8777/ICService/?RegCode=%#",code); //warning here
NSURL *url = [NSURL URLWithString:startURL];
//create a request object with that url
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
//clear out the exisiting connection if there is on
if (connectionInProgress) {
[connectionInProgress cancel];
[connectionInProgress release];
}
//Instantiate the object to hold all incoming data
[cookieData release];
cookieData = [[NSMutableData alloc]init];
//create and initiate the connection - non-blocking
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

You can't just do (#"http://188.162.17.44:8777/ICService/?RegCode=%#",code). Use [NSString stringWithFormat:#"http://188.162.17.44:8777/ICService/?RegCode=%#", tempRegCode].

It's because of the parenthesis. By writing (blabla) it becomes an expression, which you are not using as an expressing, hence the compiler complains.
Change to [NSString stringWithFormat: ...]; and it becomes a method.

Related

How can call json url multiples times in iphone?

Hello I am developing one application as currency converter in that, I have URL it will return only one country currency but my module look like if user select one country then I need to display list of currency converter values of more than one country so I need call josn more than one times.
code is as:
responseData = [[NSMutableData data] retain];
ArrData = [NSMutableArray array];
NSString *strURL = [NSString stringWithFormat:#"http://rate-exchange.appspot.com/currency?from=%#&to=%#&q=1",strtablbase,strto];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
results = [responseString JSONValue];
livevalues=[responseString JSONValue];
With above code I am geting one country values but I need pass one strto values differently
Is it possible?
If yes, please give suggestions & help me out from this problem.
Yes you can use NSOperation Queues to call the different URL or if you are using Asihttprequest this Link may be useful for you :)
responseData = [[NSMutableData data] retain];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:#"country1"];
[array addObject:#"country2"];
for (NSString *urlString in array) {
strtablbase = [NSString stringWithFormat:#"%#",urlString];
NSString *strURL = [NSString stringWithFormat:#"http://rate-exchange.appspot.com/currency?from=%#&to=%#&q=1",strtablbase,strto];
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
Try this..
Of course it is possible to pass different values. Like you already did you can start connections one after another. If the server belongs to you, I would implement a request which returns me all the rates at a time. It saves the time for sending and receiving request. It really does not matter (for waiting time) if you get 100 bytes or 500 bytes in one request.
Otherwise you need to call many requests. Like said you can call one after another and even 2-3 requests at the same time. You can implement the mechanism your self or you can use NSOperationQueue which is made exactly for many requests by Apple.
For more information https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html
And I want yo point the method (of NSURLConnection)
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

Getting error in connection class while sending url having lots of parameters

Hello Friends i am getting a problem in connection class i am using the same connection class for my different methods .Every where its working fine but on making this particular connection i m getting error ie:.. Connection did fail with error.
I am posting my entire code please help me.
Code for sending my url and making connection:-
NSString *strURl=[NSString stringWithFormat:#"http://xxxyyy.com/API/index.php?action=listIt&login=%#&pwd=%#&name=%#&catID=%d&listID=&addr1=%#&addr2=%#&locName=&city=%#&state=%#&zip=&startDate=%#&startHours=%#&startMin=%#&startMeridiem=%#&endDate=%#&endHours=%#&endMin=%#&endMeridiem=%#&contactName=&contactPhone=&contactEmail=&contactWebsite=&cost=%d&description=%#",appDelegate.strUserName,appDelegate.strPassword,strTitle,self.subCat_ID,[txtAddress1.text urlEncodeUsingEncoding:NSUTF8StringEncoding],[txtAdress2.text urlEncodeUsingEncoding:NSUTF8StringEncoding],lblCity.text,lblState.text,strBeginDate,startHr,startMin,startMeridiem,strEndDate,strEndHr,strEndMin,strEndMeridiem,cost,[txtdDscription.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
//strURl=[strURl urlEncodeUsingEncoding:NSUTF8StringEncoding];
// NSString *strEncode=[strURl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// strEncode=[strEncode stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(#" strListItURl =%#",strURl);
ConnectionClass *connection=[[ConnectionClass alloc]init];
[connection getResponseData:strURl];
connection.delegate=self;
[connection release];
NSLog(#" strListItURl =%#",strURl); this is the url with method and parameter i am sending.
strListItURl http://xxxyyy.com/API/index.php?action=listIt&login=sawanth&pwd=sawanth&name=Hello One&catID=4&listID=&addr1=One%20tow&addr2=next%20one&locName=&city=Anchorage&state=Alaska&zip=&startDate=11/8/2012&startHours=11&startMin=33&startMeridiem=AM&endDate=11/10/2012&endHours=11&endMin=33&endMeridiem=AM&contactName=&contactPhone=&contactEmail=&contactWebsite=&cost=3&description=%20Hello
Note:-when i am pasting the "strListItURl" directly on browser i am getting success.
Now after making connection i am entering in connection class
-(void)getResponseData:(NSString*)strURL
{
NSLog(#"strURL =%#",strURL);
// NSString *str1 = [NSString stringWithFormat:#"%#",strURL];
NSURL *url=[NSURL URLWithString:strURL];
NSLog(#"url is =%#",strURL);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
//Initialize the connection for NSURL Connection
theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
if(theConnection)
{
//Initialize the data and retain the data
receiveData = [[NSMutableData data] retain];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Warning" message:#"Please check internet connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
here when i m printing urlRequest i am finding
Printing description of urlRequest: NSURLRequest (null)
And finaly controls goes to
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
please tell me where i am doing wrong:)
You havent alloc the NSURLRequest. But it is strange it is printing null instead of nil.
Try:
NSURLRequest *urlRequest = [[NSURLRequest alloc] requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
I got the answer.
Actually when i was passing the strURL
NSString *strURl=[NSString stringWithFormat:#"http://xxxyyy.com/API/index.php?action=listIt&login=%#&pwd=%#&name=%#&catID=%d&listID=&addr1=%#&addr2=%#&locName=&city=%#&state=%#&zip=&startDate=%#&startHours=%#&startMin=%#&startMeridiem=%#&endDate=%#&endHours=%#&endMin=%#&endMeridiem=%#&contactName=&contactPhone=&contactEmail=&contactWebsite=&cost=%d&description=%#",appDelegate.strUserName,appDelegate.strPassword,strTitle,self.subCat_ID,[txtAddress1.text urlEncodeUsingEncoding:NSUTF8StringEncoding],[txtAdress2.text urlEncodeUsingEncoding:NSUTF8StringEncoding],lblCity.text,lblState.text,strBeginDate,startHr,startMin,startMeridiem,strEndDate,strEndHr,strEndMin,strEndMeridiem,cost,[txtdDscription.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
its taking blank space from somewhere ,and due to this my connection just fail with error.
so i trimmed the blank spaces
strURl = [strURl stringByReplacingOccurrencesOfString:#" " withString:#""];
and its start working fine.

Creating a new NSURLConnection inside of connectionDidFinishLoading

I have a NSURLConnection that gets data from a JSON web service, and everything works fine. I'm using it to post something to the server and get a success response.
After that call I want to initiate another NSURLConnection to refresh the data, so I'm doing so inside the connectionDidFinishLoading method, however this second connection isn't calling connectionDidFinishLoading when it is done loading.
Can I not initiate a NSURLConnection from inside the connectionDidFinishLoading method?
EDIT: Below is the code. I subclassed NSURLConnection to include a Tag NSString, calling the new class NSURLConnectionHelper. I'm using this to differentiate which connection has called the connectionDidFinishLoading.
- (void)connectionDidFinishLoading:(NSURLConnectionHelper *)connection
{
if([connection.Tag isEqual:#"NewMessage"]){
NSString *jsonString = [[NSString alloc] initWithData:receivedNewMessageData encoding:NSASCIIStringEncoding];
NSDictionary *results = [jsonString JSONValue];
[jsonString release];
[connection release];
if ([[results objectForKey:#"MessageAdded"] isEqual:#"True"]) {
User *newUser = [[User alloc] init];
[newUser retrieveFromUserDefaults];
if([newUser IsLoggedIn]){
Message *message = (Message *)[messages objectAtIndex: 0];
NSString *urlAsString = // url for webservice goes here
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnectionHelper *connection1 = [[NSURLConnectionHelper alloc] initWithRequest:request delegate:self];
connection1.Tag = #"GetLatestMessages";
[request release];
if (connection1) {
receivedLatestMessagesData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
}
}
}else if([connection.Tag isEqual:#"GetLatestMessages"]){
//do some other stuff but this code is never reached
}
}
I'm not familiar with NSURLConnectionHelper but it looks like you're never starting the connection.
I ended up having a space in my web service url, once I corrected that it worked.

how to send a word/text to a php file from iphone application

I am trying to send a word/text from my iphone application to php page, any idea?
You might do it with an asynchronous call.
// Prepare the URL
NSString myWord = #"YOUR_WORD";
NSString *urlString = [NSString stringWithFormat:#"http://<YOUR_DOMAIN>/<YOUR_FILE>.php?word=%#", myWord];
NSURL *url = [NSURL URLWithString:urlString];
// Get the data from the URL
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
// If you want to get an answer from this call then send "self" as delegate
// (and implement few NSURLConnectionDelegate methods),
// otherwise send "nil".
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
You might also send the word synchronously:
// Prepare the URL
NSString myWord = #"YOUR_WORD";
NSString *urlString = [NSString stringWithFormat:#"http://<YOUR_DOMAIN>/<YOUR_FILE>.php?word=%#", myWord];
// Get the data from the URL
NSError *error;
NSData *aData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString] options:2 error:&error];
You would just have to load the URL with the word / text as a parameter using NSURLConnection. See the following for help in using NSURLConnection:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836
And, some sample code:
// The word
NSString wrd = [NSString stringWithString:#"hello"];
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yourdomain.com/thepage.php?word=#%", wrd]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}

UIBarButton ignores "setEnabled:YES"

I ran into a problem. I can't image why that happens, probably you guys can.
I've created a UIBarButtonItem in IB and linked it correctly. A set the property and synthesized it. At the beginning I set the btn disabled (viewWillAppear).
Then I try to add an entry to my mysql db:
NSString *URLStr = [NSString stringWithFormat:#"http://www.justfortestingandsoon.com/example.php?entry=%#", myEntry];
URLStr = [URLStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(#"URLStr: %#",URLStr);
NSURL *addURL = [NSURL URLWithString:URLStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:addURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
urlConnectionSet = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
In it's "didReceiveData..." I check which value gets returned:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSData *myData;
myData = data;
NSString *testString = [[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];
NSLog(#"receivedData: %#", testString);
if ([testString isEqualToString:#"ENABLED"]) {
self.notificationBtnDeactivate.enabled = YES;
}
...
I already debugged it. The line self.notificationBtnDeactivate.enabled = YES; gets called but without any noticable changes on the UI.
Any Ideas?
Thanks a lot!
I have two ideas for this:
the buttons are not correctly referenced from IB, add some NSAssert and check against nil
you doing your UI changes on the wrong thread. Try to use performSelectorOnMainThread:withObject:waitUntilDone: