返回的HTTPS数据未保存

时间:2012-05-04 16:18:48

标签: objective-c xcode

我有一个名为WebCalls的对象类。在这个类中,我进行Web调用并从HTTPS服务器返回一些JSON。现在方法工作得很好,我已经测试过,数据返回正常。不过我的问题是,我无法访问类外返回的数据。

检索数据的代码在

之下

接口

@interface WebCall : NSObject{

    NSString *phoneNumber;
    NSString *jsonData;
}
@property (nonatomic, retain) NSMutableData *responseData;
@property (nonatomic, retain) NSString *jsonData;


-(void) getData: (NSString *) link;


@end

Implementation

@implementation WebCall

@synthesize jsonData;
@synthesize responseData;


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  [responseData setLength:0];
}   

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    self.responseData = nil;
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

       NSString *s = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    jsonData = s;

}

-(void) getData: (NSString *) link{

        jsonData = [[NSString alloc] init];
self.responseData = [NSMutableData data];
    NSURL * url = [NSURL URLWithString:link];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"GET"];
    [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];

}



@end

在接口类中,我有一个名为jsonData的字符串。我使用属性和合成来设置它。因此,在我进行Web调用之后,我将数据分配给jsonData,我应该能够导入Web调用类,使用getInfo方法,返回jsonData,然后使用

访问它
WebCall *wc = [[WebCall alloc] init];
[wc getData:url];
NSLog(@"%@", [c jsonData]);

然而,这只是打印出null。然而,如果我在收到数据后打印出Webcall类中的String,它会打印出来。谁能告诉我我做错了什么?

提前致谢

编辑:已更新,完整实施

此外,我无法访问方法外的字符串。我将代码复制到另一个类,并尝试分配JSON字符串,然后在正文中再次调用它,它再次出现null。似乎我只能用那种连接方法打印出来。然后它似乎清除了字符串

编辑:我尝试了什么

[wc setWebCallDidFinish:^(NSString * json, NSString *test){

    NSLog(@"%@", json);


}];
[wc getData:@"12345"];

1 个答案:

答案 0 :(得分:1)

Adam jzeData是一个空字符串的原因是因为[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];异步运行,这意味着在新线程上并且它不会阻塞。这意味着当你调用[wc getData:url]时;然后立即调用NSLog(@“%@”,[wc jsonData]); http请求尚未完成,并且WebCall中尚未调用 - (void)connectionDidFinishLoading:(NSURLConnection *)连接委托函数。

有关详细说明,请阅读此iOs Concurrency Programming Guide。基本上,您需要在WebCall中添加一个通知程序,以便它可以通知生成请求已完成加载的对象。我会像这样使用一个块。


@interface WebCall : NSObject{

    NSString *phoneNumber;
    NSString *jsonData;
    void(^webCallDidFinish)(NSString *jsonData, id otherRandomVar);
}
@property (nonatomic, retain) NSMutableData *responseData;
@property (nonatomic, retain) NSString *jsonData;


-(void) getData: (NSString *) link;
-(void)setWebCallDidFinish:(void (^)(NSString *, id))wcdf;

@end

Implementation

@implementation WebCall

@synthesize jsonData;
@synthesize responseData;

-(void)setWebCallDidFinish:(void (^)(NSString *,id))wcdf{
    webCallDidFinish = [wcdf copy];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

       NSString *s = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    jsonData = s;
    webCallDidFinish(jsonData, @"any other object");

}
//all of your other code here

然后在调用代码中使用以下


WebCall *wc = [[WebCall alloc] init];
[wc setWebCallDidFinish:^(NSString * json, id randomSecondVar) {
    NSLog(@"%@",json);
}];
[wc getData:url];

在加载jsonData之后,将为setWebCallDidFinish提供的代码块将被调用。您还可以使用Delegate模式来完成此任务。请注意,在加载此异步请求时,您应该向用户提供某种指示符。