如何在ios中的AFNetworking中进行post方法?

时间:2015-12-17 09:22:18

标签: ios objective-c afnetworking

这是我的代码。现在afnetworking完全更改为nsurlsession,并且我第一次使用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];
    }
}

第一个是response方法,而且工作正常,但是在post方法中我很难挣扎。

这是我的代码,请帮助找出解决方案......

output:


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

2 个答案:

答案 0 :(得分:0)

因为iOS9最初需要使用以下值填充info.plist文件:

查询方案

您打算在应用中打开哪些方案,如有必要,您可以使用自定义网址方案扩展到此列表:

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

传输安全性

定义了应用程序从任何类型的后端接收响应的借口:

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

注意:我个人不会建议为您和最终用户安全绕过官方应用传输安全协议,但显然如果您无法控制后端,你可能没有太多其他选择。如果您可以控制后端,我强烈推荐read this documentation from Apple并使您的服务器端足够安全,适用于iOS9 +。

答案 1 :(得分:0)

创建上传任务

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];

有用的链接

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

https://github.com/AFNetworking/AFNetworking

注意:在Xcode 7中

Make sure that these added in Plist