UIApplicationBackgroundRefreshStatusDidChangeNotification用法没有相应的委托方法 [英] UIApplicationBackgroundRefreshStatusDidChangeNotification usage without corresponding delegate method

查看:635
本文介绍了UIApplicationBackgroundRefreshStatusDidChangeNotification用法没有相应的委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得iOS 7中引入的 UIApplicationBackgroundRefreshStatusDidChangeNotification 在没有支持UIApplication委托方法的情况下几乎没用。因为,当用户为我的应用程序开启后台刷新状态时,应用程序不会得到通知。

I feel that UIApplicationBackgroundRefreshStatusDidChangeNotification introduced in iOS 7 is of little use without supporting UIApplication delegate method. Because, the app is not notified when user has switch ON the background refresh state for my app.

这是我的通知处理程序...

This is my notification handler...

- (void)applicationDidChangeBackgroundRefreshStatus:(NSNotification *)notification
{
    NSLog(@"applicationDidChangeBackgroundRefreshStatus with notification info = %@ and refresh status = %d", notification, UIApplication.sharedApplication.backgroundRefreshStatus);

    if (UIApplication.sharedApplication.backgroundRefreshStatus == UIBackgroundRefreshStatusAvailable) {
//        if ([CLLocationManager locationServicesEnabled]) {
            [self.locationManager startUpdatingLocation];
//        }
    }
}

如上所述,我想要在通过app 设置>常规>后台应用刷新制作UIBackgroundRegreshStatus 可用时开始更新核心位置。我觉得在UIApplicationDelegate中应该有一个合适的委托方法让应用知道这个变化,以便App可以重新建立它所需要的一切。

As above, I want to start updating core location when UIBackgroundRegreshStatus is made Available through app Settings > General > Background App Refresh. I feel there should have been an appropriate delegate method in UIApplicationDelegate to let the app know about this change so that App could re-establish everything it needs to.

我要么缺少某些内容(预先存在的API)或Apple SDK工程师对此通知用法有其他一些/有限的意图。请建议。

Either I'm missing something (pre-existing API) or Apple SDK engineers have some other/limited intentions about this notification usage. Please advice.

推荐答案

理想情况下,您无需检查该设置。看起来好像你正在以背景的方式走错路。通过最小化应用程序,系统将定期唤醒您的应用程序并允许其执行任务。从您的代码中,您想要更新位置。第一个开始就在这里,使用这个委托方法,当应用程序被唤醒以进行后台获取时调用

ideally, you never have to check for that setting. It looks as though you are going around background fetching the wrong way. With the application minimised, the system will periodically wake your application up and allow it to perform tasks. From your code , you want to update the location. first place to start is here , using this delegate method that gets called when the app is woken up for a background fetch

///具有获取后台模式的应用程序可以给予在后台获取更新内容或在方便系统时获取更多内容的机会。在这些情况下将调用此方法。您应该在完成该操作后立即调用fetchCompletionHandler,以便系统可以准确地估计其功率和数据成本。
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void(^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);

/// Applications with the "fetch" background mode may be given opportunities to fetch updated content in the background or when it is convenient for the system. This method will be called in these situations. You should call the fetchCompletionHandler as soon as you're finished performing that operation, so the system can accurately estimate its power and data cost. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);

这是你如何使用它,在你的应用程序委托实现中,定义方法体如下

this is how you use it, in your application delegate implementation, define the method body as follows

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{

    NSLog(@"APP IS AWAKE FOR A BACKGROUND FETCH");
    //do all the work you want to do 

//once done, its important to call the completion hadler, otherwise the system will complain
          completionHandler(UIBackgroundFetchResultNewData);

 }

但是,因为您要更新位置,你自己的代表,你只希望在你的代表返回时调用完成处理程序,而不是在那之前。调用完成处理程序将使您的应用程序重新进入休眠状态。由于完成处理程序是块对象,因此它可以像任何其他对象一样传递。一种方法如下:在应用程序委托头文件中,定义一个块对象:

however, since you are updating the location, which has its own delegates, you only want the completion handler to be called when your delegates return and not before that. Calling the completion handler will send your application back to sleep. Since the completion handler is a block object, it can be passed around like any other object. One way of doing that is as follows: in the application delegate header file, define a block object:

void (^fetchCompletionHandler)(UIBackgroundFetchResult);

然后在你的performFetchWithCompletionHandler中有:

then in your performFetchWithCompletionHandler have :

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    fetchCompletionHandler = completionHandler;

    NSLog(@"APP IS AWAKE FOR A BACKGROUND FETCH");
    //do all the work you want to do 
            [self.locationManager startUpdatingLocation];


 }

在您的位置之后的某个适当时间委托方法已经返回,你调用

at some appropriate time, after your location delegate methods have returned, you call

fetchCompletionHandler (UIBackgroundFetchResultNewData);

一定要检查你的fetchCompletionHandler是否为非nill,当nil会立即崩溃你的应用程序时调用它。从这里的苹果文档中了解更多关于块的信息 https: //developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

be sure to check if your fetchCompletionHandler is non nill, calling it when nil will immediately crash your app. read more on blocks from the apple docs here https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html

还可以查看call [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:];指定后台提取操作之间必须经过的最短时间。

also have a look at the call [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval: ]; which Specifies the minimum amount of time that must elapse between background fetch operations.

您可能会将其放入应用委托应用程序didFinishLaunchingWithOptions方法中。

You would probably chuck that into your app delegate application didFinishLaunchingWithOptions method.

希望这可以帮到你。

这篇关于UIApplicationBackgroundRefreshStatusDidChangeNotification用法没有相应的委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