-2

I have tried every possible method to send data in post method using NSURLSession,AFNetworking,NSURLConnection;but every time my backend developer says they are not receiving any data,although my co android developer is successfully sending data in POST.I am totally helpless now.Please help.

---- Using NSURLConnection ----

NSString *post = [NSString stringWithFormat:@"user_id=%@&email=%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"UserId"],emailString];
NSLog(@"------%@-------",post);
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding ];//[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@app_user_service/app_invite_email",App_Domain_Url]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(conn) {
    NSLog(@"Connection Successful");
} else {
    NSLog(@"Connection could not be made");
}

// ---- Using AFNetworking ----

NSDictionary *dic2 = @{@"user_id": [[NSUserDefaults standardUserDefaults] valueForKey:@"UserId"] , @"email" : emailString};

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[manager POST:urlstring parameters:dic2 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"success!");



    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"Response----> %@",dict);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"error: %@", error);
}];
Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73

2 Answers2

1

In iOS9, Apple added new feature called App Transport Security(ATS).

ATS enforces best practices during network calls, including the use of HTTPS.

Add the following in you info.plist

enter image description here

You can add exceptions for specific domains in your Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>testdomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • add once in your plist and try once, if you not get the answer we go for further step – Anbu.Karthik Mar 09 '16 at 06:52
  • I have added that brother/sir,as I am being able to do in GET method,the issue is with POST method only :-( Though the methods I am applying for POST is already tested in all other projects and running there successfully. :/ – Koustov Basu Mar 09 '16 at 06:53
  • @KoustovBasu -- can you show your POST method with request URL and parameters , – Anbu.Karthik Mar 09 '16 at 06:54
  • Check if you are setting @"Content-Type" property properly. – Awesome.Apple Mar 09 '16 at 06:55
  • 2
    You should not recommend to allow all connections for the iOS app, instead, you could show how to whitelist the required hosts. Allowing all connections may break the security of the app. – Fahri Azimov Mar 09 '16 at 06:55
  • @KoustovBasu -- in this place `NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];`, use `NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding ]; – Anbu.Karthik Mar 09 '16 at 07:05
  • Applied this ---> [post dataUsingEncoding:NSUTF8StringEncoding ]; Still same response man. :-( @Abu.Karthik – Koustov Basu Mar 09 '16 at 07:09
  • @KoustovBasu Add all of this code to the question, and make it readable, please. – Fahri Azimov Mar 09 '16 at 07:14
  • Okay Anbu.Karthik & Fahri Azimov – Koustov Basu Mar 09 '16 at 07:18
  • @FahriAzimov I have tried AFNetworking too,attached the code,please check.I have tried every possible way that can be used for implementing POST method for ios. Things are not working,son't know why. – Koustov Basu Mar 09 '16 at 07:26
  • This will promote app reject from Apple, instead whitelist the website. Credit : (http://stackoverflow.com/a/30732693/6042879) – Lazar Kukolj Jul 29 '16 at 20:18
0

Please check if you are setting property like below

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
Awesome.Apple
  • 1,316
  • 1
  • 11
  • 24
  • Yes I have applied like that....The response I am getting every time from backend is the response they are sending when they are not getting data from my end. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; – Koustov Basu Mar 09 '16 at 06:57
  • I have applied @"Content-Type" property same as u have mentioned @Kiran – Koustov Basu Mar 09 '16 at 07:10
  • Ask android developer what the value he/she setting? – Awesome.Apple Mar 09 '16 at 07:11