3

react native library in android can use "getCurrentActivity()" to replace "this"

final Activity activity = getCurrentActivity();

And In ios How can use to replace "self" to get current UIViewController

2
  • Hi @kiel. Have you fixed this instead of using rootViewController. Commented May 14, 2020 at 17:31
  • I am facing the same issue in iOS. But i don't want to set the rootViewController. becase when i click back to moves to root view itself. Commented May 14, 2020 at 17:32

2 Answers 2

8

For "current UIViewController" you most probably needs RCTPresentedViewController function from RCTUtils.h file:

#import <React/RCTUtils.h>

// ...
  UIViewController* vc = RCTPresentedViewController();
// ...

Note: Very old RN versions do not has this API, but it can be easy implemented by hands, implementation is easy enough: RCTUtils.m

Sign up to request clarification or add additional context in comments.

Comments

0

On iOS - unless you use wix's react native navigation - there is generally only one UIViewController, instantiated in your AppDelegate.

1st question: Why do you need your app UIViewController ?

if you really need to access it, here is a suggestion : UIApplication.sharedApplication().window.rootViewController (this code might not work for complex native setup, but for simple cases, it should give you your app ViewController)

1 Comment

Because I have a function will popup a view and i need to pass my current view. In android i pass getActivity() is work but in ios i pass "self" is return error (Could not cast value of type MyLibrary to 'UIViewController'.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.