Search Unity

AppDelegateListener

Discussion in 'iOS and tvOS' started by Democre, Feb 8, 2014.

  1. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    Is there any doc on how to use the new AppDelegateListener? More specifically I looking at using the onOpenURL event that is being triggered from the XCode's application: openURL:sourceApplication:annotation

    I really don't want to write code into the UnityAppController, or use a category and override it, or use the PostProcessBuildPlayer to monkey patch it.

    Thanks!
     
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    no need to do anything of it if you dont want to change actual impl. Everything you put into Plugins/iOS will be added to final xcode project
    Also, you can create your custom app controller subclass: you just need to IMPL_APP_CONTROLLER_SUBCLASS(YourSubclassName); somewhere in your mm files to make unity use it
     
  3. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    Thanks Alexey! 2 more questions:

    1.) Is there a way to get the URL inside of Unity code? or would I have to write that in XCode and pass it in myself?

    2.) For the IMPL_APP_CONTROLLER_SUBCLASS macro, do I do something like this?
    Code (csharp):
    1.  
    2. @implementation UnityAppControllerSub
    3. - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
    4. {
    5.     //custom code here like set a user preference or parse the url
    6.     return [super application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
    7. }
    8. @end
    9. IMPL_APP_CONTROLLER_SUBCLASS(UnityAppControllerSub);
    10.  
    and then stick that in a UnityAppControllerSub.mm file under the plugins/iOS directory?
     
  4. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    well, yes - you write the code

    yes, just dont forget that you actually need to subclass/inherit from UnityAppController
     
  5. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Can you be a little more specific please? In UnityAppController.mm it has this method

    - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation

    that sends off the passed url and other data with

    AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);

    How and where do I respond to that notification inside Unity in order to access the passed url?

    Thanks
     
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    >>How and where do I respond to that notification inside Unity in order to access the passed url?
    this is native ios notification, so you write objc code to do what you want (register listener and pass url)
     
  7. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Thanks for the reply. I've never done that before. Do you have a link to an example?

    thanks
     
  8. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Seem to have got it working. Added this to top of UnityAppController.mm

    extern void UnitySendMessage(constchar *, constchar *, constchar *);

    then inside this method

    - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation

    I added

    UnitySendMessage("ReceiveMessages", "openURLComplete", "Message to send");

    So ReceiveMessages is a gameobject, openURLComplete is the function and "Message to send" is the test string I passed. Was able to debug it in unity so it seems fine.

    Thanks
     
    zyonneo likes this.
  9. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    PS to pass the URL I used

    const char *URLString = [[url absoluteString] UTF8String];
    UnitySendMessage("ReceiveMessages", "openURLComplete", URLString);
     
    zyonneo likes this.
  10. Jespertheend2

    Jespertheend2

    Joined:
    Jan 14, 2015
    Posts:
    23
    Thanks BillyMFT!
    I had no idea how to do this but now it works :D
     
  11. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Where do I paste this code " extern void UnitySendMessage(constchar *, constchar *, constchar *); " when I pasted this at the top,below the import functions I got a error message "Unknown type name 'constchar' ".Where to paste this