In React Native Library How Can I get ios current UIViewController - react-native

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

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

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)

Related

Xcode objective C: How Clean code from UIWebView (ITMS-90809)

I use Windev mobile to developpe apps for both Android and IOS. For IOS apps, Windev mobile gave me an objective-c project that i should build in xcode then send to app store ..
I'm know only basics of basics step on xcode and i need to clean my code from "UIWebView", I have seen more than discussion about that but none of them repond to my case ..
For exemple thay talk about using: grep -r UIWebView project path .
But i realy dont know how to use that ...
Please your help will be very very appreciated !
Thanks!
Really just some guidance - how to do it actually depends on your situation.
Don't use grep - just use your IDE's search to locate all the UIWebView and then replace them with WKWebView. It can be as simple as that depending on what you use your webview's for. If for complex processing then you'll need to figure out how to do what you did in UIWebView in the WKWebView.
Another suggestion - why not wrap all your UIWebViews in some custom class and move all the functionality in there. Then it is easy to replace the UIWebView there and also rework the functionality there with the class staying the same. Apart from the functionality you'll have code that looks something like this in there.
+ ( UIWebView / WKWebView * ) addWebViewToView:( UIView * ) view
{
// Code to create webview - initially UIWebView and later WKWevView
..
// Code to embed the webview into the view
.. set the constraints to fill the view
return webview;
}
Then you can use normal UIView's in your project and use this to prime them with a webview.
To this you can later add navigation delegates to beef up the functionality or, if you just use the webview to display some HTML, you can pass in the string or URL to display.

React Native onesignal

IM trying to use onesignal with react native. I linked everything on android and ios. Everything is working fine on ios but I get undefined is not an object (evaluating RNOneSignal.configure).
and I just added this line: OneSignal.configure({});
on the AppDelegate.m put the #synthesize oneSignal = _oneSignal; statement between the #implementation block and the #end block. I was putting it after #end and I was having the same problem. Hope it helps.
I had the same issue. In my way the reason was this library was not linked correctly. Check this strings in file android/app/src/main/java/YOUR_APP_FOLDER/MainApplication.java:
import com.geektime.rnonesignalandroid.ReactNativeOneSignalPackage;
before first method and
new ReactNativeOneSignalPackage()
inside "return Arrays.asList("

Calling Google Plus Sign In inside of NSObject class rather than ViewController in IOS

I am trying to define methods for user login in my IOS app with Google+. I would like to do this in separate utility class rather than view controller (because I will expose these components to native react.js later).
My class header will look like following:
#interface LoginUtility : NSObject<GPPSignInDelegate>
+ (void)googleLogin
However, "GPPSignInDelegate" does not work inside NSObject class. After I implement like that, I start getting linker error like:
"_OBJC_CLASS_$_CMMotionManager", referenced from: objc-class-ref in GooglePlus(GPPSpamSignal.o)".
Note: I added all dependency libraries.
When I put the same code in ViewController, it just works fine. I also tried same logic with Facebook login and it worked perfectly.
Why is Google enforcing sign in method to be called inside ViewController?
Are there any ways to solve this problem?
Thanks in advance!
CMMotionManager is part of the CoreMotion framework. You obviously haven't linked that in correctly.

How my react js code can interact with existing Obj-C logic?

I have existing Obj-C project with rich business logic. I wanna try using React Native on the particular screen (I mean View Controller in terms of Cocoa), but every example I see in repo contains logic in javascript. How can I treat React Native as rendering, but pass user actions to my Objective-C code?
EDIT 31 march 2015:
Native view module does not seem to be a good solution, because native modules get instanciated from React code. Thus if I want to use already created view model for that view controller, I need to have some singleton, which will be like a some shared state on the side. I think this is bad.
It's not possible for React views to call native methods directly without going through JavaScript, unless you create custom native view plugins for literally everything onscreen.
Your best bet is probably to create a custom native module that exports all the native methods you want to call, then write a minimal React JavaScript application that does nothing except forward touch events from the views to your module by calling those methods.
If you need to communicate back to the JS application, your module can either use callbacks passed to your exported methods, or broadcast events which the JS code can observe.
To get the most out of React Native though, I'd recommend that you try to keep all the view and controller logic in the JS part, and only expose the business logic from the native side. Otherwise you lose all the benefits of rapid reloading to test changes, etc.
I have the same problem and what I did to solve it:
Create Native Module wrapper around NSNotificationCenter, so javascript code could publish iOS events.
Subscribe for that event within ReactController-wrapper around React-Native code.
Raise event when user clicked button (when we need to give control back to objective-c) with needed data to pass as dictionary.
Catch event from ReactController-wrapper (objective-c), process data, open other controllers/etc.
Using React Native within existing iOS app for some views only
Have a look at the documentation:
http://facebook.github.io/react-native/docs/nativemodulesios.html#content
React Native provides "Native Modules" as a way of bridging between Obj-C and JavaScript. There's also a little bit more about this on the React Native homepage:
http://facebook.github.io/react-native/
Under the "Extensibility" heading, it shows how to expose custom iOS views.
IF you implement RCTRootView in your view controller you can use it to access RTCBridge and then from this gain access to your module.
This will allow you to keep a reference of your view controller inside your module, making you able to pass calls to method directly instead of relying on NSNotifications.
example :
In your view controller
let reactView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "MyReactApp", initialProperties: nil, launchOptions: nil)
self.reactBridge = reactView.bridge
let myModule = self.reactBridge.module(for: MYModuleClass.self) as! MYModuleClass
myModule.viewController = self
And in your module keep the reference :
#objc(MYModuleClass)
class MYModuleClass: NSObject {
weak var viewController: MyViewController!
}

MKMapView night mode like in iOS7 Apple Maps?

Is there an easy way to enable night mode on a MKMapView like the Apple Maps apps does when the sun is down ?
I didn't find such a thing the MapKit documentation.
Unfortunately it is a private api:
//add this above the interface of the class you want to try it on
#interface MKMapView ()
-(void) _setShowsNightMode:(BOOL)yesOrNo;
#end
//call this in your viewDidLoad or somewhere else appropriate
[self.mapView _setShowsNightMode:YES];
We should file a radar for apple to open this up to everyone. It could be a safety issue for people using maps in third party apps while driving. Your app will be rejected if you try and submit it to the app store using this.
No, you'd have to use a third-party library like Mapbox iOS SDK or MBXMapKit.