c_cpp 尝试在React Native iOS中实现后台任务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 尝试在React Native iOS中实现后台任务相关的知识,希望对你有一定的参考价值。
var BackgroundTask = require('react-native').NativeModules.BackgroundTask;
BackgroundTask.beginTask("taskName", function(){})
//
// BackgroundTask.m
// tomtrack
//
// Created by Liam Edwards-Playne on 13/02/2016.
//
#import <Foundation/Foundation.h>
#import "BackgroundTask.h"
#import "RCTUtils.h"
/*
Sample:
BackgroundTask.beginBackgroundTask("gpsTracking", () => {})
*/
@implementation BackgroundTask
RCT_EXPORT_MODULE();
#pragma mark - Public API
RCT_EXPORT_METHOD(beginBackgroundTask:(NSString *)taskName jsCallback:(RCTResponseSenderBlock)jsCallback)
{
UIApplication *application = RCTSharedApplication();
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:taskName expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
printf("Running in the background\n");
// Call the JS code
jsCallback(@[]);
printf("Back from the JS\n");
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
@end
//
// BackgroundTask.h
// tomtrack
//
// Created by Liam Edwards-Playne on 13/02/2016.
//
#import "RCTBridgeModule.h"
@interface BackgroundTask : NSObject <RCTBridgeModule>
@end
以上是关于c_cpp 尝试在React Native iOS中实现后台任务的主要内容,如果未能解决你的问题,请参考以下文章
无法链接 react-native-fbsdk iOS
尝试更改状态栏样式在 ios 中给了我错误 - React native
在 iOS 中将 react-native-navigation 与 react-native-callkit 集成
React Native Maps 红色轮廓 ios
React Native Push Notification - react-native-fcm - ios无法构建
javascript React Native - 语音到文本(我尝试使用IOS https://github.com/wenkesj/react-native-voice,你可以试试Android