Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于长进程的iPhone计时器任务_Iphone_Objective C_Xcode_Timer - Fatal编程技术网

用于长进程的iPhone计时器任务

用于长进程的iPhone计时器任务,iphone,objective-c,xcode,timer,Iphone,Objective C,Xcode,Timer,当应用程序进入后台状态时,用于在后台运行的时间不起作用。下面是代码 在AppDelegate.h中 @interface AppDelegate : UIResponder <UIApplicationDelegate> { BOOL status; UIBackgroundTaskIdentifier bgTask; } 在视图控制器.h中 @interface vController : UIViewController <AVAudioPlayerDel

当应用程序进入后台状态时,用于在后台运行的时间不起作用。下面是代码

在AppDelegate.h中

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    BOOL status;
    UIBackgroundTaskIdentifier bgTask;
}
在视图控制器.h中

@interface vController : UIViewController <AVAudioPlayerDelegate>
-(void) run;
-(void) stopbackground;
-(void) getMessage:(NSTimer*)theTimer;;
+(vController*) getInstance;
@property (nonatomic,retain) NSTimer * timer;
@end

我在simulator 6.0上使用它取决于您的使用情况,这可能是不可能的。也就是说,苹果不允许您在“后台”执行代码,除非您正在执行以下任务之一:

  • 在后台向用户播放音频内容的应用程序, 例如音乐播放器应用程序
  • 随时通知用户其位置的应用程序,如导航应用程序
  • 支持互联网语音协议(VoIP)的应用程序
  • 需要下载和处理新内容的报摊应用程序
  • 从外部附件接收定期更新的应用程序

阅读更多信息:

通过创建实例,您可以在任何视图控制器中运行后台任务,如下所示:

//在
YourViewController.h
文件中声明以下内容

+ (YourViewController *)getInstance;
- (void)run;
-(void)stopbackground;
//在ViewController.m文件中定义卷

static  YourViewController *instance = NULL;

+(YourViewController *)getInstance {

    @synchronized(self) {
        if (instance == NULL) {
            instance = [[self alloc] init];
        }
    }
    return instance;
}


- (void)run

{
// strat Back ground task
}
-(void)stopbackground
{
// Stop Background task
}
AppDelegate.h
文件中声明以下内容

UIBackgroundTaskIdentifier bgTask; 
AppDelegate.m
文件中,使用以下方法

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Application entered background state.");

    // bgTask is instance variable
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {
            // Start background service synchronously
[[YourViewController getInstance] run];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });
}


- (void)applicationWillEnterForeground:(UIApplication *)application
{

    NSLog(@"Application entered background state.");

    // bgTask is instance variable
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {

 // Start background service synchronously
  [[YourViewController getInstance] stopbackground];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });


    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

非常感谢您的回复。。。我需要通过http通信下载一些东西,并以规定的时间间隔处理接收到的数据??这是一个实现细节(通过http下载)。你的应用程序在做上述哪一点?好吧,它不可能是“外部访问者的定期更新”,不可能是“VoIP”,不可能是“导航应用程序”,我敢打赌它不是“音乐播放器应用程序”。这就剩下了“报摊应用程序”。。。如果您没有制作“报摊应用程序”,或者我的假设是错误的,如果您希望您的应用程序获得AppStore的批准,则不应尝试在后台运行代码。谢谢您的回复。。。。这是应用程序的基本要求,它应该去某个服务器,定期通过http获取数据..谢谢你的回复。。西巴·普拉萨德,我会试试的。。thanxDo我需要在Run和StopBackground方法中使用Timer来运行定期重复的任务吗??请提供帮助…是的,您可以将计时器与Repeat Yes/NOHi Siba Prasad一起使用,我已经使用了代码,并且面临一个问题,即运行方法[当应用程序进入后台时]中使用的计时器没有启动其目标方法,我正在尝试播放警报声音。而从stopBackground方法的计时器调用相同的方法,并且其工作。。。。请帮忙你能发布你所有的代码,并再次编辑你的问题吗?这将有助于我们找到问题所在。
UIBackgroundTaskIdentifier bgTask; 
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Application entered background state.");

    // bgTask is instance variable
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {
            // Start background service synchronously
[[YourViewController getInstance] run];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });
}


- (void)applicationWillEnterForeground:(UIApplication *)application
{

    NSLog(@"Application entered background state.");

    // bgTask is instance variable
    NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIBackgroundTaskInvalid;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{

        if ([application backgroundTimeRemaining] > 1.0) {

 // Start background service synchronously
  [[YourViewController getInstance] stopbackground];

        }

        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIBackgroundTaskInvalid;

    });


    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}