Ios 如何发送请求进行API调用,直到布尔值为true

Ios 如何发送请求进行API调用,直到布尔值为true,ios,get,Ios,Get,我对iOS和后端开发非常陌生,所以请容忍我缺乏技术知识。我为这个可能令人困惑的标题道歉,但我的问题是: 我正在尝试创建一个iOS应用程序,允许用户在其所选课程的状态为“打开”时接收推送通知 检查所选课程状态是否打开的方法是从我学校的API发出GET请求,并解析JSON响应以提取课程状态 那么,我如何不断地获取请求以检查所选课程的状态,并在课程打开时向用户发送推送通知呢 如果有人能为我指明一个具体的研究方向,那就太好了,谢谢。推送通知应该来自某个服务器。我认为如果你想让应用程序发挥作用,你需要的是

我对iOS和后端开发非常陌生,所以请容忍我缺乏技术知识。我为这个可能令人困惑的标题道歉,但我的问题是:

我正在尝试创建一个iOS应用程序,允许用户在其所选课程的状态为“打开”时接收推送通知

检查所选课程状态是否打开的方法是从我学校的API发出GET请求,并解析JSON响应以提取课程状态

那么,我如何不断地获取请求以检查所选课程的状态,并在课程打开时向用户发送推送通知呢


如果有人能为我指明一个具体的研究方向,那就太好了,谢谢。

推送通知应该来自某个服务器。我认为如果你想让应用程序发挥作用,你需要的是一个带有本地通知的投票。这样做的缺点是,应用程序必须运行才能进行投票。我建议通过观看了解更多关于它的工作原理。要启动进行轮询的请求,请执行以下操作:

-(BOOL)application:(UIApplication*)app didFinishLaunchingWithOptions:
{
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}
在您的界面中:

@property (nonatomic, retain) NSTimer *timer;
在实施过程中:

-(void)someMethodSomewhere
{
      // Create a timer and automatically start it.
      self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0f // some number of seconds 
                              target:self
                            selector:@selector(checkStatus) 
                            userInfo:nil 
                             repeats:YES];

}

-(void)checkStatus 
{
    // Perform request, check course status

    if (/* course status is open */) 
    {
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        notification.alertBody = @"The course is now open";
        notification.fireDate = [NSDate date];
        [[UIApplication sharedApplication]presentLocalNotificationNow:notification];

        // Stop the timer
        [self.timer invalidate];
    }
}

编辑 要使其在后台运行,您可能需要阅读。结果是,您需要覆盖AppDelegate上的
应用程序:performFetchWithCompletionHandler:
选择器,然后从上面调用
checkStatus
方法。但是,您无法控制调用此函数的频率。这是操作系统的工作,在某种程度上是用户的偏好。在处理结束时,确保调用完成处理程序

您还必须设置提取的最小间隔。在应用程序的
应用程序:didFinishLaunchingWithOptions:
中,您需要添加如下内容:

-(BOOL)application:(UIApplication*)app didFinishLaunchingWithOptions:
{
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}
假设上面的代码也在AppDelegate中:

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    [self checkStatus];
    completionHandler(UIBackgroundFetchResultNewData);
}

您还必须在应用程序的Info.plist文件中设置属性。您需要将键
ui backgroundmodes
fetch
值一起添加。

但当用户关闭应用程序时,计时器是否可以运行?