2016-12-26 76 views
1

進出口新的IOS我有我想將數據發送到服務器並接收同樣的方法響應如何將數據發送到服務器並接收同一methood響應

我有一個類名 profile.m疑問我想將數據發送到服務等級和接收型材類響應

Profile.m:

NSString *parameter = [NSString stringWithFormat:@"%@&access_token=%@&lksite=social&lkmenu=profile&lktype=view&displayedname=%@&displayid=%@", baseUrl, accessToken, userName,userId]; 

Service *service = [[Service alloc]init]; 
    [service sendDataToServer:@"POST" andUrl:parameter andUrl:baseUrl]; 

Service.m

-(void) sendDataToServer:(NSString *) method andUrl:(NSString *) getUrl andUrl:(NSString *)baseUrl{ 
NSMutableData *jsondata; 
Session *ses =[[Session alloc]init]; 
accessToken = [ses getAccessToken]; 
NSLog(@"Access Token---> %@",accessToken); 

NSString *baseUrl1 = [NSString baseUrl]; 


if([method isEqualToString:@"GET"]){ 
    NSString *urlStr = [NSString stringWithFormat: @"%@&access_token=%@",getUrl,accessToken]; 
    url = [NSURL URLWithString: urlStr]; 
    urlRequest= [NSMutableURLRequest requestWithURL:url]; 
    urlRequest.HTTPMethod=method; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
    if(connection) 
    { 
     urlMutable = [NSMutableData new]; 
    } 
}else{ 
    NSData *parameterData = [getUrl dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    url = [NSURL URLWithString: baseUrl1]; 
    urlRequest=[NSMutableURLRequest requestWithURL:url]; 
    urlRequest.HTTPBody=parameterData; 
    urlRequest.HTTPMethod=method; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
    if(connection) 
    { 
     urlMutable = [NSMutableData new]; 
    } 
}} 
+0

只需使用完成處理程序與塊,第一個爲成功,第二個爲失敗請求 –

回答

1

您可以使用NSLocalNotification來實現您的任務。 使用NSLocalNotification有三個簡單步驟。

1)爲此,您可以在Profile.m文件編寫代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

Service *Service = [[Service alloc] init]; 
     [Service sendDataToServer:(with your parameters here)]; 
} 

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(responceMethod:) name:@"WebServiceCall" object:nil]; 
} 

2)在Service.m類撥打服務電話

-(void) sendDataToServer:(NSString *) method andUrl:(NSString *) getUrl andUrl:(NSString *)baseUrl{ 
    NSMutableData *jsondata; 
    Session *ses =[[Session alloc]init]; 
    accessToken = [ses getAccessToken]; 
    NSLog(@"Access Token---> %@",accessToken); 

    NSString *baseUrl1 = [NSString baseUrl]; 


    if([method isEqualToString:@"GET"]){ 
     NSString *urlStr = [NSString stringWithFormat: @"%@&access_token=%@",getUrl,accessToken]; 
     url = [NSURL URLWithString: urlStr]; 
     urlRequest= [NSMutableURLRequest requestWithURL:url]; 
     urlRequest.HTTPMethod=method; 
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
     if(connection) 
     { 
      urlMutable = [NSMutableData new]; 
     } 
    }else{ 
     NSData *parameterData = [getUrl dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     url = [NSURL URLWithString: baseUrl1]; 
     urlRequest=[NSMutableURLRequest requestWithURL:url]; 
     urlRequest.HTTPBody=parameterData; 
     urlRequest.HTTPMethod=method; 
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
     if(connection) 
     { 
      urlMutable = [NSMutableData new]; 
     } 
    } 

} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
    int errorCode = (int)httpResponse.statusCode; 
    NSLog(@"response is %d", errorCode); 

    [urlMutable setLength: 0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [urlMutable appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSError *error; 
    id *response = [NSJSONSerialization JSONObjectWithData:webData options:NSJSONReadingAllowFragments error:&error]; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WebServiceCall" object:nil userInfo:response]; 
} 

最後
3)使用您在Profile.m文件中編寫的方法在此實現您的響應解析:

-(void)responceMethod:(NSNotification *)notif{ 
NSDictionary *dict = [notif userInfo]; 
// Your response parsing. 
} 
+0

它必須是評論,不要發佈爲答案。請閱讀相同的文檔。 – KAR

+0

@KAR我希望有人可以等待...... !!! –

+0

NSURLConnection? – Abizern

-1

聲明塊爲:

typedef void (^completionBlock)(id responseObject , NSError *error); 

,然後將此塊作爲添加到您的方法:

- (void) performRequestWithCompletion:(completionBlock)block; 

在調用此方法,將返回「responseObject」和「錯誤」。錯誤無成功

相關問題