Soru & Cevap

AFNetworking geri dönüş değeri ...

29.03.2015 - 01:45

Merhaba arkadaşlar

ViewController içerisinde bir class'tan instance ve bu class içerisinde de AFNetworking ile webservis sorgusu yaptım.Ancak AFNetworking asynchronous işlem yaptığı için success bloğu içerisinde dönen değeri görüntüleyebiliyorum ama  ViewController içerisinde görüntüleyemiyorum.İşlemleri aşağıdaki gibi yaptım.Bu konu hakkında bilgisi olan var mı acaba?

ViewController.m

Service *tempService = [[Service alloc]init];

BOOL result=[tempService Login:txtUserName.text Control:txtPassword.text];   (result NULL geliyor)

 

Service.m

CustomResponse  *obj= [[CustomResponse alloc]init];

NSURLRequest *request1 = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request1];

operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)

obj= (NSDictionary *)responseObject;

 }

failure:^(AFHTTPRequestOperation *operation, NSError *error) {

     NSLog(@"Error");

 }];

[operation start];

4 Görüntülenme

3 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user coskungun
coskungun
29.03.2015 - 04:42

webserviceManager isminde bir classın olsun 

[code]

-(void)KurulumIDTuret:(id)view endMethod:(SEL)_endMethod dName:(NSString *)_domainName error:(SEL)_error

{

   

    NSString *soapAddress = [NSString stringWithFormat:@"%@?wsdl",WebAdres];

    NSURL *baseURL = [NSURL URLWithString:soapAddress];

    

    NSString *soapBody = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetKurulumId xmlns=\"http://tempuri.org/\"><domainName>%@</domainName></GetKurulumId></soap:Body></soap:Envelope>",_domainName];

    

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];

    

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];

 

    [request addValue:@"http://tempuri.org/GetKurulumId" forHTTPHeaderField:@"SOAPAction"];

    

    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        

        //NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        

        SEL mySelector = _endMethod;

        [view performSelector:mySelector withObject:responseObject];

        

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

 

        SEL myError = _error;

        [view performSelector:myError withObject:error];

 

        

    }];

    

    

    [operation start];

    

    

}

[/code]

bu kod içerisinde yer alsın  çağırmak istediğin view controllerdada bu şekilde cagir

[code]

webServisManager *wsManager = [[webServisManager alloc]init];

    [wsManager KurulumIDTuret:self endMethod:@selector(endMethodKurulumID:) dName:domainName error:@selector(errorKurulum:)];

[/code]

kodu bu şekilde yazdığında artık sorgunun bittiği zaman ve errore düştüğü zamanki metotları yakalamış oldun.

[code]

-(IBAction)endMethodKurulumID:(id)response

{

    JsonManager *jsManager = [[JsonManager alloc]init];

    NSMutableArray *arrDe = [jsManager GetKurulumId:response];

    kurulumID = [[arrDe objectAtIndex:0]KurulumID];

}

[/code]

[code]

-(IBAction)errorKurulum:(id)response

{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:myAppName message:appServiceError delegate:self cancelButtonTitle:@"Tamam" otherButtonTitles:nil, nil];

    [alert show];

}

[/code]

takıldığın yer olursa tekrar sorarsın. İyi çalışmalar.

 

 

 

Osman YILDIRIM
29.03.2015 - 06:13
çok teşekkürler şu linkte http://imnotyourson.com/afnetworking-with-block-callback/ block yöntemi diye bir şey buldum onu uyguladım ve istediğim sonucu aldım.Ama sizin dediğiniz yöntemi de uygulayacağım.
Coşkun
29.03.2015 - 07:05
orada afnetworkingin block ile kullanımı anlatılmış
Osman YILDIRIM
29.03.2015 - 07:35
evet block yöntemini denedim ama sizce hangisi daha sağlıklı olur?
picture-12235-1468440280.png
yildirimosman
29.03.2015 - 02:31

Öncelikle cevabınız için teşekkürler peki bu yazdığınız method geriye bir değer döndürüyo diyelim ve ViewController içerisinde bu methodu nasıl çağırmam gerekli acaba?

Ulaş Sancak
30.03.2015 - 03:32
İstediğiniz şey protocol'dür. Delegate de deniyor. Bu konuyu inceleyin.
Osman YILDIRIM
31.03.2015 - 08:12
delegate konusunu araştırdım ama sanırım olmadı yukardaki gibi farklı bir class tan geri dönüş değerini viewcontroller da görüntüleyebileceğim bir örnek varmı bildiğiniz
Profile picture for user coskungun
coskungun
29.03.2015 - 07:23

Selam Osman,

performselector kullanırsan block dışında sonucu yakalabilirsin 

Örneğin ; 

[code]

-(void)KurulumIDTuret:(id)view endMethod:(SEL)_endMethod dName:(NSString *)_domainName error:(SEL)_error
{
   
    NSString *soapAddress = [NSString stringWithFormat:@"%@?wsdl",WebAdres];
    NSURL *baseURL = [NSURL URLWithString:soapAddress];
    
    NSString *soapBody = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetKurulumId xmlns=\"http://tempuri.org/\"><domainName>%@</domainName></GetKurulumId></soap:Body></soap:Envelope>",_domainName];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseURL];
    
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];

    [request addValue:@"http://tempuri.org/GetKurulumId" forHTTPHeaderField:@"SOAPAction"];
    
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        SEL mySelector = _endMethod;
        [view performSelector:mySelector withObject:responseObject];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        SEL myError = _error;
        [view performSelector:myError withObject:error];

        
    }];
    
    
    [operation start];
    
    
}

[/code]