简体   繁体   中英

Toggle airplane mode monotouch xamarin

Note: this isn't an app for the App Store, so using private API's isn't a concern to me.

I've been trying to toggle Airplane mode off and back on using Xamarin/Monotouch based off this SO question: How to turn on/off airplane mode in IOS 5.1 using private API

I'm having a hard time getting this code to run in monotouch - first thing I tried was to create a native objective C library using a monotouch binding project.

The AppSupport framework is used so my linkwith file is as follows:

[assembly: LinkWith ("libAirplaneModeLib.a", LinkTarget.ArmV7, ForceLoad = true, Frameworks = "AppSupport")]

When I do this, it compiles but I can't reference the namespace from my main project. If I don't include the framework as follows:

[assembly: LinkWith ("libAirplaneModeLib.a", LinkTarget.ArmV7, ForceLoad = true)]

In this case I can reference the namespace, but the main project compiles on linking with:

  "_OBJC_CLASS_$_RadiosPreferences", referenced from:
      objc-class-ref in libAirplaneModeLib.a(AirplaneModeLib.o)
ld: symbol(s) not found for architecture armv7
collect2: ld returned 1 exit status

error MT5202: Native linking failed. Please review the build log.

My ApiDefinition file is as follows:

[BaseType(typeof(NSObject))]
public interface AirplaneModeLib
{
    [Export("toggleAirplane")]
    void ToggleAirplane ();
}

The native library is as follows:

#import "AirplaneModeLib.h"
#import "RadiosPreferences.h"

@implementation AirplaneModeLib

- (void) toggleAirplane {
    RadiosPreferences* preferences = [[RadiosPreferences alloc] init];
    preferences.airplaneMode = NO;
    [preferences synchronize];

    preferences.airplaneMode = YES; 
    [preferences synchronize];
    [preferences release];        
}

@end

Is there a better or easier way to do this? If not, can anyone suggest what I'm doing wrong with my current approach?

It seems I've solved it. For some reason the binding project can't reference the AppSupport framework (or can, by some method I'm not aware of)

In my main project I manually added the additional build arguments

-gcc_flags "-F /path/to/framework -framework AppSupport"

And it compiles fine now. Still doesn't toggle airplane mode, but that's a separate issue I guess...

It appears that even though this code will run, it will not toggle airplane mode. As far as I can tell there is no way to do this with a non-jailbroken device in iOS 5+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM