0

Hi I am new for Ios and in my app I want to run my Web services with BackGround Thread and I want to run this web Service even when app at Background State.

How can I do this Scenario? Can some one help me please?

My code is below.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self getServieCalling:@"MY URL"];
}

-(void)getServieCalling :(NSString*)mainurl{


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:mainurl,kServerBaseURL1]]

                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy

                                                       timeoutInterval:60.0];

    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        if (error != nil) {

            NSString * BasicnetworkError = [error localizedDescription];
            NSString * AppendString = @"Http Response failed with the following ";
            NSString * networkError = [AppendString stringByAppendingString:BasicnetworkError];

            [self BasicError:networkError];

            return;
        }

        else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
            if (statusCode != 200) {

                NSString *statusCodeError = [NSString stringWithFormat: @"Http Response failed with the following code %ld", (long)statusCode];



            }else{

                NSError *parseError;

                id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

                NSLog(@"response object %@",responseObject);


                else {

                    NSLog(@"Error parsing JSON: %@", parseError);

                    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

                    NSLog(@"responseString = %@", responseString);
                }
            }
        }

    }];

    [task resume];

   });
}
Elydasian
  • 2,016
  • 5
  • 23
  • 41
Krish
  • 4,166
  • 11
  • 58
  • 110
  • 2
    Have a look at this http://stackoverflow.com/questions/15268702/implementing-long-running-tasks-in-background-ios – Vishal Sonawane Jan 05 '17 at 06:25
  • i my code i wrote clearly how to run web services in using background thread but my question how to run this operation when app even at background state? – Krish Jan 05 '17 at 06:28
  • 1
    That answer deals with background mode dude. Have you read it properly. It about running your code in background state . – Vishal Sonawane Jan 05 '17 at 06:30
  • NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init]; [operationQueue addOperationWithBlock:^{ // Perform long-running tasks without blocking main thread }]; – Krish Jan 05 '17 at 06:32
  • This will also help you http://stackoverflow.com/questions/8167956/how-can-i-call-method-while-app-is-in-background-in-ios – Pushkraj Lanjekar Jan 05 '17 at 06:32
  • they provide above block for running app using Background thread – Krish Jan 05 '17 at 06:32
  • is this block perform even app at background state Vishal? – Krish Jan 05 '17 at 06:33
  • Have't you noticed this part of answer???? - (void)applicationWillResignActive:(UIApplication *)application { bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ // Wait until the pending operations finish [operationQueue waitUntilAllOperationsAreFinished]; [application endBackgroundTask: bgTask]; bgTask = UIBackgroundTaskInvalid; }]; } – Vishal Sonawane Jan 05 '17 at 06:34
  • so what is this bgTask? i am not understand – Krish Jan 05 '17 at 06:37
  • bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ }]; – Krish Jan 05 '17 at 06:39
  • can you explain me this clearly – Krish Jan 05 '17 at 06:40
  • i think [this answer](http://stackoverflow.com/a/15269029/1219956) explains it a bit better – Fonix Jan 05 '17 at 06:45

0 Answers0