Oneeb Oneeb - 2 years ago 75
Objective-C Question

Show a view after Response from Server in IOS

I am facing some issue in displaying a view in my view controller. I have made a custom view in a view controller which is shown when the response is 0 or getting correct response. Made two custom views . When the response comes 0 it shows view1 in which user click register button and user go to register page and when the response get value than it should display view2 in which it gets pin. Now i'm trying to display the view but i'm not getting it. The code i tried is this,

NSString *string1 = _Mobile.text;

NSLog(@"ID is %@",string1);

NSString *string2=@"&action=pin";


NSString *urll=@"MyURL";
NSString *string3 = [urll stringByAppendingString:string1];
NSString *final=[string3 stringByAppendingString:string2];

NSLog(@"URL %@",final);

_indicator.hidden=NO;
[_indicator startAnimating];
NSString *targetUrl = [NSString stringWithFormat:@"%@",final];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:targetUrl]];

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
[_indicator stopAnimating];
_indicator.hidden=YES;
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", myString);

if([myString intValue] == 0){
[self RegAlert];
}else if ([myString intValue]==response){
[self PinAlert];
}

}] resume];


The function i'm writing is this,

-(void)RegAlert{
_regView.hidden=NO;
[self.regView bounceIntoView:self.view direction:DCAnimationDirectionLeft
}

-(void)PinAlert{
_pinView.hidden=NO;
[self.pinView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}


My response coming from service is this when its 0 this


{"data":"0"}


When its right then it shows this pin


{"data":[{"pin":"5583"}]}

Answer Source

Add this code for json before dispatch_async block

 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
                                      options:NSJSONReadingMutableContainers 
                                        error:nil];
  NSString *value = json[@"data"];

Modify your ui update like this:

dispatch_async(dispatch_get_main_queue(), ^{

            if([value isEqualToString: @"0"]){
                [self RegAlert];
            }else {
                [self PinAlert];
            }            
        });

Note: UI update should be run on the main queue, hope it helps you

-(void)RegAlert{
    pinView.hidden=YES;
    _regView.hidden=NO;
    [self.regView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}

-(void)PinAlert{
    _regView.hidden=YES;
    _pinView.hidden=NO;
        [self.pinView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download