Accessing AppDelegate Property - 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.

Related

How to share a variable AND it's value in Objective-C?

I have surfed quite a lot about this. But the results that I've got tells me that either I should use appDelegate or singleton etc.
All of which produces the same result. i.e I am able to share a variable in different ViewControllers but as soon as the ViewController gets changed , the variable looses it's value.
For example I used a variable named myVar of type int. I declared it in AppDelegate and then I'm able to use it in all the ViewControllers with the help of AppDelegate. But as soon as I move from A ViewController to B ViewController the value of the myVar variable gets "0" again. I don't want that . I want the variable to hold it's value. And I don't want to pass this data with the help of pushViewController etc. Please suggest me a good solution.
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, assign) int myVar;
#end
FirstViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate* app = (AppDelegate*)[UIApplication sharedApplication].delegate;
app.myVar = 1;
NSLog(#"%d",app.myVar); //Shows "1" in Log
}
SecondViewController.m
- (IBAction)pressButton:(id)sender{
AppDelegate * app = (AppDelegate*)[UIApplication sharedApplication].delegate;
NSLog(#"%d",app.myVar); // Shows "0" in Log (But I want it to show "1" as I have already set "myVar" value as "1" in my FirstViewController)
}
Your #property (strong, assign) int myVar; is declaring both strong and assign. As an int, it should only be assign, not strong. strong would be used for Objective-c objects, assign for primitive C properties.
I think what you want is the following:
#property (assign, nonatomic) int myVar;
First of all check your code there are some error
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, assign) int myVar;
And check it gives same value in FirstViewController.m and SecondViewController.m
if your var is a NSString, you can simple use the [copy] protocol. e.g.
NSString *eString = [cString copy];
If it is an object, you need to implement the [copy] protocol for that class.

How to solve the error 'No visible #interface for 'UIViewController' declares the selector ''

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.

No managedObjectContext defined in my AppDelegate

I'm trying to test my core data scheme. However, it seems I am unable to create the context because it says No visible #interface for 'MyAppDelegate' declares the selector 'managedObjectContext'.
In online tutorials this method seems to be auto-generated when we create the app. However, in my case it doesn't exist.
This is MyAppDelegate:
Header
#import <UIKit/UIKit.h>
#interface MyAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
.m file
#import "MyAppDelegate.h"
#implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
// Override point for customization after application launch.
return YES;
}
How should I fix this in Xcode 5 with iOS 7?
I think the best way for you is to create a Master-Detail Application with Xcode 5 and don't forget to check Use Core Data :
With that, you will have an AppDelegate.h and an AppDelegate.m configured with a managedObjectContext.
You will have a project configured correctly with Core Data and a .xcdatamodeld to use easily your SQLite database.

Receiver 'SMClient' for class message is a forward declaration

I'm trying to add StackMob to my project. It says to create an SMClient instance after having dragged the SDK to the project, checking 'create groups for..' and adding to target. I followed these steps.
However, when I'm creating my SMClient and SMCoreDataStore instances, it gives me an error of Receiver 'SMClient' for class message is a forward declaration and the same for SMCoreDataStore. Here's my code:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class SMClient;
#class SMCoreDataStore;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (strong, nonatomic) SMCoreDataStore *coreDataStore;
#property (strong, nonatomic) SMClient *client;
#end
And part of my .m:
#import "AppDelegate.h"
#import "StackMob.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.client = [[SMClient alloc] initWithAPIVersion:#"0" publicKey:#"YOUR_PUBLIC_KEY"];
self.coreDataStore = [self.client coreDataStoreWithManagedObjectModel:self.managedObjectModel];
return YES;
}
I already cleaned the project, imported the relevant header files, but it still gives that error.
Any ideas?
This might be happening because you forgot to import the classes.
Add it on your .m:
#import "SMClient.h"
#import "SMCoreDataStore.h"

Can't initialize RMConnectCenter of RenRen SDK

I tried to make Renren iOS app along with SocialPlugin docs.
http://wiki.mobile.renren.com/en/index.php/Social_Plugin_Download
I made AppDelegate belows. But every time I invoke app, it failed
at the point of RMConnectCenter initializeConnectWithAPIKey.
The Code is like this.
--noriakiAppDelegate.h--
#import <UIKit/UIKit.h>
#import "RMConnectCenter.h"
#class noriakiViewController;
#interface noriakiAppDelegate : UIResponder <UIApplicationDelegate,RenrenMobileDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) noriakiViewController *noriakiviewcontroller;
#end
--noriakiAppDelegate.h.m--
#import "noriakiAppDelegate.h"
#import "noriakiViewController.h"
#implementation noriakiAppDelegate
#synthesize window = _window;
#synthesize noriakiviewcontroller = _noriakiviewcontroller;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[RMConnectCenter initializeConnectWithAPIKey:#"MY API KEY" secretKey:#"MY SECRET" appId:#"2080970" mobileDelegate:self];
return YES;
}
You mean application crashes when you call initializeConnectWithAPIKey ?
You are not passing your API Key and secret
[RMConnectCenter initializeConnectWithAPIKey:#"MY API KEY" secretKey:#"MY SECRET" appId:#"2080970" mobileDelegate:self];
Are you using storyboards?

Resources