OneSignal / OneSignal-Unity-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your Unity app with OneSignal. https://onesignal.com
Other
222 stars 61 forks source link

3.0.1 Can't build for iOS on Windows (Could not find Entitlements.entitlements) #491

Open SergeiEfimik opened 2 years ago

SergeiEfimik commented 2 years ago

What happened?

The build is successful, but in the Unity console the error "Could not find a part of the path... Entitlements.entitlements" (see "Relevant log output" for full details). File "Entitlements.entitlements" is created, but its content is smaller than in file created in SDK version 2.14.6.

When opening a built project (.xworkspace) in xCode on Mac, it writes an error: The file "/Users/work/MyProject/iOS/C:/Unity3d/MyProject/Builds/iOS/Entitlements.entitlements" could not be opened. Verify the value of the CODE_SIGN_ENTITLEMENTS build setting for target "Unity-iPhone" is correct and that the file exists on disk.

Оn version 2.14.6 everything works correctly on iOS and Android. On version 3.0.1 and 3.0.0 work only on Android. Copying the contents of the Entitlements.entitlements file from 2.14.6 to the new one does not solve the problem, the error in xCode remains the same.

Steps to reproduce?

1. Install OneSignal SDK 3.0.1
2. Build for iOS on Windows

What did you expect to happen?

Build with correctrly Entitlements.entitlements file for iOS on Windows

Unity version

2021.3.0f1

OneSignal Unity SDK version

3.0.1, 3.0.0

Platform

Windows

Relevant log output

