Configure device specific global instance variables - ios

I'm currently writing a universal app, and would like to set certain parameters, such as standard button size, globally, depending on device. Currently I am using:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
iVar = 88;
}
else {
iVar = 44;
}
in various places. However, I'd like to run this only once, and set the ivar somewhere globally. I know I could do this in the app delegate, once the app initialises, and declare the variable in the header, but I'm wondering if there is a more elegant solution that is standard practice.
(I am aware that I could use the native image size using xcassets, but I'd like more control than this).

Create a class Global.h and declare all global variables in it. Keep only the header file and import the Global.h file in YourProject-Prefix.pch like this:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Global.h"
#endif
In order to define your global variables, simply open the Global file and put the following line of code, i.e:
#define __metricsTableRowHeight 45
#define __metricsTableSectionHeight 45
#define __metricsTableSectionButtonHeight 57

Related

in XCode objective-C game project, how to include a .swift file with constants delcared in it

I have a game in Objective-C and I have constant in it declared like this, in file GameplayConstants.h:
#ifndef GameplayConstants_h
#define GameplayConstants_h
static float REGULAR_TIME_PER_FRAME = 0.1f;
#endif /* GameplayConstants_h */
I am converting game to swift, so I want to convert one by one file to swift. In particular, I want this constant (REGULAR_TIME_PER_FRAME) converted to a swift file, to a constant variable in a swift file GameplayConstants.swift (rather than in the GameplayConstants.h file which it currently sits in now). What should I do? I set the defines module in configuration file to YES, and I included the file (with line include GameplayConstants.swift), but I have various errors. At the moment, error is:
"missing #end"
in HeroAnimationhelper.m file, which has code (partial code displayed):
#import "HeroAnimationHelper.h"
#import "HeroConstants.h"
#import "dealer-Swift.h"
#implementation HeroAnimationHelper
//#include "GameplayConstants.h"
#include "GameplayConstants.swift"
As you see, I'm replacing the original .h file with .swift file.
What else do I need to do to use swift constants in a objective-c class?

My custom native module is not present inside NativeModules object

So, i wanted to create a native module which will detect, if the app is running on emulator/simulator or an actual device.
Everything works fine on android, but i'm facing issue on iOS.
I have create a AbcModule.h and a AbcModule.m file
#import <React/RCTBridgeModule.h>
#interface AbcModule : NSObject <RCTBridgeModule>
#end
This is AbcModule.h
#import "AbcModule.h"
#implementation AbcModule
RCT_EXPORT_MODULE(GetDetails);
- (BOOL) xyzFunctn {
#if TARGET_IPHONE_SIMULATOR
return YES;
#else
return NO;
#endif
}
RCT_EXPORT_METHOD(xyzFunctn: (RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject) {
resolve self.xyzFunctn;
}
#end
This is AbcModule.m
Here i have followed the react native documentation for implementing the Native Modules.
But i'm consistently facing this error which says
"TypeError null is not an object, evaluating GetDetails.xyzFunctn"
I have went through several solutions and articles but nothing seems to be working here.
Need help guys!
from the docs
If you do not specify a name, the JavaScript module name will match the Objective-C class name, with any "RCT" or "RK" prefixes removed.
so just do not specify any name,
#implementation AbcModule
// To export a module named AbcModule
RCT_EXPORT_MODULE();
#end
in your case it should then be accessible from within JS with
AbcModule
But the documentation is not clear if the Objective-C Class declaration needs to be written with prefixed "RCT" or "RK".. but because both prefixes seem to be valid, you should be able to just use AbcModule without prefix.
In other words, if you want to use GetDetails from within JS you need to name your interface and implementation accordingly
#implementation RCTGetDetails
RCT_EXPORT_MODULE(GetDetails);
// or
// RCT_EXPORT_MODULE();
#end
Okay, if there is someone who is facing this issue and feels like their code should work but it isn't and any solution online not working for you as well.
Try this:
When you create your .h and .m file for header and objective-c or swift file, make sure you do it in Xcode and not from VSCode.
VSCode eventually doesn't adds you .h file in the required resources folder, i have wasted my 2 weeks trying to find out solution for it, but lastly, that was it, yes this is it.
in your .m file, let's say GetDetails is a class of NSObject .swift
you need:
#interface RCT_EXTERN_MODULE(WidgetManager, NSObject)
// method to export
RCT_EXTERN_METHOD(isAuthenticated: (BOOL *)isAuthenticated)
#end
in your GetDetails.swift:
#objc(GetDetails)
class GetDetails: NSObject {
#objc(isAuthenticated:)
func isAuthenticated(_ isAuthenticated: Bool) {
}
}

#define a global preprocessor directive in a separate file in Objective-C

I want to define a global preprocessor directive in my app. For example:
In MyProgram.h, I define:
#define USE_LOCALHOST
I import this file from beginning in appDelegate.m.
For the later use, in another file, I refer to this global preprocessor directive.
In MyWebService.h, I define:
#ifdef USE_LOCALHOST
static NSString *MY_SERVER = #"http://192.168.1.130:8888";
#else
static NSString *MY_SERVER = #"http://myserver.com";
#endif
However, the value of MY_SERVER is always #"http://myserver.com". How to make it work properly? Thanks.
Define it in .pch. And you will never forget to include .h-file where you have defined USE_LOCALHOST.
Or you can define it in build settings in Preprocessor Macros.
For example only for Debug.
#define works only in the file where it's defined in. But you can #import "MyProgram.h" in MyWebService.h and problem solved. Every time you need to access USE_LOCALHOST, just import the header file.

Global variables in my Constants file

Until now if I needed access to a global variable across my app i just added
#define PATH [NSString stringWithFormat:#"www.url.com"]
To my Constants.h file.
I need to fetch the PATH value from my server.
How can i assign the value I'm getting from a server to a macro \string like the above and still be able to use just the variable PATH anywhere in my app? (Without naming the class like a property use such as class.PATH
This works:
#import <Foundation/Foundation.h>
NSString* PATH;
#interface Constants : NSObject
+(void)getPathFromServer;
#end
And PATH is accessible from anywhere in my app but I'm not sure if that should be the way to go.
As I understand so far, you need to define a macro which dynamic change it's url content.
If I'm right , you may need a Vararg Macros which takes a variable.
#define PATH(...) [NSString stringWithFormat:#"%#",__VA_ARGS__]
You can use extern keyword
Example :
//Header file
extern NSString * const path;
// .m file under implementation
NSString * const Ppath = [NSString stringWithFormat:#"www.url.com"];
Have a look at these
Constants in Objective C
#define vs const in Objective-C

Macro constant for something like status bar height

I know how to define constants and macros using C. Is there any way to define a constant in Objective-C like this:
#define STATUSBAR_HEIGHT [UIApplication sharedApplication].statusBarFrame.size.height
yes thats pretty much it. For example you want to open a url then -
#define OPEN_URL(urlString) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]]
Put this in a file like utils.h and import that header file for this macro to be available. But in a large code base it is irritating to be importing in each file, You could declare a global macro in the #ifdef __OBJC__ section of your AppName-Prefix.pch. Now you need not import in each file...

Resources