Oneeb Oneeb - 2 years ago 68
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 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];
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,

[self.regView bounceIntoView:self.view direction:DCAnimationDirectionLeft

[self.pinView bounceIntoView:self.view direction:DCAnimationDirectionLeft];

My response coming from service is this when its 0 this


When its right then it shows this pin


Answer Source

Add this code for json before dispatch_async block

 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
  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

    [self.regView bounceIntoView:self.view direction:DCAnimationDirectionLeft];

        [self.pinView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download