簡體   English   中英

如何與NSNotification一起發送字符串?

[英]How to send a string along with NSNotification?

我正在嘗試為phonegap / cordova編寫一個插件,該插件可在電話中斷時恢復音頻。 我正在使用AVAudioSesionInterruptionNotification,並且運作良好。 但是,我需要能夠向我的事件處理程序發送一個字符串,但是我不知道該怎么做。

我在這里設置一個事件監聽器,並從javascript層調用它:

- (void) startListeningForAudioSessionEvent:(CDVInvokedUrlCommand*)command{
     NSString* myImportantString = [command.arguments objectAtIndex:0];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
}

我在這里處理事件:

- (void) onAudioSessionEvent:(NSNotification *)notification
{
    if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){        
        //do stuff using myImportantString
    }
}

無法弄清楚如何將myImportantString傳遞給onAudioSessionEvent。 我對Objective-C的了解很少(因此我使用了cordova),所以請像對待孩子一樣回應。 謝謝!

順便說一句,我只是想在此處找到的cordova媒體插件之上添加幾個方法: https : //github.com/apache/cordova-plugin-media/tree/master/src/ios

所以上面的代碼是我的.m文件的整體減去這部分

@implementation CDVSound (extendedCDVSound)

像這樣:

// near the top of MyController.m (above @implementation)
@interface  MyController ()

@property NSString *myImportantString;

@end

// inside the implementation
- (void) startListeningForAudioSessionEvent:(CDVInvokedUrlCommand*)command{
     self.myImportantString = [command.arguments objectAtIndex:0];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
}

- (void) onAudioSessionEvent:(NSNotification *)notification
{
    if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){        
        //do stuff using self.myImportantString
    }
}

使用基於塊的API添加觀察者可能更容易:

- (void) startListeningForAudioSessionEvent:(CDVInvokedUrlCommand*)command{
    NSString* myImportantString = [command.arguments objectAtIndex:0];
    id observer = [[NSNotificationCenter defaultCenter] addObserverForName:AVAudioSessionInterruptionNotification
                                                                    object:nil
                                                                     queue:nil
                                                                usingBlock:^(NSNotification *notification){
        if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){        
            //do stuff using myImportantString; you could even skip the use of that
            //temporary variable and directly use [command.arguments objectAtIndex:0],
            //assuming that command and command.arguments are immutable so that you
            //can rely on them still being the same
        }
    }];
}

通過使用基於塊的API,可以在發布通知時調用的代碼中直接引用添加觀察者時范圍內的任何變量。

完成觀察后,您需要除去observer作為通知的觀察者。

暫無
暫無

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

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