简体   繁体   中英

How to do post method in AFNetworking in ios?

This is my code. Now afnetworking totally changed to nsurlsession and first time i am using afnetworking .

-(void)responseRunning:(NSString *)url :(BOOL) method :(NSData *)data
{
    if(method)
    {
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

        NSURL *URL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];

        NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error)
            {
//                NSLog(@"Error: this is error message %@", error);
            }
            else
            {
                NSLog(@"this is response objectv %@%@", response,responseObject);
                [self responseCompleted:responseObject];
            }
        }];

        [dataTask resume];
    }
    else
    {
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

        NSURL *URL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];

        NSURLSessionDataTask *dataTask = [manager uploadTaskWithRequest:request fromData:data progress:nil

         completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
             if (error)
             {
                 NSLog(@"Error: this is error message %@", error);
             }
             else
             {
                 NSLog(@"this is response objectv %@%@", response,responseObject);
                 [self responseCompleted:responseObject];
             }
         }];
        [dataTask resume];
    }
}

first one is response method and thats working fine ,but while do post method i am struggling .

This is my code please help to find out the solution ...

output:


  Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=http:/modules/api/forgetpassword/, NSErrorFailingURLStringKey=http:/modules/api/forgetpassword/, NSLocalizedDescription=cancelled}

since iOS9 initially you need to populate your info.plist file by the following values:

the query schemes

which schemes you intent to open in your app, you can extend to this list with your custom URL schemes, if necessary:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>http</string>
    <string>https</string>
</array>

the transport security

which defines excuses for the app to receive response from any type of back-end:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

NOTE: I personally would not recommend to bypass the official app transport security protocols for you and for your end-users safety, but obviously if you have no control of the back-end, you might not have too many other choices. if you can control the back-end I highly recommend to read this documentation from Apple and make your server side secure enough for iOS9+.

Creating an Upload Task

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
    NSLog(@"Error: %@", error);
} else {
  //enter code here 
    NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];

Useful Links

https://www1.in.tum.de/lehrstuhl_1/home/98-teaching/tutorials/540-ios14intro-client-server-communication

https://github.com/AFNetworking/AFNetworking

Note: In Xcode 7

确保这些已添加到Plist中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM