0
완료 블록이 작동하지 않는 이유를 알지 못합니다. 먼저 현재 위치를 찾은 다음 위도와 경도를 사용하여 서버에서 데이터를 가져옵니다. NSURLSessionDelegate를 사용하여 백그라운드 세션을 시도했지만 데이터를 얻지 못했습니다.오늘 확장 내에서 URL 세션 사용
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
[self updateWidget];
self.placeImageView.image = [UIImage imageNamed:@"no-image"];
self.crowdednessLevelImageView.image = [UIImage imageNamed:@"circle-crowd"];
self.placeImageView.layer.cornerRadius = 10;
NSUserDefaults *defaults = [[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.wherepeeps.mostCrowdedPlace.widgert"];
NSString *coordinateString = [defaults objectForKey:@"CurrentLocationCoordinateString"];
NSDictionary *responseObject = [defaults objectForKey:@"listOfPlacesForWidget"];
NSLog(@"responseObject - %@", responseObject);
if (coordinateString) {
NSString *stringUrl = [NSString stringWithFormat:@"https://sheltered-fjord-29765.herokuapp.com/api/v1/search?location=%@", coordinateString];
NSURLSession *mySession = [self configureMySession];
NSURL *url = [NSURL URLWithString:stringUrl];
[mySession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSUserDefaults *defaults = [[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.wherepeeps.mostCrowdedPlace.widgert"];
[defaults setObject:responseObject forKey:@"listOfPlacesForWidget"];
[defaults synchronize];
NSLog(@"responseObject - %@", responseObject);
completionHandler(NCUpdateResultNewData);
}];
} else {
completionHandler(NCUpdateResultNoData);
}
}
- (NSURLSession *)configureMySession {
if (!self.mySession) {
// NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.mycompany.myapp.backgroundsession"];
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
// To access the shared container you set up, use the sharedContainerIdentifier property on your configuration object.
config.sharedContainerIdentifier = @"com.mycompany.myappgroupidentifier";
self.mySession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}
return self.mySession;
}
'dataTaskWithURL :'당신이'resume'을 호출해야하는 작업을 반환합니다. – dan