将后台升级的数据任务功能从Swift翻译成Objective C

Translate data task function for background upgrade from Swift to Objective C

我正在尝试在 AppDelegate.m 中实现一个功能,以便在后台更新我的应用程序。

我在网上找到一篇文章(这是 link: https://medium.com/@vialyx/ios-dev-course-background-modes-fetch-70c18f9f58d5)但是它是用 Swift 写的,我无法从 Swift 翻译它至Objective C这部分代码:

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()
    }
}

谁能帮帮我?谢谢

你可以试试

- (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); 
    } 
  }];
}