Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

calling a method after each 60 seconds in iPhone

I have created an GKSession and as its object is created, it starts search for availability of devices, as

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

I want to call this method after each 60 seconds, what should I do?

Upvotes: 33

Views: 50516

Answers (6)

Gurjinder Singh
Gurjinder Singh

Reputation: 10299

Swift 5.0

var timer = Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)

@objc func updateTimer() {
   print("1 min passed")
}

Upvotes: 0

J. Doe
J. Doe

Reputation: 13033

Swift 3:

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

Upvotes: 2

Mannam Brahmaiah
Mannam Brahmaiah

Reputation: 2283

Use the following code:

 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
 }
}

Upvotes: 3

Gal Marom
Gal Marom

Reputation: 8629

Once you set NSTimer to scheduleWithTimeInterval it calls it immediately. You can use

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

Upvotes: 13

Anish
Anish

Reputation: 2917

You can use schedular method...

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

you can call above function by using this...

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

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Use NSTimer

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

After each 60.0 second , iOS will call the below function

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

Upvotes: 93

Related Questions