ios 13 objective-c background task request

依然范特西╮ 提交于 2020-04-11 05:31:32

问题


I've got a question with objective-c and background task request.

Restricted to background modes with ios 13.

My App does not run in the background more than 30 seconds.

Background modes changed with ios 13.

I need to register a background task with objective-c like this:

BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.SO.apprefresh", using: nil) { task in
    self.scheduleLocalNotification()
    self.handleAppRefreshTask(task: task as! BGAppRefreshTask)
}

and I need schedule when app goes to background

func scheduleAppRefresh() {
    let request = BGAppRefreshTaskRequest(identifier: "com.SO.apprefresh")
    request.earliestBeginDate = Date(timeIntervalSinceNow: 2 * 60) // App Refresh after 2 minute.
    do {
        try BGTaskScheduler.shared.submit(request)
    } catch {
        print("Could not schedule app refresh: \(error)")
    }
}

回答1:


The register function looks like this.

static NSString* TaskID = @"com.SO.apprefresh";

-(void)configure {
    [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:TaskID
                                                          usingQueue:nil
                                                       launchHandler:^(BGTask *task) {
        [self scheduleLocalNotifications];
        [self handleAppRefreshTask:task];
    }];
}

-(void)scheduleLocalNotifications {
    //do things
}

-(void)handleAppRefreshTask:(BGTask *)task {
    //do things with task
}

The swift closure signature converts from:

{ task in
 //....
}

to an Objective-C block signature:

^(BGTask *task) {
  //...
}

And the other function looks like this.

-(void)scheduleAppRefresh {
    BGAppRefreshTaskRequest *request = [[BGAppRefreshTaskRequest alloc] initWithIdentifier:TaskID];
    request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:2*60];
    NSError *error = NULL;
    BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
    if (!success) {
        NSLog(@"Failed to submit request: %@",error);
    }
}

Any throwing function in Swift

func submit(_ taskRequest: BGTaskRequest) throws

Will always convert to a function that returns BOOL and passes an error by reference.

- (BOOL)submitTaskRequest:(BGTaskRequest *)taskRequest error:(NSError **)error




回答2:


Also don't forget to whitelist your Task Id in info.plist file under Permitted background task scheduler identifiers (BGTaskSchedulerPermittedIdentifiers)




回答3:


I've added a block of code that you wrote. But i have some issues.

all codes in appdelegate.m:

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"The code runs through here!");
}





static NSString* TaskID = @"com.SO.apprefresh";

-(void)configure {
    [[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:TaskID
                                                          usingQueue:nil
                                                       launchHandler:^(BGTask *task) {
        [self scheduleLocalNotifications];
        [self handleAppRefreshTask:task];
    }];
}

-(void)scheduleLocalNotifications {
    //do things
}
-(void)handleAppRefreshTask:(BGTask *)task {
    //do things with task
}


-(void)scheduleAppRefresh {
    BGAppRefreshTaskRequest *request = [[BGAppRefreshTaskRequest alloc] initWithIdentifier:TaskID];
    request.earliestBeginDate = [NSDate dateWithTimeIntervalSinceNow:2*60];
    NSError *error = NULL;
    BOOL success = [[BGTaskScheduler sharedScheduler] submitTaskRequest:request error:&error];
    if (!success) {
        NSLog(@"Failed to submit request: %@",error);
    }
}


@end

How can i declare Backgroundprocess? or What should I write to appdelegate.h?

Thanks a lot



来源:https://stackoverflow.com/questions/58149980/ios-13-objective-c-background-task-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!