2011-02-03 3 views
4

약간의 도움이 필요합니다. 현재 방법이 있습니다. iTunes에서 현재 재생중인 트랙의 아티스트 이름, 트랙 이름 및 재생 시간을 가져 오는 Mac OS X 응용 프로그램의 updateTrackInfoObjective-C Mac OS X 분산 알림 iTunes

그러나 앱에서 분산 iTunes 알림을 듣고 싶습니다. com.apple.iTunes.playerInfo 그런 다음 알림이 iTunes에서 배포 될 때 updateTrackInfo 메서드를 호출하십시오. 누가 머리글과 구현 파일에 모두 쓸 필요가있는 사람이 나를 도울 수 있습니까?

감사합니다. Sami.

답변

13

당신은 -[NSDistributedNotificationCenter addObserver:selector:name:object:] 찾고 : 다른 곳에서 같은 클래스에서

NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter]; 
[dnc addObserver:self selector:@selector(updateTrackInfo:) name:@"com.apple.iTunes.playerInfo" object:nil]; 

... 그것은 심지어 당신에게 통지 트랙 정보의 전체 무리를 제공

- (void) updateTrackInfo:(NSNotification *)notification { 
    NSDictionary *information = [notification userInfo]; 
    NSLog(@"track information: %@", information); 
} 

합니다. 멋지지 않아?

- (id) init { 
self = [super init]; 
if (!self) return nil; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(receiveNotification:) 
              name:@"com.apple.iTunes.playerInfo" 
              object:nil]; 
return self;} 

- (void) receiveNotification:(NSNotification *) notification { 
if ([@"com.apple.iTunes.playerInfo" isEqualToString:@"com.apple.iTunes.playerInfo"]) { 
    NSLog (@"Successfully received the test notification!"); 
}} 

을하지만 NSNotificationCenter 대신 NSDistributedNotificationCenter를 사용 : 당신의 도움에 대한

+0

대단히 효과적이지만 현재 노래가 멈췄을 때 알려주지 않습니다. 즉 현재 노래가 _no_입니다. – seaturtle

3

감사합니다, 당신은 내 코드를 수정 도움이,이까지 작성했다. 나는 잘못 가고있는 곳이다.

감사합니다. Sami.

+1

네, 그럴 겁니다. 부수적으로,'-receiveNotification :'메소드의'if()'문은 항상 true가 될 것입니다 ... –