Why NSMutableURLRequest gets defined different in iOS6 and iOS7 - iphone

I Have the following piece of Code that works perfectly fine in iOS6 but not in iOS7, the difference is how the request1 is constructed by each version, why it changes and what can be done to make it work on both:
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setHTTPMethod:#"POST"];
[request1 setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request1 setHTTPBody:[jsonInputString dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#" %#",request1);
And the log shows the following for iOS6 :
< NSMutableURLRequest http://XX.XX.XX.XX/ServiceFindMyFamily.svc/GPS/PosicionPorIDDispositivo/>
And for iOS7 ( it Fails ):
< NSMutableURLRequest: 0x16d8a020> { URL: http://XX.XX.XX.XX/ServiceFindMyFamily.svc/GPS/PosicionPorIDDispositivo/ }
Thanks for your kindly help

I don't think I follow.. do you wonder why the NSLog isn't equal?
Well ... they aren't implemented the same
the log merely calls the description method on it
the definition of the class did not change though

Related

cachedResponseForRequest method not being accessed

I am trying to set up a cache, however the method I am using 'as below' is not being accessed by the thread.
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
I am initializing the connection like this, and connectionDidFinishLoading is accessed so I am not sure what I am missing.
- (IBAction)searchRequest:(NSData *)postBodyData
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:#"https://127.0.0.1:88"];
NSURL *url = [NSURL URLWithString:databaseURL];
NSString *postLength = [NSString stringWithFormat:#"%d", [postBodyData length]];
//SynchronousRequest to grab the data, also setting up the cachePolicy
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; //if request dose not finish happen within 60 second timeout.
// NSInputStream *fileStream = [NSInputStream inputStreamWithData:postBodyData];
[request setHTTPMethod: #"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/octet-stream" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:postBodyData];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [NSMutableData data];
} else {
// Inform the user that the connection failed from the connection:didFailWithError method
}
}
any help would be appreciated.
connection:willCacheResponse: is only called in cases when the response will be cached. POST requests are not cacheable in most cases. (More details: Is it possible to cache POST methods in HTTP?)
You should probably look at something like MKNetworkKit which handles a lot of this kind of caching, particularly for REST protocols.
You can also look at Drop-in offline caching for UIWebView. You'd have to modify it significantly, but NSURLProtocol can be used to solve this kind of problem. AFCache is currently working to integrate this approach, and is another toolkit to consider. (Read through the comments in the blog post for more background on the issues.)

AFNetworking AFMultipartFormData how to set key

I want to upload (POST) image to server with setkey "FileData" like before (wrote with ASIHTTPRequest and it works)
[self.request setData:imageData withFileName:dateFormatted andContentType:#"image/jpeg" forKey:#"Filedata"];
with AFNetworking I set like below:
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:#"POST" path:#"" parameters:paramsDic constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:#"Filedata" fileName:dateFormatted mimeType:#"image/jpeg"];
}];
seems name not actually works...
How should I set the key? #mattt
Thanks
I figure it out, the problem is I add this line
[afRequest setValue:#"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:#"Content-Type"];
But I still don't know why, I add this line to other http request and it works, but file upload. I checked server, when do file upload with this line, server received many ... hm... come to think how to describe it, mass letters ...like below:
Content-Type: image/jpeg^M ^M
ÿØÿà^#^PJFIF^#^A^A^#^#^A^#^A^#^#ÿá^#XExif^#^#MM^#*^#^#^#^H^#^B^A^R^#^C^#^#^#^A^#^A^#^#<87>i^#^D^#^#^#^A^#^#^#
hi i am uploading audio/image file as below please check it and its working fine using the ASIHTTPRequest.
NSURL *audiourl = [NSURL URLWithString:#"Your Url"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl];
NSData *postData = [NSData dataWithContentsOfURL:SoundPath];
//SoundPath is your audio url path of NSDocumentDirectory.
[request addData:postData withFileName:#"mynewfile.png" andContentType:#"image/png" forKey:#"FileData"];
[request setDelegate:self];
[request startasynchronous];
Thanks

ASIHTTPRequest with UIWebView

I need to connect to a protected site and try to use ASIHTTPRequest
Here is my code:
url = [NSURL URLWithString:#"http://myurl/page.aspx"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:username];
[request setPassword:password];
[request setDomain:domain];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
[webView loadHTMLString:[request responseString] baseURL:[request url]];
}
When I use NSLog to see [request responseString], I get the correct HTML, but the result is a blank white webview.
From the outgoing request warnings that little snitch displays, I see the initial request and one going to an external resource.
My guess so far is that the inital request correctly uses the authentication from ASIHTTPRequest and fetches the page, but the uiwebview will try to load the included .js files and since uiwebview is not authenticating, it will not render the page at all ...
Anybody knows how to fix my problem?
Have you tried ASIWebPageRequest? My guess is you have resources in that page that are not downloaded, like http://myurl/image.jpg
ASIHttpRequest runs asynchronously. You need to put your webview loading code into the ASIHTTPRequest callbacks. (requestFinished).
Add a method to your class as follows:
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSError *error = [request error];
if (!error) {
[webView loadHTMLString:[request responseString] baseURL:[request url]];
}
}
There is also a requestFailed method that you can use to trap additional errors, you should implement this as well. One or the other of these methods will be called once ASIHttpRequest completes.
Note you will probably also need to set the delegate on the request before making the asynch call. (so same place you set the auth stuff).
request.delegate = self;

Do a GET request with parameters on iPhone SDK?

I'm doing an app which retrieves user info, and it does that using a GET request, but I don't know how could I make one. Already tried ASIHTTPRequest with this code:
NSURL *url = [NSURL URLWithString:#"http://forrst.com/api/v2/users/info"];
// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:#"1" forKey:#"id"];
[request setRequestMethod:#"GET"];
// Initiate the WebService request
[request setDelegate:self];
[request startAsynchronous];
But I get this response from forrst:
2011-06-05 16:59:32.189 Forrsty[4335:207] {"resp":{"error":"you must pass either id or username"},"stat":"fail","in":0.0557,"authed":false,"authed_as":false,"env":"prod"}
Which I understand I'm not doing the GET request ok, so how I would do it? Thanks :)
A Get Parameter ?
So why don't you try "http://forrst.com/api/v2/users/info?id=1" ?
[ NSString stringWithFormat:#"http://forrst.com/api/v2/users/info?id=%d", 1 ];
By the way, take a look at this librayry : http://allseeing-i.com/ASIHTTPRequest/
Good luck !

Can we get Preapproval key by sending request to PayPal server from iPhone? (For testing)

I am just trying to fetch the Preapproval Key from PayPal server right from my iPhone App instead of setting up a Separate server for that. (For Testing Purpose). Can this be achieved?
I have used the following code to do that:
NSString *url = #"requestEnvelope.errorLanguage=en_US&cancelUrl=http://www.bytelyte.com/PayPal_X_NVP_tester.php?cmd=test&currencyCode=USD&endingDate=27.05.11 &maxAmountPerPayment=5&maxNumberOfPayments=2&maxTotalAmountOfAllPayments=5&pinType=NOT_REQUIRED&returnUrl=http://www.bytelyte.com/PayPal_X_NVP_tester.php?cmd=test&startingDate=27.01.11&senderEmail=krish_1297240918_per#gmail.com//www.bytelyte.com/PayPal_X_NVP_tester.php?cmd=test&startingDate=27.01.11&senderEmail=krish_1297240918_per#gmail.com";
NSData *postData = [url dataUsingEncoding:NSASCIIStringEncodinallowLossyConversion:YES];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setTimeoutInterval:1.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"XXYYZZZZ" forHTTPHeaderField:#"X-PAYPAL-SECURITY-USERID"];
[request setValue:#"XXYYZZZZ" forHTTPHeaderField:#"X-PAYPAL-SECURITY-PASSWORD"];
[request setValue:#"XXYYZZZZ" forHTTPHeaderField:#"X-PAYPAL-SECURITY-SIGNATURE"];
[request setValue:#"NV" forHTTPHeaderField:#"X-PAYPAL-REQUEST-DATA-FORMAT"];
[request setValue:#"NV" forHTTPHeaderField:#"X-PAYPAL-RESPONSE-DATA-FORMAT"];
[request setValue:#"127.0.0.1" forHTTPHeaderField:#"X-PAYPAL-DEVICE-IPADDRESS"];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"utf-8" forHTTPHeaderField:#"charset"];
The Response I have got is the following:
responseEnvelope.timestamp=2011-02-09T05%3A02%3A38.859-08%3A00&responseEnvelope.ack=Failure&responseEnvelope.correlationId=981f044262212&responseEnvelope.build=1655692&error(0).errorId=560029&error(0).domain=PLATFORM&error(0).severity=Error&error(0).category=Application&error(0).message=The+required+X-PAYPAL-APPLICATION-ID+header+is+missing+from+the+HTTP+request&error(0).parameter(0)=X-PAYPAL-APPLICATION-ID
Am I missing something or this is entirely not possible?
#Krishnan, seems you are missing a HTTP header called X-PAYPAL-APPLICATION-ID.