Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track Mac installations on Posthog #1737

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions Nos/Extensions/Bundle+Current.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Security

private class CurrentBundle {}

Expand Down Expand Up @@ -33,16 +34,63 @@ extension Bundle {
/// Checks the app's receipt URL to determine if it contains the TestFlight-specific
/// "sandboxReceipt" identifier.
/// - Returns: `true` if the app was installed through TestFlight, `false` otherwise.
private var isTestFlight: Bool {
private var isIosTestFlight: Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe have a single isTestFlight and inside of it, check whether we're on iOS or macOS? Essentially combine isIosTestFlight and isMacTestFlight into one?

Additionally, I wonder if we need to check whether it's macOS first? https://gist.github.com/lukaskubanek/cbfcab29c0c93e0e9e0a16ab09586996?permalink_comment_id=4686545#gistcomment-4686545

Copy link
Contributor Author

@pelumy pelumy Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @joshuatbrown. I will do the refactoring. I will also make sure to check for macOS first before iOS.

Do you also mean including comments about how to test it in the code?
Currently, to test, we need to merge the code, install the app on the Mac through TestFlight and see if it shows up in the downloads graph on post hog.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no -- I just mean I'm hoping for a way to test like by setting a breakpoint, but I don't know if that's possible. We may just have to deploy to TestFlight and test that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can test with the breakpoint, I was able to test the iOS one after merging.

Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
}

/// Returns whether the bundle was signed for TestFlight beta distribution by checking
/// the existence of a specific extension (marker OID) on the code signing certificate.
///
/// This routine is inspired by the source code from ProcInfo, the underlying library
/// of the WhatsYourSign code signature checking tool developed by Objective-See. Initially,
/// it checked the common name but was changed to an extension check to make it more
/// future-proof.
///
/// For more information, see the following references:
/// - https://github.com/objective-see/ProcInfo/blob/master/procInfo/Signing.m#L184-L247
/// - https://gist.github.com/lukaskubanek/cbfcab29c0c93e0e9e0a16ab09586996#gistcomment-3993808
private var isMacTestFlight: Bool {
#if os(macOS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I just found this in our NoteUITextViewRepresentable:

            if ProcessInfo.processInfo.isiOSAppOnMac {

I think this is the way to detect whether we're running on macOS or not. I think the #if os(macOS) only works if we have a real Mac app, or maybe if we have a Catalyst app running on Mac.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is going to be a problem then, because the mac code doesn't work (throws errors) except there is #if os(macOS). It has to actually run on a Mac.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. I'll approve, we can merge and test, then decide next steps if needed.

var status = noErr

var code: SecStaticCode?
status = SecStaticCodeCreateWithPath(bundleURL as CFURL, [], &code)

guard status == noErr, let code = code else { return false }

var requirement: SecRequirement?
status = SecRequirementCreateWithString(
"anchor apple generic and certificate leaf[field.1.2.840.113635.100.6.1.25.1]" as CFString,
[], // default
&requirement
)

guard status == noErr, let requirement = requirement else { return false }

status = SecStaticCodeCheckValidity(
code,
[], // default
requirement
)

return status == errSecSuccess
#else
return false
#endif
}

/// Returns the app's installation source: debug, TestFlight, or App Store.
var installationSource: InstallationSource {
#if DEBUG
return .debug
#else
return isTestFlight ? .testFlight : .appStore
#if os(iOS)
return isIosTestFlight ? .testFlight : .appStore
#elseif os(macOS)
return isMacTestFlight ? .testFlight : .appStore
#else
return .debug
#endif
#endif
}
}
Loading