Skip to content

Commit

Permalink
[firebase] Update sample and use one binding project
Browse files Browse the repository at this point in the history
The analytics and messaging native projects have been merged into a
single MauiFirebase Xcode project.  The sample has been updated to use
bound APIs from both the analytics and messaging SDKs.
  • Loading branch information
pjcollins committed Apr 23, 2024
1 parent 81a525d commit 51409ff
Show file tree
Hide file tree
Showing 33 changed files with 513 additions and 971 deletions.
58 changes: 34 additions & 24 deletions eng/Common.macios.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project>

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>

<XcodeDefaultBuildDir>.build</XcodeDefaultBuildDir>
<XcodeBuildDirName Condition=" '$(XcodeBuildDirName)' == '' ">$(XcodeDefaultBuildDir)</XcodeBuildDirName>

Expand All @@ -23,6 +25,7 @@
<_XcArchiveiOSFullPath>$([System.IO.Path]::Combine($(_XcodeBuildDirFullPath), $(XcodeScheme)-ios.xcarchive))</_XcArchiveiOSFullPath>
<_XcArchiveiOSSimulatorFullPath>$([System.IO.Path]::Combine($(_XcodeBuildDirFullPath), $(XcodeScheme)-iossimulator.xcarchive))</_XcArchiveiOSSimulatorFullPath>
<_XcArchiveMacCatalystFullPath>$([System.IO.Path]::Combine($(_XcodeBuildDirFullPath), $(XcodeScheme)-maccatalyst.xcarchive))</_XcArchiveMacCatalystFullPath>
<_XcArchiveExtraArgs>ENABLE_BITCODE=NO SKIP_INSTALL=NO SWIFT_INSTALL_OBJC_HEADER=YES BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_LDFLAGS='-ObjC' OTHER_SWIFT_FLAGS='-no-verify-emitted-module-interface' OBJC_CFLAGS='-fno-objc-msgsend-selector-stubs -ObjC'</_XcArchiveExtraArgs>

<_XcFrameworkFullPath>$([System.IO.Path]::Combine($(_XcodeBuildDirFullPath), $(XcodeScheme).xcframework))</_XcFrameworkFullPath>
</PropertyGroup>
Expand All @@ -34,22 +37,35 @@
<_XcodeProjectInputs Include="$(_XcodeProjectFullPath)/*.xcworkspace" />
</ItemGroup>

<Target Condition=" '$(XcodeBuildXCFramework)' == 'true' " DependsOnTargets="$(BuildXCFrameworkDependsOnTargets)" BeforeTargets="ProcessFrameworkReferences" Name="BuildXCFramework" Inputs="@(_XcodeProjectInputs)" Outputs="$(_XcFrameworkFullPath)/Info.plist">
<PropertyGroup Condition="$(TargetFramework.Contains('ios')) Or $(TargetFramework.Contains('maccatalyst'))">
<_GenerateBindingsDependsOn>
BuildXCFramework;
ObjSharpieBind;
$(_GenerateBindingsDependsOn);
</_GenerateBindingsDependsOn>
</PropertyGroup>

<Target Name="BuildXCFramework"
Condition=" '$(XcodeBuildXCFramework)' == 'true' "
DependsOnTargets="$(BuildXCFrameworkDependsOnTargets)"
Inputs="@(_XcodeProjectInputs)"
Outputs="$(_XcFrameworkFullPath)/Info.plist">
<Error Condition=" !Exists('$(_XcodeProjectFullPath)') " Text="Xcode project '$(_XcodeProjectFullPath)' not found." />

<Exec Condition=" '$(XcodeBuildiOS)' == 'True' " Command="xcodebuild -project $(_XcodeProjectFullPath) archive -scheme $(XcodeScheme) -configuration Release -archivePath $(_XcArchiveiOSFullPath) -destination 'generic/platform=iOS' -derivedDataPath $(_XcodeBuildDirFullPath) -IDECustomBuildProductsPath='' -IDECustomBuildIntermediatesPath='' ENABLE_BITCODE=NO SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_SWIFT_FLAGS='-no-verify-emitted-module-interface' OBJC_CFLAGS='-fno-objc-msgsend-selector-stubs -ObjC'" />
<Exec Condition=" '$(XcodeBuildiOSSimulator)' == 'True' " Command="xcodebuild -project $(_XcodeProjectFullPath) archive -scheme $(XcodeScheme) -configuration Release -archivePath $(_XcArchiveiOSSimulatorFullPath) -destination 'generic/platform=iOS Simulator' -derivedDataPath $(_XcodeBuildDirFullPath) -IDECustomBuildProductsPath='' -IDECustomBuildIntermediatesPath='' ENABLE_BITCODE=NO SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_SWIFT_FLAGS='-no-verify-emitted-module-interface' OBJC_CFLAGS='-fno-objc-msgsend-selector-stubs -ObjC'" />
<Exec Condition=" '$(XcodeBuildMacCatalyst)' == 'True' " Command="xcodebuild -project $(_XcodeProjectFullPath) archive -scheme $(XcodeScheme) -configuration Release -archivePath $(_XcArchiveMacCatalystFullPath) -destination 'generic/platform=macOS,variant=Mac Catalyst' -derivedDataPath $(_XcodeBuildDirFullPath) -IDECustomBuildProductsPath='' -IDECustomBuildIntermediatesPath='' ENABLE_BITCODE=NO SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_SWIFT_FLAGS='-no-verify-emitted-module-interface' OBJC_CFLAGS='-fno-objc-msgsend-selector-stubs -ObjC'" />
<Exec Condition=" '$(XcodeBuildiOS)' == 'True' " Command="xcodebuild -project $(_XcodeProjectFullPath) archive -scheme $(XcodeScheme) -configuration $(Configuration) -archivePath $(_XcArchiveiOSFullPath) -destination 'generic/platform=iOS' $(_XcArchiveExtraArgs)" />
<Exec Condition=" '$(XcodeBuildiOSSimulator)' == 'True' " Command="xcodebuild -project $(_XcodeProjectFullPath) archive -scheme $(XcodeScheme) -configuration $(Configuration) -archivePath $(_XcArchiveiOSSimulatorFullPath) -destination 'generic/platform=iOS Simulator' $(_XcArchiveExtraArgs)" />
<Exec Condition=" '$(XcodeBuildMacCatalyst)' == 'True' " Command="xcodebuild -project $(_XcodeProjectFullPath) archive -scheme $(XcodeScheme) -configuration $(Configuration) -archivePath $(_XcArchiveMacCatalystFullPath) -destination 'generic/platform=macOS,variant=Mac Catalyst' $(_XcArchiveExtraArgs)" />

<ItemGroup>
<XcodeBuiltFrameworks Condition=" '$(XcodeBuildiOS)' == 'True' " Include="$(_XcArchiveiOSFullPath)/Products/Library/Frameworks/$(XcodeScheme).framework" />
<XcodeBuiltFrameworks Condition=" '$(XcodeBuildiOSSimulator)' == 'True' " Include="$(_XcArchiveiOSSimulatorFullPath)/Products/Library/Frameworks/$(XcodeScheme).framework" />
<XcodeBuiltFrameworks Condition=" '$(XcodeBuildMacCatalyst)' == 'True' " Include="$(_XcArchiveMacCatalystFullPath)/Products/Library/Frameworks/$(XcodeScheme).framework" />
<_CreateXcFxArgs Include="-create-xcframework" />
<_CreateXcFxArgs Condition=" '$(XcodeBuildiOS)' == 'True' " Include="-archive $(_XcArchiveiOSFullPath) -framework $(XcodeScheme).framework" />
<_CreateXcFxArgs Condition=" '$(XcodeBuildiOSSimulator)' == 'True' " Include="-archive $(_XcArchiveiOSSimulatorFullPath) -framework $(XcodeScheme).framework" />
<_CreateXcFxArgs Condition=" '$(XcodeBuildMacCatalyst)' == 'True' " Include="-archive $(_XcArchiveMacCatalystFullPath) -framework $(XcodeScheme).framework" />
<_CreateXcFxArgs Include="-output $(_XcFrameworkFullPath)" />
</ItemGroup>

<RemoveDir Directories="$(_XcFrameworkFullPath)" />

<Exec Command="xcodebuild -create-xcframework @(XcodeBuiltFrameworks->'-framework %(Identity)', ' ') -output $(_XcFrameworkFullPath)" />
<Exec Command="xcodebuild @(_CreateXcFxArgs, ' ')" />
</Target>

<PropertyGroup>
Expand All @@ -61,24 +77,18 @@
<ItemGroup>
<ObjcBindingApiDefinitionFiles Include="$(ObjSharpieBindOutputDir)/ApiDefinitions.cs" />
<_ObjSharpieInputs Include="$(ObjSharpieSourceHeader)" />

</ItemGroup>

<Target Name="ObjSharpieBind" BeforeTargets="ProcessFrameworkReferences" DependsOnTargets="BuildXCFramework" Inputs="@(_ObjSharpieInputs)" Outputs="@(ObjcBindingApiDefinitionFiles)" Condition="'$(ObjSharpieBind)' == 'true'">

<Exec Command="xcodebuild -showsdks" ConsoleToMSBuild="true" Condition=" '$(ObjSharpieBindXcodeSdkName)' == '' ">
<Output TaskParameter="ConsoleOutput" PropertyName="_XcodeShowSdksOutput" />
</Exec>

<PropertyGroup>
<ObjSharpieBindXcodeSdkName Condition=" '$(ObjSharpieBindXcodeSdkName)' == '' ">$([System.Text.RegularExpressions.Regex]::Match($(_XcodeShowSdksOutput), `iphoneos[^; \n\r\t]+`))</ObjSharpieBindXcodeSdkName>
<ObjSharpieBindNamespace Condition=" '$(ObjSharpieBindNamespace)' == '' ">Binding</ObjSharpieBindNamespace>
</PropertyGroup>

<Target Name="ObjSharpieBind"
Condition="'$(ObjSharpieBind)' == 'true'"
Inputs="@(_ObjSharpieInputs)"
Outputs="@(ObjcBindingApiDefinitionFiles)">
<ItemGroup>
<ObjSharpieBindScopes Include="$(_XcArchiveiOSFullPath)/Products/Library/Frameworks/$(XcodeScheme).framework/Headers/" />
<_ObjSharpieArgs Include="--output=$(ObjSharpieBindOutputDir)" />
<_ObjSharpieArgs Include="--namespace=$(ObjSharpieBindNamespace)" />
<_ObjSharpieArgs Include="--framework $(_XcArchiveiOSFullPath)/Products/Library/Frameworks/$(XcodeScheme).framework" />
</ItemGroup>

<Exec Command="sharpie bind --sdk=$(ObjSharpieBindXcodeSdkName) --output=$(ObjSharpieBindOutputDir) --namespace=$(ObjSharpieBindNamespace) @(ObjSharpieBindScopes->'--scope=%(Identity)', ' ') $(ObjSharpieSourceHeader)" />
<Exec Command="sharpie bind @(_ObjSharpieArgs, ' ')" />
</Target>

</Project>
97 changes: 97 additions & 0 deletions firebase/macios/Firebase.MaciOS.Binding/ApiDefinitions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#nullable enable
using System;
using Foundation;

namespace Firebase
{
// @interface MauiFIRAnalytics : NSObject
[BaseType (typeof(NSObject))]
interface MauiFIRAnalytics
{
// +(void)logEventWithEventName:(NSString * _Nonnull)eventName parameters:(NSDictionary<NSString *,id> * _Nonnull)parameters;
[Static]
[Export ("logEventWithEventName:parameters:")]
void LogEvent (string eventName, NSDictionary parameters);

// +(void)getAppInstanceIdWithCompletion:(void (^ _Nonnull)(NSString * _Nullable))completion;
[Static]
[Export ("getAppInstanceIdWithCompletion:")]
[Async]
void GetAppInstanceId (Action<NSString?> completion);

// +(void)setUserIdWithUserId:(NSString * _Nonnull)userId;
[Static]
[Export ("setUserIdWithUserId:")]
void SetUserId (string userId);

// +(void)setUserPropertyWithPropertyName:(NSString * _Nonnull)propertyName value:(NSString * _Nonnull)value;
[Static]
[Export ("setUserPropertyWithPropertyName:value:")]
void SetUserProperty (string propertyName, string value);

// +(void)setSessionTimeoutWithSeconds:(NSInteger)seconds;
[Static]
[Export ("setSessionTimeoutWithSeconds:")]
void SetSessionTimeout (nint seconds);

// +(void)resetAnalyticsData;
[Static]
[Export ("resetAnalyticsData")]
void ResetAnalyticsData ();
}

// @interface MauiFIRApp : NSObject
[BaseType (typeof(NSObject))]
interface MauiFIRApp
{
// +(void)autoConfigure;
[Static]
[Export ("autoConfigure")]
void AutoConfigure ();

// +(void)configure:(NSString * _Nonnull)googleAppId gcmSenderId:(NSString * _Nonnull)gcmSenderId;
[Static]
[Export ("configure:gcmSenderId:")]
void Configure (string googleAppId, string gcmSenderId);
}

// @interface MauiFIRMessaging : NSObject
[BaseType (typeof(NSObject))]
interface MauiFIRMessaging
{
// +(BOOL)getIsAutoInitEnabled __attribute__((warn_unused_result("")));
// +(void)setIsAutoInitEnabled:(BOOL)enabled;
[Static]
[Export ("getIsAutoInitEnabled")]
bool IsAutoInitEnabled { get; [Bind("setIsAutoInitEnabled:")] set; }

// +(NSString * _Nullable)getFcmToken __attribute__((warn_unused_result("")));
[Static]
[NullAllowed, Export ("getFcmToken")]
string FcmToken { get; }

// +(void)register:(NSData * _Nonnull)apnsToken completion:(void (^ _Nonnull)(NSString * _Nullable, NSError * _Nullable))completion;
[Static]
[Export ("register:completion:")]
[Async]
void Register (NSData apnsToken, Action<NSString?, NSError?> completion);

// +(void)unregister:(void (^ _Nonnull)(NSError * _Nullable))completion;
[Static]
[Export ("unregister:")]
[Async]
void Unregister (Action<NSError?> completion);

// +(void)subscribe:(NSString * _Nonnull)topic completion:(void (^ _Nonnull)(NSError * _Nullable))completion;
[Static]
[Export ("subscribe:completion:")]
[Async]
void Subscribe (string topic, Action<NSError?> completion);

// +(void)unsubscribe:(NSString * _Nonnull)topic completion:(void (^ _Nonnull)(NSError * _Nullable))completion;
[Static]
[Export ("unsubscribe:completion:")]
[Async]
void Unsubscribe (string topic, Action<NSError?> completion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<IsBindingProject>true</IsBindingProject>
<NoBindingEmbedding>true</NoBindingEmbedding>

<XcodeProject>$(MSBuildThisFileDirectory)../native/analytics/MauiFirebaseAnalytics.xcodeproj</XcodeProject>
<XcodeProject>$(MSBuildThisFileDirectory)../native/MauiFirebase.xcodeproj</XcodeProject>
<XcodeBuildXCFramework>true</XcodeBuildXCFramework>
<ObjSharpieBind>False</ObjSharpieBind>
<ObjSharpieBind>true</ObjSharpieBind>
<ObjSharpieBindNamespace>Firebase</ObjSharpieBindNamespace>
</PropertyGroup>

<ItemGroup>
<ObjcBindingApiDefinition Include="ApiDefinitions.cs" />
<NativeReference Include="../native/analytics/.build/MauiFirebaseAnalytics.xcframework">
<NativeReference Include="../native/.build/MauiFirebase.xcframework">
<Kind>Framework</Kind>
<SmartLink>True</SmartLink>
<SmartLink>true</SmartLink>
</NativeReference>
</ItemGroup>

Expand Down
54 changes: 0 additions & 54 deletions firebase/macios/FirebaseAnalytics.Binding/ApiDefinitions.cs

This file was deleted.

38 changes: 0 additions & 38 deletions firebase/macios/FirebaseMessaging.Binding/ApiDefinitions.cs

This file was deleted.

This file was deleted.

23 changes: 18 additions & 5 deletions firebase/macios/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# MAUI
A set of examples for slim binding native libraries for use with .NET MAUI apps
# Firebase Slim Binding
This folder contains a slim binding for the Firebase SDK which demonstrates simple [Analytics][0] and [Messaging][1] usage.

### Build and Run
```shell
dotnet build sample -t:Run -f net8.0-ios
```

## Testing
Add your GoogleService-Info.plist to the sample project and change <ApplicationId> to your
iOS app identifier in the Sample.csproj
### Configure
The included sample requires some modification to fully function. You will need to log in to
a Firebase developer account and configure an app to interface with this sample.
For more details, reference the [Get Started (iOS)][2] page.

1. Download your `GoogleService-Info.plist` and replace `Platforms/iOS/GoogleService-Info.plist` with it.
2. Change the `<ApplicationId>` value in `Sample.csproj` to your Firebase iOS app identifier.


[0]: https://firebase.google.com/docs/analytics/get-started?platform=ios
[1]: https://firebase.google.com/docs/cloud-messaging/ios/client
[2]: https://firebase.google.com/docs/ios/setup
3 changes: 3 additions & 0 deletions firebase/macios/native/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.build/
build/
deps/
Loading

0 comments on commit 51409ff

Please sign in to comment.