簡體   English   中英

NSTIMER iOS SDK的問題

[英]issue With NSTIMER iOS SDK

我寫道:

[NSTimer scheduledTimerWithTimeInterval:900 target:self selector:@selector(CallGetCounts) userInfo:nil repeats:YES];

這意味着我想每5分鍾重復一次計時器,但是我的計時器沒有重復,無法找到原因

誰能告訴我答案。

我在“ AppDelegate ”->“ - (void)applicationDidEnterBackground:(UIApplication *)application ”方法中編寫了此代碼。

在后台,您的代碼被掛起,並且在應用程序再次出現在前台之前,不會收到任何計時器事件。

在給定的時間間隔后,您應該去安排本地通知以從后台喚醒。 但是,它們將向用戶顯示他必須首先接受的彈出窗口或橫幅。

以下是有關操作方法的一些步驟:

// When you want to schedule:
UILocalNotification* localNotification = [[[UILocalNotification alloc] init] autorelease];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // seconds
localNotification.timeZone = [NSTimeZone defaultTimeZone];  
localNotification.alertBody = @"Body text";
localNotification.alertAction = @"Button text";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

// when it's fired it will call your AppDelegate's didReceiveLocalNotification
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification 
{ 
    // you can handle timer event here and eventually re-schedule your local notification
}

通常,當應用進入后台時,它會被掛起,因此根本不會執行。 特別是,NSTimers不會觸發。 如果您希望后台發生某些事情,則需要將您的應用程序配置為在后台運行,並使用一種批准的方法來執行您想要執行的任務。 運行NSTimers不是受支持的任務之一。

建議您閱讀《 iOS編程指南》,尤其是“ 后台執行和多任務處理”部分。

UILocalNotification實例會在您設置的時間觸發時觸發彈出框(並喚醒您的應用程序)。如果您確實選擇了UILocalNotification這里有SO線程中討論的很好的教程鏈接。 希望那些會幫助您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM