Skip to content

Instantly share code, notes, and snippets.

@conceptdev
Created July 20, 2010 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conceptdev/482295 to your computer and use it in GitHub Desktop.
Save conceptdev/482295 to your computer and use it in GitHub Desktop.
using MonoTouch.Foundation;
using MonoTouch.CoreLocation;
using MonoTouch.ObjCRuntime;
using System;
/// <summary>
/// Helper class intended SOLELY to provide access to
/// CLLocationManager:locationServicesEnabled
/// that works on MonoTouch versions 2.x and 3.x (iOS3 and iOS4)
/// </summary>
/// <remarks>
/// In iOS4, the locationServicesEnabled method was changed from an instance to a class method (and the instance method was deprecated).
///
/// AFAICT, this means in iOS4 and above we are encouraged to use the class method, but the instance method still exists for backward compatibility.
/// This seems to be true since my older apps (compiled against 3.2 and MT 2.x) still work on iPad and iOS4.
///
/// However, MT3.x has totally removed the instance method. What this seems to mean is:
///
/// - an app compiled with MT2 using an (instance of CLLocationManager).LocationServicesEnabled will work on iOS3 and iOS4 (because even though it has been deprecated, it still exists)
/// - if i update to MT3, the project will not compile unless i change the instance method to a class method
/// call CLLocationManager.LocationServicesEnabled (which is bound to the obj-c static method, which doesn't exist in OS3)
/// - once the code is updated for MT3, it will run on iOS4 BUT on iOS3 (older phones+iPad) this error occurs:
/// Objective-C exception thrown. Name: NSInvalidArgumentException Reason: *** +[CLLocationManager locationServicesEnabled]: unrecognized selector sent to class 0xe67e2ca
///
/// http://developer.apple.com/iphone/library/documentation/CoreLocation/Reference/CLLocationManager_Class/DeprecationAppendix/AppendixADeprecatedAPI.html
///
/// http://monotouch.net/Releases/API_Changes/2.0_to_3.0
/// </remarks>
[Register("CLLocationManager")]
public class CLLocationManager2 : NSObject
{
// Fields
private static IntPtr class_ptr = Class.GetHandle("CLLocationManager");
private static IntPtr selLocationServicesEnabled;
// Methods
[Export("init")]
public CLLocationManager2() : base(NSObjectFlag.Empty)
{
selLocationServicesEnabled = (new Selector ("locationServicesEnabled")).Handle;
if (base.IsDirectBinding)
{
base.Handle = Messaging.IntPtr_objc_msgSend(this.Handle, Selector.Init);
}
else
{
base.Handle = Messaging.IntPtr_objc_msgSendSuper(base.SuperHandle, Selector.Init);
}
}
[Export("initWithCoder:")]
public CLLocationManager2(NSCoder coder) : base(NSObjectFlag.Empty)
{
selLocationServicesEnabled = (new Selector ("locationServicesEnabled")).Handle;
if (base.IsDirectBinding)
{
base.Handle = Messaging.IntPtr_objc_msgSend_IntPtr(this.Handle, Selector.InitWithCoder, coder.Handle);
}
else
{
base.Handle = Messaging.IntPtr_objc_msgSendSuper_IntPtr(base.SuperHandle, Selector.InitWithCoder, coder.Handle);
}
}
public CLLocationManager2(NSObjectFlag t) : base(t)
{
selLocationServicesEnabled = (new Selector ("locationServicesEnabled")).Handle;
}
public CLLocationManager2(IntPtr handle) : base(handle)
{
selLocationServicesEnabled = (new Selector ("locationServicesEnabled")).Handle;
}
// Properties
public override IntPtr ClassHandle
{
get
{
return class_ptr;
}
}
public virtual bool LocationServicesEnabled
{
[Export("locationServicesEnabled")]
get
{
if (base.IsDirectBinding)
{
return Messaging.Boolean_objc_msgSend(this.Handle, selLocationServicesEnabled);
}
return Messaging.Boolean_objc_msgSendSuper(base.SuperHandle, selLocationServicesEnabled);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment