Preprocessor not switching between __ANDROID__ and __IOS__

EpicLoydEpicLoyd USMember ✭✭

Hi! I'm using shared project with forms and when i'm want to switch from *.Droid to *.iOS project for checking cross-platform code preprocessor won't undefine ANDROID and define IOS for me. Why could it be and how can i fix it?

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited February 2017

    @EpicLoyd

                #region preprocessor commands __ANDROID__, __IOS__, __MOBILE__ do not work in PCL
                switch (Device.OS)
                {
                    case TargetPlatform.Android:
                        break;
                    case TargetPlatform.iOS:
                        break;
                    case TargetPlatform.Windows:
                        break;
                    case TargetPlatform.WinPhone:
                        break;
                    case TargetPlatform.Other:
                        break;
                }
                #endregion __ANDROID__, __IOS_, __MOBILE do not work in PCL
    
  • EpicLoydEpicLoyd USMember ✭✭
    @ClintStLaurent I use shared project, not PCL
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @EpicLoyd
    Ok. Does that change the suggested solution? You can still use TargetPlatform. can't you?
    "Not working in PCL" does not imply "Does Work in Shared". What you're saying with your problem description is that it doesn't work their either.

  • EpicLoydEpicLoyd USMember ✭✭

    @ClintStLaurent
    I can't do this because then i wouldn't be able to compile code because compiler won't compile iOS code for android and vice versa ( even if it conditioned by TargetPlatform ) so i need preprocessor working

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @EpicLoyd

    I'm not ashamed to admit you lost me. I can't even guess what you're doing. Are you saying you have iOS native code in your PCL/Shared project and want it included based on the preprocessor directive? This whole design is starting to sound... odd... and maybe like it isn't following the paradigm of established Xamarin solutions. But I could be way off on that with so little to go on.

    Also... You know you can define any directive you want, right? If you want to define CUSTOMERRELEASE you can do that, then #if CUSTOMERRELEASE. I do this all the time so QA gets a Release build that still has some special tools to make their QA job easier, without the actual customer release having those tools/screens/shortcuts/easter-eggs. So if your issue only boil down to lack of a directive, just make new ones.

  • EpicLoydEpicLoyd USMember ✭✭

    No, i don't want to compile iOS native code, haha. I had to have sepatate code for iOS and Droid devices and i want to split it using preprocessor. Like this:
    `

    if ANDROID

            cookie_manager.SetCookie(url, string.Format("{0}={1}", name, value));
    

    elif IOS

            var cookieDict = new NSMutableDictionary ();
            cookieDict.Add (NSHttpCookie.KeyOriginURL, new NSString(string));
            cookieDict.Add (NSHttpCookie.KeyName, new NSString(name));
            cookieDict.Add (NSHttpCookie.KeyValue, new NSString(value));
            cookieDict.Add (NSHttpCookie.KeyPath, new NSString("/"));
    
            var myCookie = new NSHttpCookie(cookieDict);
    
            NSHttpCookieStorage.SharedStorage.SetCookie(myCookie);
    

    else //WinPhone

    endif

    `

    Also... You know you can define any directive you want, right? If you want to define CUSTOMERRELEASE you can do that, then #if CUSTOMERRELEASE.

    That is what i'm talking about: i had ANDROID and IOS defined in *.iOS and *.Droid projects. But they are changed when i'm switching from Droid to iOS project
    Here is screenshot of what i'm mean

  • EpicLoydEpicLoyd USMember ✭✭

    @PierceBoggan
    Oooooh, blaming myself i had not noticed it before. Thank you :>

  • AndrKrmerAndrKrmer DEMember

    @PierceBoggan said:

    This is super confusing, so I'll be filing an enhancement request. By default, this setting should default to the active startup project, rather than always defaulting to Android.

    I wondered if there are any news on that? In the current Version of Visual Studio (15.5.6) the editor view still isn't synchronized with the active startup project.

    Thanks in advance

    Andre

    Microsoft MVP

  • jerryhuangjerryhuang Member ✭✭

    This problem is still persist in VS2019 16.3.7 as of today. It's not only displaying it wrong in the IDE, it also compile as what it displaying and causing the exception in runtime in my case; and cost me hours until I completely comment out the condition like below:
    //#if IOS
    // [DllImport("__Internal", EntryPoint = "decode_g726")]
    //#else
    // [DllImport("libCLib", EntryPoint = "decode_g726")]
    //#endif
    //test below for iOS and it works
    [DllImport("__Internal", EntryPoint = "decode_g726")]

    It can be easily reproduced by creating a brand new Xamarin forms solution, and create a code shared project. The only way I'm able to get it "refresh" is to, let say, I'm setting iOS as start up project, and IOS is not working, by unloading the Android project, the IOS will then be turn on and vice versa for ANDROID. However, by reloading the Android project back in, IOS will not get turn off.....

    This will be a nightmare when it comes to production release as you won't be able to find out until run-time (at least in my case)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @jerryhuang said:

     //#if __IOS__
     //                [DllImport("__Internal", EntryPoint = "decode_g726")]
     //#else
     //        [DllImport("libCLib", EntryPoint = "decode_g726")]
     //#endif
     //test below for iOS and it works
             [DllImport("__Internal", EntryPoint = "decode_g726")]
    

    If you wrap your code in proper markdown code tags the site parser doesn't try to format your code: Turning your underscores into bold, etc.

Sign In or Register to comment.