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.
Related
This is my AppDelegate.h:
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
ProductHandlerModel *handler;
}
#property (strong, nonatomic) UIWindow *window;
-(ProductHandlerModel*)getHandler;
#end
and this is in my AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
handler = [[ProductHandlerModel alloc]init];
return YES;
}
- (ProductHandlerModel*)getHandler {
return handler;
}
How can I access this handler object from another UIViewController class?
call like
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate getHandler];
or simply call like
[(AppDelegate *)[[UIApplication sharedApplication] delegate] getHandler];
Dont forget to import.
#import "AppDelegate.h"
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
I would like to pass a string to all my other view controllers from the AppDelegate. I have defined a variable called appName and assign it a string
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) NSString *appName;
#end
AppDelegate.m
#implementation AppDelegate
#synthesize appName;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
appName=#"SatKacPara";
return YES;
}
The following implementation does not have access to appName property. I could not able to figure out.
ViewController1.m
appLabel.text=[(AppDelegate*) [UIApplication shareApplication].delegate ].appName;
You have a simple typo in your line of code. It should be:
appLabel.text = ((AppDelegate*)[UIApplication sharedApplication].delegate).appName;
But using such a property isn't necessary. You can get the app's display name without hardcoding it.
Get rid of your appName property and change this line of code to:
appLabel.text = [NSBundle mainBundle].infoDictionary[#"CFBundleDisplayName"];
This has the advantage of showing the right name even if you rename your app or localize it.
Besides, adding the iAd and AdMob code to get banner to appear on screen, is the following code the correct way to set up a shared banner?
In AppDelegate.h
#import GoogleMobileAds;
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <GoogleMobileAds/GoogleMobileAds.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property(nonatomic, strong) ADBannerView *iAdView;
#property(nonatomic, strong) GADBannerView *adMobView;
#end
AppDelegate.m
#import "AppDelegate.h"
#import <iAd/iAd.h>
#implementation AppDelegate
#synthesize iAdView;
#synthesize adMobView;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
iAdView= [[ADBannerView alloc]init];
return YES;
}
iPhone6.m
#import "iPhone6.h"
#import <iAd/iAd.h>
#import "AppDelegate.h"
-(AppDelegate *) appdelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
-(void)viewDidLoad{
iAdView =[[self appdelegate] iAdView];
}
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.