在iPhone中每60秒后调用一次方法

我创build了一个GKSession,创build它的对象后,它开始search设备的可用性

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { 

我想每60秒后调用一次这个方法,我该怎么办?

使用NSTimer

 NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES]; 

每隔60.0秒后,iOS会调用下面的函数

 -(void) callAfterSixtySecond:(NSTimer*) t { NSLog(@"red"); } 

一旦你设置NSTimer scheduleWithTimeInterval立即调用它。 您可以使用

  [self performSelector:@selector(doSomething) withObject:nil afterDelay:60.0f]; 

使用下面的代码:

  NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self selector: @selector(timerFired:) userInfo: nil repeats: YES]; - (void)timerFired:(NSTimer*)theTimer{ if(condition){ [theTimer isValid]; //recall the NSTimer //implement your methods }else{ [theTimer invalidate]; //stop the NSTimer } } 

你可以使用schedular方法…

 -(void) callFunction:(CCTime)dt { NSLog(@"Calling..."); } 

你可以通过使用这个调用上面的函数…

 [self schedule:@selector(callFunction:) interval:60.0f]; 

Swift 3:

 Timer.scheduledTimer(withTimeInterval: 60, repeats: true, block: { (timer) in print("That took a minute") })