2010-11-30 3 views
4

로컬 알림 설정을 처리하기 위해 새 스레드를 만들 수 있는지 알아야합니다.새 스레드에서 로컬 알림을 설정 하시겠습니까?

내 앱은이 알림에 크게 의존하므로 전화가 알림을 설정하는 동안 앱을 작동시키고 싶습니다.

예 :

(지금)

앱을 실행이 앱이 로컬 알림을 설정하려면 시작 화면에서 중단, 그것은 시작합니다.

앱 출시를 (내가 원하는) 및 로컬 알림을 설정하는 동안 사용할 수 있습니다.

는 나도 :)하십시오, 일부 샘플 코드가 필요

(기록을 위해, 내가 60 개 지역 통지 앱에서 내 자신의 이유로 전경 입력 할 때마다 ... 설정하고)

감사합니다! !

답변

3

예이 작업을 수행 할 수 있습니다 참조 : 내가 지금 일하고 프로젝트에서 촬영

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Add the navigation controller's view to the window and display. 
    [NSThread detachNewThreadSelector:@selector(scheduleLocalNotifications) toTarget:self withObject:nil]; 
    [window addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

-(void) scheduleLocalNotifications 
{ 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    for (int i = 0; i < 60; i++) 
    { 
     UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
     if (localNotif == nil) 
      return; 
     NSDate *sleepDate = [[NSDate date] dateByAddingTimeInterval:i * 60]; 
     NSLog(@"Sleepdate is: %@", sleepDate); 

     localNotif.fireDate = sleepDate;  

     NSLog(@"fireDate is %@",localNotif.fireDate); 
     localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
     localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"This is local notification %i"), i]; 

     localNotif.alertAction = NSLocalizedString(@"View Details", nil); 
     localNotif.soundName = UILocalNotificationDefaultSoundName; 
     localNotif.applicationIconBadgeNumber = 1; 

     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
     NSLog(@"scheduledLocalNotifications are %@", [[UIApplication sharedApplication] scheduledLocalNotifications]); 
     [localNotif release]; 

    } 

    [pool release]; 
} 

, 나는 예상대로 작동하는지 확인할 수 있습니다.

편집 : NSAutoreleasePool가 누락 된 처리 때문에
scheduleLocalNotifications에서 누출 된 - 지금은 예에 추가됩니다.

+0

안녕하세요 np .. 그게 우리가 여기에있어 –

+1

메인 스레드에서 UIApplication을 사용하면 안전하지 않으며 '예기치 않은 결과'가 발생할 수 있습니다. 왜 스레드에서 제거해야하는지에 대한 질문입니다. 많은 시간이 필요합니다. 쓰레드가 필요없는 경우, 안전을 위해 메인 쓰레드에서 'scheduleLocalNotification'을 실행해야합니다. –

3

스레드를 수행하는 한 가지 방법은 performSelectorInBackground입니다. 예를 들어

:

[myObj performSelectorInBackground:@selector(doSomething) withObject:nil]; 

당신은 애플이 꽤 강하게 명시 적으로 산란 스레드 대신 NSOperation의 파견 대기열과 같은 높은 수준의 개념을 사용하는 것이 추천되고 있다는 점에 유의해야한다. 나는 모든 시간을 수행 Concurrency Programming Guide

+0

귀하의 기여에 감사드립니다! – Mazyod

+0

더 많이 충돌합니다. : –

관련 문제