2016-05-16 4 views
2

私は応答を取得mのとき、それは私がNSObjectクラスにサーバー呼び出しで実装viewController.asに戻りましたNSObjectからサーバの応答を取得したいが、私はNSObjectメソッドに呼び出されますが、サーバーの応答の前に私のNSStringnullとして復帰しています。NSObjectクラスへのNSURLSessionConfigurationの設定方法は?

NSObject class : 

    @interface getServerCallClassMethod : 
    NSObject<NSURLSessionDelegate> 
{ 
     NSString *ResponceStr; 
} 
-(NSString *)getServerCall:(NSString *)mobleNo serverUrl:(NSString *)url; 

    @end 

一部を実装:

@implementation getServerCallClassMethod

-(NSString *)getServerCall:(NSString *)mobleNo serverUrl:(NSString *)url{  

NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration]; 
     NSURLSession *urlSession=[NSURLSession 
     sessionWithConfiguration:sessionConfig delegate:self 
     delegateQueue:[NSOperationQueue mainQueue]]; 
    NSURL *servrurl=[NSURL URLWithString:GetOTP]; 
    NSURLSessionDataTask *dataTask=[urlSession 
     dataTaskWithURL:servrurl completionHandler:^(NSData * _Nullable data, 
     NSURLResponse * _Nullable response, NSError * _Nullable 
     connectionError) 
     { if (data.length > 0 && connectionError == nil){ 
      [email protected]"success"; } else{ [email protected]"fail"; 
     } 
     }]; 
     [dataTask resume]; 
    }); 
      return ResponceStr; 
    } 
    In Vc : 
    getServerCallClassMethod *GetServerCallMethod=[[getServerCallClassMethod alloc]init]; 

    NSString *valide=[GetServerCallMethod getServerCall:@"800000000" serverUrl:@"http://www.gg.com"]; NSLog(@"valide"); 
+0

もし私が同じコードをすべてにオーバーライドしたくないので、NSObjectクラスから取得する方法を知りたいなら、サーバーの応答を取得したい場合はこれを試してみてください(リンク先はhttp://stackoverflow.com/a/37086586/4003548) – vaibhav

+0

NSObjectクラスのサーバーメソッドを呼び出すには –

答えて

1

を試してみて、この:インタフェースファイルで

実装ファイルで

@interface APISession : NSURLSession <NSURLSessionDownloadDelegate,NSURLSessionDelegate,NSURLSessionDataDelegate,NSURLSessionTaskDelegate> 

+(void)apiCallSharedSessionGet:(NSString *)strURL withCompletionHandlar:(void (^) (NSString *response, NSError *error, int status)) completionBlock; 

@implementation APISession 



+(void)apiCallSharedSessionGet:(NSString *)strURL withCompletionHandlar:(void (^) (NSString *response, NSError *error, int status)) completionBlock 
{ 


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]]; 
    [request setHTTPMethod:@"GET"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 

    __block NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
     if (error!=nil) 
     { 
      completionBlock(nil,error,0); 
      [task suspend]; 
     } 
     else 
     { 
      NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
      NSLog(@"requestReply: %@", requestReply); 
      completionBlock(requestReply,error,1); 
      [task suspend]; 
     } 

    }]; 
[task resume]; 

} 
+0

本当にありがとうございました。Piyush Patel –

+0

私は私の問題を解決しました –

+0

@Tamil_Arya wc ... :) –

0

を使用すると、データの受信を開始する前にNSURLSessionDataTaskからの応答を取得したい場合は、dataTaskWithURL: completionHandlerとの最初ではないNSURLSessionDataTaskを行います。 dataTaskWithURLを使用してデータタスクを初期化し、代わりに代理人を処理する必要があります。

NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *urlSession=[NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
NSURL *servrurl=[NSURL URLWithString:GetOTP]; 
NSURLSessionDataTask *dataTask=[urlSession dataTaskWithURL:servrurl]; 

デリゲートファイルにNSURLSessionDataDelegateを追加する必要があります。デリゲートの実装で

:データが一度に受信されませんので、あなたは、あなたの要求の完全なデータを受信するように正しく二デリゲートメソッドを実装する必要があり

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { 
    NSLog(@"Response: %@", response); 
    completionHandler(NSURLSessionResponseAllow); // Allow to continue this request 
} 

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { 
    NSLog(@"Receive data"); 
} 

注意。

詳細については、このドキュメントを参照してください:https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionDataDelegate_protocol/index.html#//apple_ref/occ/intfm/NSURLSessionDataDelegate/URLSession:dataTask:didReceiveDataを:

+0

Tien:どのような方法で正しく応答が得られるのかを実装しましたが、この成功応答をNSObjectクラスのviewControllerと共有する方法 –

関連する問題