WebCallsというオブジェクトクラスが設定されています。このクラスでは、Web呼び出しを行い、HTTPSサーバーからJSONを返します。今、メソッドは完全に動作し、私はテストし、データは正常に返されます。しかし、私の問題は、私はクラスの外に返されたデータにアクセスすることができないということです。私はjsonDataと呼ばれる文字列を持つインターフェイスクラスのインターフェイスHTTPSデータが保存されませんでした
@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
下のデータを取得するための
コードがあります。私は取得し、プロパティと合成を使用して設定します。私は、Web呼び出しを行った後だから、私はjsonDataにデータを割り当てる、と私は、ウェブコールクラスをインポートGETINFOメソッドを使用することができるはず、jsonDataが戻ってきた後、
WebCall *wc = [[WebCall alloc] init];
[wc getData:url];
NSLog(@"%@", [c jsonData]);
しかし、これだけでプリント使用してアクセスアウトヌル。それでも、データを受け取った後にWebcallクラスのStringを出力すると、問題なく印刷されます。誰かが私が間違っていることを教えてもらえますか?事前に
おかげ
編集:私は法の外の文字列にアクセスすることはできません。また完全な実装で
を更新しました。コードを別のクラスにコピーして、JSON Stringを割り当ててから本体で再度呼び出すと、nullが再び出てきます。私はその接続方法でしか印刷することができないようです。そして、それは文字列に
編集をクリアするようだ:私が試した何
[wc setWebCallDidFinish:^(NSString * json, NSString *test){
NSLog(@"%@", json);
}];
[wc getData:@"12345"];
WebCall –
の完全な実装方法を教えてください。なぜこのようにしているのですか?これは本当にイライラしていて、文字通りなぜこれをやっているのか分からない – AdamM
cの第2のコードセットでは何ですか? –