2016-06-30 27 views
0
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{ 
    //call 2 web service here. 
    [self jsonParser:jsonData]; 
    completionHandler(UIBackgroundFetchResultNewData); 
} 

我如下後臺遠程可執行通知Web服務調用不起作用?

-(void)jsonParser:(NSData *)data 
{ 
    //[downloader downloadXMLContentsFromURL:actualURL withXML:encrypted]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:actualURL] 
                   cachePolicy:NSURLRequestUseProtocolCachePolicy 
                  timeoutInterval:HTTP_REQUEST_TIME_OUT]; 
    [request setHTTPMethod:@"POST"]; 

    NSString *encodedXML = [encrypted urlEncodeUsingEncoding:NSUTF8StringEncoding]; 
    NSString *params = [NSString stringWithFormat:@"%@=%@", REQUEST_PARAMETER_NAME, encodedXML]; 
    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
    { 
     NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:nil]; 
    }]; 
    [postDataTask resume]; 
    //Here call to other service not shown 
} 

我已啓用「背景提取」從功能也是「遠程通知」稱這種方法

我是否需要實現這個方法?

- (void)application:(UIApplication *)application performFetchWithCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler 

這個工作正常,當應用程序處於活動狀態。但不工作的應用程序是在背景和Closed.same時間,當我打開應用程序它工作正常。我想在後臺運行服務時,應用程序關閉。如何解決這個問題?任何幫助將不勝感激。

回答

1

您必須啓用後臺提取服務,因此您需要設置MinimumBackgroundFetchInterval。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    [application setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum]; 
    return YES; 
} 

和實現這樣的公共方法實現

-(void)jsonParser:(NSData *)data Completion: (void (^)(UIBackgroundFetchResult))completionHandler 
{ 
    //[downloader downloadXMLContentsFromURL:actualURL withXML:encrypted]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:actualURL] 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:HTTP_REQUEST_TIME_OUT]; 
    [request setHTTPMethod:@"POST"]; 

    NSString *encodedXML = [encrypted urlEncodeUsingEncoding:NSUTF8StringEncoding]; 
    NSString *params = [NSString stringWithFormat:@"%@=%@", REQUEST_PARAMETER_NAME, encodedXML]; 
    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
    { 
     NSError *localError = nil; 
     NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&localError]; 
     if (localError != nil) { 
      // handle your data here 
      completionHandler(UIBackgroundFetchResultNewData); 
      NSLog(@"New data was fetched."); 
     }else{ 
      completionHandler(UIBackgroundFetchResultFailed); 
      NSLog(@"Failed to fetch new data."); 
     } 
    }]; 
    [postDataTask resume]; 
    //Here call to other service not shown 
} 

的方法和實現方法是這樣

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{ 
    NSDate *fetchStart = [NSDate date]; 

    [self jsonParser:jsonData Completion:^(UIBackgroundFetchResult result){ 

     NSDate *fetchEnd = [NSDate date]; 
     NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart]; 
     NSLog(@"Background Fetch Duration: %f seconds", timeElapsed); 
    }]; 
} 

我希望這會幫助你,請檢查此鏈接http://www.appcoda.com/ios7-background-fetch-programming/

+0

像魅力工作..感謝您的答案。 –

相關問題