DirectoryNotFoundException: Could not find a part of the path "C:\Unity3d\MyProject\Builds\iOS\C:\Unity3d\MyProject\Builds\iOS\Entitlements.entitlements".
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <6073cf49ed704e958b8a66d540dea948>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) (at <6073cf49ed704e958b8a66d540dea948>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions)
System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize) (at <6073cf49ed704e958b8a66d540dea948>:0)
System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding) (at <6073cf49ed704e958b8a66d540dea948>:0)
(wrapper remoting-invoke-with-check) System.IO.StreamWriter..ctor(string,bool,System.Text.Encoding)
System.IO.File.WriteAllText (System.String path, System.String contents, System.Text.Encoding encoding) (at <6073cf49ed704e958b8a66d540dea948>:0)
UnityEditor.iOS.Xcode.PlistDocument.WriteToFile (System.String path) (at C:/buildslave/unity/build/External/XcodeAPI/Xcode/PlistParser.cs:404)
UnityEditor.iOS.Xcode.ProjectCapabilityManager.WriteToFile () (at C:/buildslave/unity/build/External/XcodeAPI/Xcode/ProjectCapabilityManager.cs:76)
OneSignalSDK.BuildPostProcessor.AddProjectCapabilities () (at Library/PackageCache/com.onesignal.unity.ios@3.0.1/Editor/BuildPostProcessor.cs:158)
OneSignalSDK.BuildPostProcessor.OnPostprocessBuild (UnityEditor.Build.Reporting.BuildReport report) (at Library/PackageCache/com.onesignal.unity.ios@3.0.1/Editor/BuildPostProcessor.cs:105)
UnityEditor.Build.BuildPipelineInterfaces+<>c__DisplayClass18_0.<OnBuildPostProcess>b__1 (UnityEditor.Build.IPostprocessBuildWithReport bpp) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEditor.Build.BuildPipelineInterfaces.InvokeCallbackInterfacesPair[T1,T2] (System.Collections.Generic.List`1[T] oneInterfaces, System.Action`1[T] invocationOne, System.Collections.Generic.List`1[T] twoInterfaces, System.Action`1[T] invocationTwo, System.Boolean exitOnFailure) (at <780782bc035845f9909cebbd4c983ae3>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Code of Conduct

jkasten2 commented 2 years ago

@SergeiEfimik Thanks for reporting, I can confirm this issue on Windows when using Unity's build iOS option. This is not an issue on macOS.

We have a fixed this issue and it will be included in the next update in PR #493. In the mean time you can work around the issue by building on macOS.

SergeiEfimik commented 2 years ago

Thanks for reply!

SergeiEfimik commented 2 years ago

The problem is still on the version 3.0.2. The build on iOS runs correctly, the Entitlements.entitlements file is created as needed. But when publishing in xCode - error with code 90206. Error 90206 is solved by setting "Always Embed Swift Standard Libraries" to "true" for Unity-iPhone project and "false" value for UnityFramework and OneSignalNotificationServiceExtension.

jkasten2 commented 2 years ago

@SergeiEfimik Thanks for reporting this, we addressed this PR #501 so the "Always Embed Swift Standard Libraries" should be set to false for all targets by default now in the OneSignal-Unity-SDK 3.0.2. I would recommend doing a clean build from Unity as well as Xcode ensure the old setting is not carrying over.

SergeiEfimik commented 2 years ago

@jkasten2 Maybe you have a version of BuildPostProcessor.cs where the problem with the entitlements file for building on Windows for iOS target is solved?

jkasten2 commented 2 years ago

@SergeiEfimik Can you double check the iOS part of the OneSignal SDK is on 3.0.2 as well as core? image

If this is all up to date can you include the exact version of Unity you are using?

I have confirmed the issue no longer happens on Unity 2020.3.30f1 on Windows 11.

SergeiEfimik commented 2 years ago

@jkasten2 Windows 10 Pro 21H2, Unity 2021.3.2 LTS, External Dependency Manager 1.2.171. Before installing OneSignal, I deleted folders Library, obj, Logs from project directory. Installed with scoped registries. Also tried installing OneSignal via unitypackage - same error for version 3.0.0, 3.0.1, 3.0.2. All the same, but with version 2.14.6 - build complete without entitlements error. I checked also on an empty project - build complete without errors, but Entitlements.entitlements file in build directory not created.

SergeiEfimik commented 2 years ago

I think I've found the source of the problem. Apparently there is a conflict with the code responsible for native authorization through AppleID. My project has this script:

#if UNITY_IOS || UNITY_TVOS
#define UNITY_XCODE_EXTENSIONS_AVAILABLE
#endif

using UnityEditor;
using UnityEditor.Callbacks;
using AppleAuth.Editor;
#if UNITY_XCODE_EXTENSIONS_AVAILABLE
using UnityEditor.iOS.Xcode;
#endif

namespace MyProject.PostBuild
{
    public static class SignInWithApplePostprocessor
    {
        [PostProcessBuild(1)]
        public static void OnPostProcessBuild(BuildTarget target, string path)
        {
            if (target == BuildTarget.iOS || target == BuildTarget.tvOS)
            {
#if UNITY_XCODE_EXTENSIONS_AVAILABLE
                    var projectPath = PBXProject.GetPBXProjectPath(path);
#if UNITY_2019_3_OR_NEWER
                        var project = new PBXProject();
                        project.ReadFromString(System.IO.File.ReadAllText(projectPath));
                        var manager = new ProjectCapabilityManager(projectPath, "Entitlements.entitlements", null, project.GetUnityMainTargetGuid());
                        manager.AddSignInWithApple();
                        manager.WriteToFile();
#else
                        var manager = new ProjectCapabilityManager(projectPath, "Entitlements.entitlements", PBXProject.GetUnityTargetName());
                        manager.AddSignInWithAppleWithCompatibility();
                        manager.WriteToFile();
#endif
#endif
            }
            else if (target == BuildTarget.StandaloneOSX)
            {
                AppleAuthMacosPostprocessorHelper.FixManagerBundleIdentifier(target, path);
            }
        }
    }
}

As I wrote above, when importing OneSignal into an empty project, the "Entitlements.entitlements" file is not created in the root directory. Instead, a "OneSignalNotificationServiceExtension.entitlements" file is created in the "OneSignalNotificationServiceExtension" folder. But I can't figure out why there is a conflict between the entitlements files in my project with the script above.

SergeiEfimik commented 2 years ago

I replase [PostProcessBuild(1)] index 1 on int.MaxValue: [PostProcessBuild(int.MaxValue)] - build complete without errors. But this is in an empty project with imported AppleAuth from my project. In my source project, changing the index did not fix the error. I will continue to try and look for more options.

jkasten2 commented 2 years ago

@SergeiEfimik

iOS build error on Windows

Thanks for the Unity version. With the OneSignal SDK 3.0.2 do you see a different error when building on Windows now? Do you only get an error when you add your SignInWithApplePostprocessor? Can you share your new full error as well?

Could you share where you got the code for SignInWithApplePostprocessor from? Or the documentation you used to create it if it is something you created?

Error 90206 is solved by setting "Always Embed Swift Standard Libraries"

Are you still seeing this error on OneSignal 3.0.2? If so are you using the same Unity version on macOS to build? Also what version of Xcode are you using?

SergeiEfimik commented 2 years ago

@jkasten2 About 90206: yes, error still in version 3.0.2 + xCode 13.1 (13A1030d). I tried to build the project both on one platform (Windows and Mac) on target iOS - result is the same (same settings "Always Embed Swift Standard Libraries"). Should "Enable Swift Framework Support Workaround" checkbox be enabled in iOS Resolver Settings (External Dependency Manager 1.2.171)?

About Entitlements. Found out the cause of the problem. I am using Mobile Notifications Unity-package to get DeviceToken on iOS. If disable push notification in project settings, then build on Windows complete correctly. image At the same time, notifications from OneSignal received as expected.

But now I can't get DeviceToken (required "Enable Push Notification").

pettersaiteppeland commented 2 years ago

I get the same issue, when having another PostProcessor script running prior to the OneSignal PostProcessor. I think the issue lies in the GetEntitlementsPath method. It returns the fullpath to the entitlement file when the file already exists (which is the case when there is another PostProcessor adding this file). bilde

Then ProjectCapabilityManager.WriteToFile combines this fullpath with the buildpath leading to a faulty path. bilde

A temporary fix for me is to make sure that this PostProcessor script is running first by changing the callbackOrder on my other PostProcessor script.

SergeiEfimik commented 2 years ago

@pettersaiteppeland, I wrote above that I changed callbackOrder from 1 to int.MaxValue, but this did not solve my problem. Perhaps when Mobile Notifications are enabled (see screenshot above), Unity automatically creates an entitlement file before OneSignal is processed.

pettersaiteppeland commented 2 years ago

@SergeiEfimik Strange, you could try to attach a breakpoint in AddProjectCapabilities and confirm that the entitlement file already exists or not. When it does not exist, it should work.

SergeiEfimik commented 2 years ago

@pettersaiteppeland How to do it right? My VS doesn't switch to Degug mode while building a project in Unity.

image

pettersaiteppeland commented 2 years ago

@SergeiEfimik I am not familiar with VS unfortunately, Rider is my IDEA. When the debugger is attached, the build stops at breakpoints. Hmm, you could embed the package and add custom log entries to find more information perhaps?

SergeiEfimik commented 2 years ago

@pettersaiteppeland It turned out to start the debug mode. At OneSignal build time, there is already a game.entitlements file.

image

image

When Unity Mobile Notifications are disabled, file "game.entitlements" is not created.

pettersaiteppeland commented 2 years ago

@SergeiEfimik Yes, we are in the same boat then. GetEntitlementsPath is returning the fullpath which is not working with ProjectCapabilityManager.WriteToFile.