Global variables in Uno native module

Hello guys i’m creating a native module with Uno, im trying to play a song from streaming, i have done this, but i need to declarate a global variable for “player” to use it in another 2 functions, is there any way to do this?

i have this code

public playAudiofromUrl(){
    AddMember(new NativeFunction("play",(NativeCallback)PlayAudio));
    AddMember(new NativeFunction("pause",(NativeCallback)PauseAudio));
    AddMember(new NativeFunction("resume",(NativeCallback)ResumeAudio));
}

object PlayAudio(Context c, object[] args){
    PlayAudio();
    return null;
}

object PauseAudio(Context c, object[] args){
    PauseAudio();
    return null;
}

object ResumeAudio(Context c, object[] args){
    ResumeAudio();
    return null;
}

[Foreign(Language.ObjC)]

static extern(iOS) void PlayAudio(){
    //i want that this 3 line of codes to be global!
    NSURL url = [[NSURL alloc]initWithString:@"myuri"];
    AVPlayerItem item = [[AVPlayerItem alloc]initWithURL:url];
    AVPlayer *player = [[AVPlayer alloc]initWithPlayerItem:item];
    //
    [player play];
}

[Foreign(Language.ObjC)]
static extern(iOS) void PauseAudio(){
    [player pause]; 
}

[Foreign(Language.ObjC)]
static extern(iOS) void ResumeAudio(){
    [player play]; 
}

static extern(!iOS) void PlayAudio(){

}

i hope you can help me! :smiley:

Hey!

You can hold onto Objective-C objects (id and subclasses) in Uno with ObjC.Object, so one way to do this is to add a field of type ObjC.Object to your Uno class. You can then use macros to set the Uno field from foreign code.

Here’s an example of how this can be done for Java.Objects; Objective-C objects work similarly.

Since you’ll use the _this object (the foreign representation of the Uno this object) your method can’t be static.

Hope that helps!