xcode 如何为 NSURL 设置 NSURLConnection 委托

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6915928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:38:23  来源:igfitidea点击:

How to set NSURLConnection Delegate for NSURL

iphoneobjective-cxcode

提问by Baub

My iPhone app sends a message to a server via HTTP post. I would like to make use of the NSURLConnection Delegate for several reasons.

我的 iPhone 应用程序通过 HTTP post 向服务器发送消息。我想使用 NSURLConnection 委托有几个原因。

Here is the code. What should I do to make it use a delegate?

这是代码。我该怎么做才能使用委托?

//.h
#import <Foundation/Foundation.h>

@interface ServerConnection : NSObject <NSURLConnectionDelegate>

-(NSData*) postData: (NSString*) strData;

//delegate method
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

@end

and the .m (the important part is the second method)

和 .m (重要的部分是第二种方法)

//.m

#import "ServerConnection.h"

@implementation ServerConnection

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    return self;
}

-(NSData*) postData: (NSString*) strData //it's gotta know what to post, nawmean?
{

    //postString is the STRING TO BE POSTED
    NSString *postString;

    //this is the string to send
    postString = @"data=";
    postString = [postString stringByAppendingString:strData]; 

    NSURL *url = [NSURL URLWithString:@"//URL GOES HERE"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];

    //setting prarameters of the POST connection
    [request setHTTPMethod:@"POST"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"en-US" forHTTPHeaderField:@"Content-Language"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    [request setTimeoutInterval:20.0];

    NSLog(@"%@",postString);

    NSURLResponse *response;
    NSError *error;

    //this sends the information away. everybody wave!
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    return urlData;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
//delegate
{
    NSLog(@"connectionDidFinishLoading!!!");
}

@end

Again, the question is what should I do to make it use a delegate? I need to know when I am done with the connection.

再次,问题是我应该怎么做才能使用委托?我需要知道何时完成连接。

As a side note, I'm using ARC so you don't worry about my memory management. ;)

附带说明一下,我使用的是 ARC,因此您不必担心我的内存管理。;)

回答by NWCoder

The method sendSynchronousRequest: doesn't use a delegate.

方法 sendSynchronousRequest: 不使用委托。

Take a look at connectionWithRequest:delegate:

看看 connectionWithRequest:delegate:

回答by kperryua

You're using the synchronous API, so when the method returns the connection has already finished. You only need the delegate if you're using the asynchronous API which you can use with any of +connectionWithRequest:delegate:, –initWithRequest:delegate:, or –initWithRequest:delegate:startImmediately:.

您使用的是同步 API,因此当该方法返回时,连接已经完成。只有当您正在使用异步API,你可以用任何的使用需要委托+connectionWithRequest:delegate:–initWithRequest:delegate:–initWithRequest:delegate:startImmediately: