How to call native iOS viewController' code when in RN?
Like use RN to push to a native iOS viewController and then let the native code do the job.
You need to use Export Method .
AppDelegate.h
#import <UIKit/UIKit.h>
#import "RCTBridgeModule.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate,RCTBridgeModule>
#property (nonatomic, strong) UIWindow *window;
#end
AppDelegate.m
#implementation AppDelegate
RCT_EXPORT_MODULE()
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//NO Change Here
}
RCT_EXPORT_METHOD(pushVC:(NSString *)vcName){
Class ctrlClass = NSClassFromString(vcName);
UIViewController *newVc = [[ctrlClass alloc] initWithNibName: vcName bundle: nil];
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:newVc animated:YES completion:nil];
}
You can call above response in javascript like this:
import { NativeModules } from 'react-native'
NativeModules.AppDelegate.pushVC('viewControllerNameHere')
But I highly recommend to make with external file to bridge like documentation explain here:
https://facebook.github.io/react-native/docs/native-modules-ios
Related
My ViewController instance is connected to my AppDelegate like this:
AppDelegate.h
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, retain) IBOutlet ViewController *myVC;
synthesized in AppDelegate.m
ViewController.m
#import "AppDelegate.h"
#interface ViewController ()
#end
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.myVC = self;
}
In the AppDelegate I am using the method:
- (BOOL) application:(UIApplication *)application openURL:(NSURL *)
url sourceApplication:(NSString *) sourceApplication annotation:(id)
annotation
while processing the URL further I have to call a method of the current instance of my ViewController like:
[myVC showResultsOfProcessedURL:url];
This works fine when the App is running but it won't be called when the App is being freshly launched (not just became active again).
I see that the AppDelegate can't know about the ViewController instance yet. But I don't know how to make him know.
I'm messing around with Rdio's iOS SDK. I've set up everything correctly in "Getting Started" outlined here (http://www.rdio.com/developers/docs/libraries/ios/). The track key in the app delegate works and plays after I launch the app.
Now I'm trying to get a simple UIButton click to play a track, and I can't for the life of me get it to work.
I have this in ViewController.h
#import <UIKit/UIKit.h>
#import <Rdio/Rdio.h>
#interface ViewController : UIViewController <RdioDelegate, RDPlayerDelegate>
#property (readonly) Rdio *rdio;
#end
And in ViewController.m
- (IBAction)playButton:(UIButton *)sender
{
[self.rdio preparePlayerWithDelegate:nil];
NSArray *sources = [NSArray arrayWithObjects:#"t1", #"p1", #"a1", nil];
[self.rdio.player playSources:sources];
}
I really appreciate the help!
I resolved my issue. My issue was I wasn't calling the initializer initWithConsumerKey... that I had in my App Delegate. I had also failed to set it as a delegate properly.
So my App Delegate looks like this:
#import "AppDelegate.h"
static AppDelegate *launchedDelegate;
#interface AppDelegate ()
#end
#implementation AppDelegate
+ (Rdio *)rdioInstance
{
return launchedDelegate.rdio;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
launchedDelegate = self;
_rdio = [[Rdio alloc] initWithConsumerKey:#"removed" andSecret:#"removed" delegate:nil];
return YES;
}
and in ViewController.m:
- (IBAction)listenButton:(UIButton *)sender
{
_rdio = [AppDelegate rdioInstance];
[self.rdio preparePlayerWithDelegate:nil];
[self.rdio.player playSource:#"p12691138"];
}
Feeling silly that I didn't get that at first! Leaving it up here in case it helps anybody.
I am using Xcode 5
This is my AppDelegate.h file
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *viewController;
#end
This is my AppDelegate.m file
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[self.viewController saveData];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[self.viewController loadData];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#end
This is my ViewController.h file
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
//some variables declared here
#interface ViewController : UIViewController <UIActionSheetDelegate, UINavigationBarDelegate, UIImagePickerControllerDelegate>{
//some outlets declared here
}
//some actions and - (void) declared here
- (NSString *) getFilePath;
- (void) saveData;
- (void) loadData;
#end
Beside the code [self.viewController saveData] and [self.viewController loadData] there's an error message:'No visible #interface for 'UIViewController' declares the selector 'saveData' and 'No visible #interface for 'UIViewController' declares the selector 'loadData'.
What should I do?
You should specify class of UIViewController in your AppDelegate.h file:
#property (strong, nonatomic) ViewController *viewController;
and add import statement in this file
#import "ViewController.h"
Your viewController is subclass of UIViewController class so there is not saveData/LoadData method.
The two method exists in your custom class - ViewController.
The solution is replace line in AppDelegate.h from:
#property (strong, nonatomic) UIViewController *viewController;
to:
#property (strong, nonatomic) ViewController *viewController;
or cast viewController to ViewController every time you call saveData/loadData
If the view controller in the app delegate is truly a ViewController object, then change the property to say so:
#property (strong, nonatomic) ViewController *viewController;
You will then also need the matching #class (.h) and #import (.m) statements.
Ive been hitting the wall for two days on this hard (but simple for you) problem.
The problem is as follows:
I am posting a notification in the appdelegate
I am attempting to receive that notification in a viewcontroller but I cannot receive it.
Here is the code for this.
In the appdelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) NSString * someString;
#end
In the appdelegate.m
#import "AppDelegate.h"
#import "SomeContextExample.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.someString = #"Hello!";
NSDictionary * userInfo = #{SomeContextExampleRef : self.someString};
[[NSNotificationCenter defaultCenter] postNotificationName:#"SomeContextExample"
object:nil
userInfo:userInfo];
return YES;
}
The "SomeContextExampleRef" is coming from a .h file as follows:
#ifndef SampleNotWorking_SomeContextExample_h
#define SampleNotWorking_SomeContextExample_h
#define SomeContextExampleRef #"SomeContextExampleRef"
#endif
Finally, in the viewController.m:
#import "ViewController.h"
#import "SomeContextExample.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:#"SomeContextExample"
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note)
{
NSLog(#"got the notification!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! %#", note.userInfo);
}];
}
My full code is attached here:
https://github.com/moomoo23/SampleNotWorking
Thank you for helping a beginner!
Try posting your notification when you are confident your "ViewController" object has instantiated and/or come into view. E.G. why not try it out by putting it into an "IBAction" method fired by some UIButton?
The observing view controller may or may not (more likely not in your case) be existing at the end of "application:didFinishLaunchingWithOptions:" in your app delegate.
Another alternative is to post the notification after a delay using the scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: method.
Now, conceptually, why would you post a notification when the appFinsihedLaunching?.... just curious.... e
I am trying to make a tab bar application, first i followed This Facebook grap api tutorial and my project was working fine like in picture when I touch to login, display popups
after that I have added only a tab bar and I am lost, No errors but program does not launch facebook login display. I have used breakpoints to understand why program doesnt launch facebook login display but couldnt understand because it just stuck no errors. it has to be something with tab bar.
Now it programs stucks at popin up facebook login display
my app delegate.h
#import <UIKit/UIKit.h>
#class FBFunMe;
#interface FBFunAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
app delegate.m
#import "FBFunAppDelegate.h"
#import "FBFunMe.h"
#import "FBFunLoginDialog.h"
#implementation FBFunAppDelegate
#synthesize window = _window;
#synthesize rootController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window addSubview:rootController.view];
[self.window makeKeyAndVisible];
return YES;
}
I know rest of the code is working because i have an identical project without tab bar that works fine.
Any suggestion or example code to make it work?
_____-----------EDIT------------________
In app delegate when i try this code it display FBFunLoginDialog which what i need but i still need to this in my login button not in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
because i call some variables and appID and staff in order to login to the Facebook
FBFunLoginDialog *loginController=[[FBFunLoginDialog alloc] init];
[self.window addSubview:rootController.view];
[self.rootController presentModalViewController:loginController animated:YES];
[window makeKeyAndVisible];
return YES;
in FBFunMe.h
#interface FBFunMe :UIViewController <FBFunLoginDialogDelegate,UITextFieldDelegate> {
FBFunLoginDialog *_loginDialog;
UIView *_loginDialogView;
}
in FBFunMe.M
- (IBAction)loginButtonTapped:(id)sender {
NSString *appId = #"3888888883";
NSString *permissions = #"publish_stream";
if (_loginDialog == nil) {
self.loginDialog = [[[FBFunLoginDialog alloc] initWithAppId:appId
requestedPermissions:permissions delegate:self] autorelease];
self.loginDialogView = _loginDialog.view;
}
if (_loginState == LoginStateStartup || _loginState == LoginStateLoggedOut) {
_loginState = LoginStateLoggingIn;
[_loginDialog login];
} else if (_loginState == LoginStateLoggedIn) {
_loginState = LoginStateLoggedOut;
[_loginDialog logout];
}
[self refresh];
}