I'm trying to implement a function in AppDelegate.m in order to update my app in background.
I've found an article online (this is the link: https://medium.com/@vialyx/ios-dev-course-background-modes-fetch-70c18f9f58d5) but it's written in Swift and I can't translate it from Swift to Objective C this part of the code:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Create url which from we will get fresh data
if let url = URL(string: "https://www.vialyx.com") {
// Send request
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
// Check Data
guard let `data` = data else { completionHandler(.failed); return }
// Get result from data
let result = String(data: data, encoding: .utf8)
// Print result into console
print("performFetchWithCompletionHandler result: \(String(describing: result))")
// Call background fetch completion with .newData result
completionHandler(.newData)
}).resume()
}
}
Can anyone help me? Thanks.
You can try
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Create url which from we will get fresh data
NSURL*url = [[NSURL alloc] initWithString:@"https://www.vialyx.com"];
[[NSURLSession sharedSession ] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data != nil) {
NSString* result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Print result into console
NSLog(@"performFetchWithCompletionHandler result: %@",result);
completionHandler(UIBackgroundFetchResultNewData);
}
else {
completionHandler(UIBackgroundFetchResultFailed);
}
}];
}