Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by frarees · Sep 04, 2012 at 02:47 PM · iosxcodedelegateobjective-c

Unobtrusively extending AppController delegates. Possible?

Hi,

I'm developing a Unity Plug-in for iOS. To do so, I've created my C/Objective-C library, and a binding (so I can call it in my C# scripts). However, I'd like my plug-in to extend application delegates (such as applicationDidBecomeActive) and code them in my plug-in manager class, so that I don't touch the AppController.mm file at all. I need to do this because I want everything as automatized as possible (I'm using a custom-made post-build system to automatically update my XCode project). I've thought about Objective-C's categories, but I don't want to override the complete class (I don't want to lose the AppController original implementation) but extend it. Is there a programmatically way to properly extend the AppController delegates in my class, or any way to get my class noticed whenever an AppController delegate is called?

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by jacknutting · Jun 13, 2013 at 09:58 AM

If you just want to be able to react to app state changes, you can do some of this by creating your own class, instantiating it, and telling it to listen for the notification event that corresponds to the app delegate method. For instance, you can create something like the following file in Assets/Plugins/iOS:

 // BackgroundWatcher.m
 #import <UIKit/UIKit.h>
 
 @interface BackgroundWatcher : NSObject
 @end
 
 __strong BackgroundWatcher *sharedInstance;
 
 @implementation BackgroundWatcher
 
 + (void)load {
     sharedInstance = [[self alloc] init];
 }
 
 - (id)init {
     if (!(self = [super init])) return nil;
     
     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
     [nc addObserverForName:UIApplicationDidFinishLaunchingNotification
                     object:nil
                      queue:[NSOperationQueue mainQueue]
                 usingBlock:^(NSNotification *note) {
                     NSLog(@"App finished launching! Hooray!");
                 }];
     return self;
 }
 
 @end

The code in the "load" method (which you can think of as similar to MonoBehavior.Awake, but in this case is a class (static) method that is called when the class is loaded at runtime) creates a single instance of this class. The code in "init" sets up that instance to listen for the UIApplicationDidFinishLaunchingNotification, which is an event that is posted just after the correspond app delegate method is called. The code inside the block (where the NSLog call is shown here) is what will be executed when the notification occurs.

So this works in the general case. So far I've found one case where I had to actually insert some code into the app delegate instead, though. I wanted to send a message to unity in response to the app going to background. By the time that notification is sent, the app delegate has already paused Unity so it's too late :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by eppz · May 12, 2017 at 03:19 AM

I wanted the same, just made a writeup on this:
Override app delegate in Unity for iOS and OSX (1/4) Plugin workflow.

Basically two approach, one is doing with Notification Center, the other uses Method swizzling. Also there is an alternative for OSX.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Automatically import missing ios frameworks after ios build 0 Answers

Call Unity class in XCode 1 Answer

Using NavigationController in an iOS plugin 1 Answer

Is there a way to tell in Unity iOS when onDidReceiveMemoryWarning is received? 3 Answers

Issue with integration into native iOS app 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges