简体   繁体   English

Objective-C:未声明的标识符applicationDidEnterBackground

[英]Objective-c: undeclared identifier applicationDidEnterBackground

I'm trying to use code from this example : 我正在尝试使用此示例中的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    bgTask = [application beginBackgroundTaskWithName:@"MyTask" 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), ^{

        // Do the work associated with the task, preferably in chunks.

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

I'm a react-native developer, so sorry, probably this is a silly question :) 我是一名本机开发人员,很抱歉,这可能是一个愚蠢的问题:)

How do i know where from should i import this method? 我怎么知道我应该从哪里导入这种方法? applicationDidEnterBackground

Docs aren't give me answer for this either. 文件也没有给我答案。

There are 2 ways of doing this: 有两种方法可以做到这一点:

AppDelegate

You should implement this method in your AppDelegate.m You can find your app's delegate by looking at main.m file and you will something like 您应该在AppDelegate.m实现此方法。您可以通过查看main.m文件来查找应用程序的委托,您将看到

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

AppDelegate is your app's delegate class. AppDelegate是您的应用程序的委托类。

NSNotificationCenter

There is a built-in UIApplicationDidEnterBackgroundNotification notification which is declared in UIApplication.h and you can add any kind of object as observer to NSNotificationCenter . 有一个内置的UIApplicationDidEnterBackgroundNotification这是在宣告通知UIApplication.h ,你可以添加任何类型的对象作为观察员NSNotificationCenter

[[NSNotificationCenter defaultCenter]
     addObserver:{myObserver}
     selector:@selector({myObserverMethod}:) // adding ":" at the end will provide you the sent notification as parameter to your method
     name:UIApplicationDidEnterBackgroundNotification
     object:nil];

Now you need to implement {myObserverMethod}:(NSNotification *)notification in your {myObserver} class 现在,您需要在{myObserver}类中实现{myObserverMethod}:(NSNotification *)notification

// MyObserver.m
- (void)myObserverMethod:(NSNotification *)notification {
    UIApplication *application = notification.object;
    // Now you can call `[application beginBackgroundTask:]` or etc.
    NSLog(@"applicationDidEnterBackground");
}

NOTE: Don't forget that prior to iOS 9 you need to call [[NSNotificationCenter defaultCenter] removeObserver:{myObserver}] when you are done with it! 注意:不要忘记, 在iOS 9之前,完成后需要调用[[NSNotificationCenter defaultCenter] removeObserver:{myObserver}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM