invertase / react-native-apple-authentication

A React Native library providing support for Apple Authentication on iOS and Android.
Other
1.42k stars 223 forks source link

Support for VisionOS #350

Closed j6alvarez closed 2 months ago

j6alvarez commented 3 months ago

I have been doing some work with this repo https://github.com/callstack/react-native-visionos

And wanted to contribute sharing what made it work for me following the instructions in this post

Here is the needed code for it to run appleAuth methods for react-native-visionos

Steps:

  1. On RNAppleAuthentication.podspec Add visionos support

s.platforms = { :ios => "9.0", :osx => "10.15", :visionos => "1.0" }

  1. Run bundle install and then bundle exec pod install. Then run build on xcode you'll find some errors

  2. To fix the errors, look for this file RNAppleAuthASAuthorizationDelegates.m Add these imports

    #import <UIKit/UIKit.h>
    #import <AuthenticationServices/AuthenticationServices.h>

Find this if

#if TARGET_OS_OSX
    return [[NSApplication sharedApplication] keyWindow];
#else
    return [[UIApplication sharedApplication] keyWindow];
#endif

And change it for this

#if TARGET_OS_OSX
    return [[NSApplication sharedApplication] keyWindow];
#else
    #if TARGET_OS_VISION
        // visionOS
        UIWindow *window = nil;
        NSSet<UIScene *> *connectedScenes = [UIApplication sharedApplication].connectedScenes;
        for (UIScene *scene in connectedScenes) {
            if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {
                UIWindowScene *windowScene = (UIWindowScene *)scene;
                window = windowScene.windows.firstObject;
                break;
            }
        }
        return window;
    #else
        return [[UIApplication sharedApplication] keyWindow];
    #endif
#endif
mikehardy commented 2 months ago

Happy to take a PR here if you want to make one? Seems very reasonable

j6alvarez commented 2 months ago

Done in #352, thanks!