Skip to content

Commit

Permalink
Removed events from UIApplication that were crashing Unity
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpeppers committed Apr 16, 2015
1 parent c56fccf commit bb1cdcc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 67 deletions.
29 changes: 15 additions & 14 deletions Assets/Tests/UIApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ public void SharedApplication()
Assert.AreNotEqual(IntPtr.Zero, app.Handle);
}

[Test]
public void ApplicationEvents()
{
var app = UIApplication.SharedApplication;

app.DidBecomeActive += (sender, e) =>
{
Console.WriteLine("DidBecomeActive!");
};
app.WillResignActive += (sender, e) =>
{
Console.WriteLine("WillResignActive!");
};
}
// These events are removed temporarily until we find a workaround
// [Test]
// public void ApplicationEvents()
// {
// var app = UIApplication.SharedApplication;
//
// app.DidBecomeActive += (sender, e) =>
// {
// Console.WriteLine("DidBecomeActive!");
// };
// app.WillResignActive += (sender, e) =>
// {
// Console.WriteLine("WillResignActive!");
// };
// }

[Test]
public void SharedApplicationDispose()
Expand Down
108 changes: 55 additions & 53 deletions Assets/iOS4Unity/UIApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,61 @@ public override IntPtr ClassHandle
internal UIApplication(IntPtr handle)
: base(handle)
{
ObjC.MessageSend(Handle, "setDelegate:", Handle);
}

public event EventHandler DidBecomeActive
{
add { Callbacks.Subscribe(this, "applicationDidBecomeActive:", value); }
remove { Callbacks.Unsubscribe(this, "applicationDidBecomeActive:", value); }
}

public event EventHandler WillResignActive
{
add { Callbacks.Subscribe(this, "applicationWillResignActive:", value); }
remove { Callbacks.Unsubscribe(this, "applicationWillResignActive:", value); }
}

public event EventHandler WillEnterForeground
{
add { Callbacks.Subscribe(this, "applicationWillEnterForeground:", value); }
remove { Callbacks.Unsubscribe(this, "applicationWillEnterForeground:", value); }
}

public event EventHandler WillTerminate
{
add { Callbacks.Subscribe(this, "applicationWillTerminate:", value); }
remove { Callbacks.Unsubscribe(this, "applicationWillTerminate:", value); }
}

public event EventHandler DidReceiveMemoryWarning
{
add { Callbacks.Subscribe(this, "applicationDidReceiveMemoryWarning:", value); }
remove { Callbacks.Unsubscribe(this, "applicationDidReceiveMemoryWarning:", value); }
}

public event EventHandler<NSErrorEventArgs> FailedToRegisterForRemoteNotifications
{
add
{
if (_failed == null)
_failed = new Dictionary<object, IntPtrHandler2>();
IntPtrHandler2 callback = (_, i) => value(this, new NSErrorEventArgs { Error = Runtime.GetNSObject<NSError>(i) });
_failed[value] = callback;
Callbacks.Subscribe(this, "application:didFailToRegisterForRemoteNotificationsWithError:", callback);
}
remove
{
IntPtrHandler2 callback;
if (_failed != null && _failed.TryGetValue(value, out callback))
{
_failed.Remove(value);
Callbacks.Unsubscribe(this, "application:didFailToRegisterForRemoteNotificationsWithError:", callback);
}
}
}
//Overwriting this delegate crashes Unity, will revisit this
//ObjC.MessageSend(Handle, "setDelegate:", Handle);
}

//These events cause issues in Unity, will revisit this
// public event EventHandler DidBecomeActive
// {
// add { Callbacks.Subscribe(this, "applicationDidBecomeActive:", value); }
// remove { Callbacks.Unsubscribe(this, "applicationDidBecomeActive:", value); }
// }
//
// public event EventHandler WillResignActive
// {
// add { Callbacks.Subscribe(this, "applicationWillResignActive:", value); }
// remove { Callbacks.Unsubscribe(this, "applicationWillResignActive:", value); }
// }
//
// public event EventHandler WillEnterForeground
// {
// add { Callbacks.Subscribe(this, "applicationWillEnterForeground:", value); }
// remove { Callbacks.Unsubscribe(this, "applicationWillEnterForeground:", value); }
// }
//
// public event EventHandler WillTerminate
// {
// add { Callbacks.Subscribe(this, "applicationWillTerminate:", value); }
// remove { Callbacks.Unsubscribe(this, "applicationWillTerminate:", value); }
// }
//
// public event EventHandler DidReceiveMemoryWarning
// {
// add { Callbacks.Subscribe(this, "applicationDidReceiveMemoryWarning:", value); }
// remove { Callbacks.Unsubscribe(this, "applicationDidReceiveMemoryWarning:", value); }
// }
//
// public event EventHandler<NSErrorEventArgs> FailedToRegisterForRemoteNotifications
// {
// add
// {
// if (_failed == null)
// _failed = new Dictionary<object, IntPtrHandler2>();
// IntPtrHandler2 callback = (_, i) => value(this, new NSErrorEventArgs { Error = Runtime.GetNSObject<NSError>(i) });
// _failed[value] = callback;
// Callbacks.Subscribe(this, "application:didFailToRegisterForRemoteNotificationsWithError:", callback);
// }
// remove
// {
// IntPtrHandler2 callback;
// if (_failed != null && _failed.TryGetValue(value, out callback))
// {
// _failed.Remove(value);
// Callbacks.Unsubscribe(this, "application:didFailToRegisterForRemoteNotificationsWithError:", callback);
// }
// }
// }

public static UIApplication SharedApplication
{
Expand Down

0 comments on commit bb1cdcc

Please sign in to comment.