# Custom URL redirect from SFSafariViewController to UE4 app? ...FOnOpenURL delegate?

**URL:** <https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607>\
**Category:** Mobile\
**Tags:** unreal-engine, iOS\
**Created:** [November 13, 2020, 11:27pm UTC](https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607 "2020-11-13T23:27:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mainframenzo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mainframenzo](https://forums.unrealengine.com/u/mainframenzo)\
**Post date:** [November 13, 2020, 11:27pm UTC](https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607/1 "2020-11-13T23:27:29Z")

</div>

Hi folks,

I am trying to do a redirect from an HTML/JS page that is loaded inside a SFSafariViewController back to my UE4 app using a custom uri scheme. I am using this [How to use SafariViewcontroller as Callback on some URL in Objective C - Mobikul](https://mobikul.com/use-safariviewcontroller-callback-url-objective-c/) more or less. The redirect is supposed to close the SFSafariViewController w/ additional information passed back in query params.

I can not get the redirect to work and/or SFSafariViewController does not close. I have validated that the uri scheme is defined correctly (in project settings additional plist data) by using Safari dev tools on my computer to invoke Javascript in the SFSafariViewController in my deployed app on the iOS device. Trying to redirect to a url w/ \*window.location.href = ‘testing://?success=1’ \*, which is different than defined uri scheme, gives a “Safari cannot open the page because the address is invalid”, which is expected, and when redirecting to the url w/ _window.location.href = ‘freedomofkeima://?success=1’_, I do not see this error, but I also do not see the SFSafariViewController close.

Does anyone know how to handle custom uri schemes? I am unsure if the delegate FOnOpenURL in [UnrealEngine/Engine/Source/Runtime/Core/Public/IOS/IOSAppDelegate.h at master · raysjoshua/UnrealEngine · GitHub](https://github.com/raysjoshua/UnrealEngine/blob/master/Engine/Source/Runtime/Core/Public/IOS/IOSAppDelegate.h) would be available to me or can be used (new to c++), and I am also not sure if the code from the example link in my app is modified correctly.

My additional plist data (my bundle id is com.mypackage.mygame):

```auto

<key>LSApplicationQueriesSchemes</key><array><string>freedomofkeima</string></array><key>CFBundleURLTypes</key><array><dict><key>CFBundleURLSchemes</key><array><string>freedomofkeima</string></array><key>CFBundleURLName</key><string>com.mypackage.mygame</string></dict></array><key>UIRequiresPersistentWiFi</key><true/>

```

My blueprint, which is run on game start:

BrowserBlueprint.cpp:

```auto

#include "BrowserBlueprint.h"
#include "Runtime/Engine/Classes/Kismet/KismetSystemLibrary.h"
#include "Engine/GameEngine.h"
#include "BrowserSettings.h"
#include "EngineUtils.h"
#include "Engine/World.h"

#if PLATFORM_IOS
#include "MyBrowserController.h"
#endif

UBrowserBlueprint::UBrowserBlueprint(const FObjectInitializer &ObjectInitializer) : Super(ObjectInitializer) {}

void UBrowserBlueprint::loadBrowserWithUrl() {
#if PLATFORM_IOS
  [MyBrowserController loadBrowserWithUrl];
#endif
}

```

MyBrowserController.h:

```auto

#pragmaonce

#if PLATFORM_IOS
#import <Foundation/Foundation.h>
#import <SafariServices/SafariServices.h>

@interface MyBrowserController : UIViewController <SFSafariViewControllerDelegate>
+ (void)loadBrowserWithUrl;

@end
#endif

```

MyBrowserController.cpp

```auto

#include "MyBrowserController.h"
#include "BrowserSettings.h"

#if PLATFORM_IOS
#include "IOSAppDelegate.h"
#import <Foundation/Foundation.h>
#import <SafariServices/SafariServices.h>
#endif

#if PLATFORM_IOS
@interface MyBrowserController()
@end

@implementation MyBrowserController

+ (MyBrowserController*)GetDelegate {
  static MyBrowserController *Singleton = [MyBrowserController alloc] init];

  return Singleton;
}

+ (void)loadBrowserWithUrl {
  [MyBrowserController GetDelegate] performSelectorOnMainThread:@selector(loadBrowserURLInIOSThread) withObject:nil waitUntilDone:NO];
}

- (void)loadBrowserURLInIOSThread {
  const UBrowserSettings *browserSettings = GetDefault<UBrowserSettings>();

  NSString *oUrlString = browserSettings->url.GetNSString();
  NSString *urlString = [NSString alloc] initWithString:UrlString];
  NSURL *url = [NSURL URLWithString:urlString];

  SFSafariViewController *safariVC = [SFSafariViewController alloc] initWithURL:url entersReaderIfAvailable:NO];
  safariVC.delegate = self;

  UINavigationController *navigationController = [UINavigationController alloc]initWithRootViewController:safariVC];
  [navigationController setNavigationBarHidden:YES animated:NO];

  [IOSAppDelegate GetDelegate].IOSController presentViewController:navigationController animated:YES completion:nil];
}

#pragma mark - SFSafariViewController delegate methods

-(void)safariViewController:SFSafariViewController *)controller didCompleteInitialLoad:BOOL)didLoadSuccessfully {
  //Load finished
}

-(void)safariViewControllerDidFinish:SFSafariViewController *)controller {
  //Done button pressed
}

- (BOOL)application:UIApplication *)app openURL:NSURL *)url options:NSDictionary<NSString *, id> *)options {
  NSString *sourceApplication = [options objectForKey:@"UIApplicationOpenURLOptionsSourceApplicationKey"];
  NSString *urlScheme = [url scheme];
  NSString *urlQuery = [url query];

  if ([urlScheme isEqualToString:@"freedomofkeima"] && [sourceApplication isEqualToString:@"com.apple.SafariViewService"]) {

    //TODO check for success query param

    NSLog(@"Value: %@", urlQuery);
    NSDictionary *data = [NSDictionary dictionaryWithObject:urlQuery forKey:@"key"];

    [NSNotificationCenter defaultCenter] postNotificationName:@"SafariCallback" object:self userInfo:data];

    return YES;
  }

  return NO;
}

- (void)safariCallback:NSNotification *)notification {
  [IOSAppDelegate GetDelegate].IOSController dismissViewControllerAnimated:NO completion:nil];
}

@end
#endif

```

---

<div class="post-metadata">

**Author:** ![shive\_1807](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shive\_1807](https://forums.unrealengine.com/u/shive_1807)\
**Post date:** [May 31, 2024, 1:51pm UTC](https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607/2 "2024-05-31T13:51:28Z")

</div>

Hey Man, I know its been long but need to ask, I solved it in android, did you get any luck solving it for IOS ?

---

<div class="post-metadata">

**Author:** ![tlemsani-achref](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tlemsani-achref](https://forums.unrealengine.com/u/tlemsani-achref)\
**Post date:** [June 5, 2024, 9:01pm UTC](https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607/3 "2024-06-05T21:01:04Z")

</div>

hello , i am stuck in getting the redirect url for android could you pleas help me ?

i to make login functionality using OAuth2 so i send the player to the login page and after he login the api will send him back to the game using redirect uri that contain AuthorizationCode wich i need it for login him

---

<div class="post-metadata">

**Author:** ![sidunreal](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@sidunreal](https://forums.unrealengine.com/u/sidunreal)\
**Post date:** [June 6, 2024, 10:02am UTC](https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607/4 "2024-06-06T10:02:22Z")

</div>

I am struggling with getting app uri in android as well, I want to get app redirect working could you please help?

---

<div class="post-metadata">

**Author:** ![shive\_1807](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shive\_1807](https://forums.unrealengine.com/u/shive_1807)\
**Post date:** [June 17, 2024, 9:36am UTC](https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607/5 "2024-06-17T09:36:19Z")

</div>

> [@sidunreal](#):
>
> you please help

Hey Just saw your questions,

Here is the solution of how to open the callback URL in android, so my call back url is of type,

```auto

#if PLATFORM_ANDROID
#include <jni.h>                            
#include "Android/AndroidApplication.h"
#include "Android/AndroidJavaEnv.h"
#include "Android/AndroidJava.h"
#include "Android/AndroidJNI.h" // For FJavaWrapper
#endif

**static** jmethodID javaMethodId;

FString ABeyondLoginSystemLatest::GetAccessTokenFromIntent(){

JNIEnv* env = FAndroidApplication::GetJavaEnv();

javaMethodId = FJavaWrapper::FindMethod(env, FJavaWrapper::GameActivityClassID, "GetAccessTokenIntent", "()Ljava/lang/String;", **false** );

jstring result = (jstring)FJavaWrapper::CallObjectMethod(env, FJavaWrapper::GameActivityThis, javaMethodId);

**const**  **char*** str = env->GetStringUTFChars(result, **NULL** );

FString FStringResult(str);

AccessToken = FStringResult;

Success = **true** ;

**return** FStringResult;

}

```

the above code is you put in your actor class, Search for the following file in you UE project file.

Now create a UPL android class in your project, and add the following code.

```
    Uri data = this.getIntent().getData();
    if (data != null){
        if(data.isHierarchical()){
            access_token = data.getQueryParameter("access_token");
            refresh_token = data.getQueryParameter("refresh_token");
            PrintAccessToken();
        }
    }
   </insert>

```

the above code will fetch the data from the url scheme.

 public static String refresh\_token; public static String access\_token;

```
    public String GetAccessTokenIntent(){
        return access_token;
    }

    public String GetRefreshTokenIntent(){
        return refresh_token;
    }
    
  	</insert>

```

when calling the methods shared above it calles the above funtion to get the data.

---

<div class="post-metadata">

**Author:** ![sidunreal](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@sidunreal](https://forums.unrealengine.com/u/sidunreal)\
**Post date:** [July 23, 2024, 5:14am UTC](https://forums.unrealengine.com/t/custom-url-redirect-from-sfsafariviewcontroller-to-ue4-app-fonopenurl-delegate/153607/6 "2024-07-23T05:14:37Z")

</div>

Thank you.
