How can I create a custom property in theos/logos? - iphone

Would it be possible to create a custom property in a hooked method using theos/logos?
example:
//define my custom property
#interface SBAwayController : NSObject {
UIView *myCustomView;
}
#property (nonatomic, retain) UIView *myCustomView;
#end
%hook SBAwayController
- (void)activate {
//call original method
%orig;
//use my custom property
if (tweakEnabled)
[self.awayView addSubview:myCustomView];
}
%end
I've tried it as exampled above, but no success.

An alternative is to create a singleton class that will hold your object/context. Take this example from this Introspy hook class. tracerStorage is a variable defined outside the class and all hooks access it.
To apply it to your case, you can have an external variable NSMutableDictionary with the current SBAwayController as key and the property myCustomView as value.

Related

Accessing Obj-C property defined in parent class extension that you can't modify

Assume you parent class is, e.g. MyClass.m
#interface MyClass()
#property (nonatomic, readwrite, retain) NSString * str;
#end
And you inherit from this class and want to modify the value of str, is it possible without touching MyClass.m or MyClass.h?
Thanks.
Yes you can, just use KVC:
[anObject setValue:#"foo" forKey:#"str"];
NSString* x =[anObject valueForKey:#"str"];
But don't do this. The property is hidden from the public interface in *.h because the developer of the parent class wanted to hide it.
Eventually the developer of the parent class might just stop using the property. Then your code which uses this property will crash.
So, don't do this.

How to use delegates in ios 5

I need to use a delegate object in an iOS application. I have declared the delegate as this:
In the class where the function is defined:
#interface OOObjectCommandInterface : NSObject<OOCameraControllerDelegate>
In the class where the function must be invoqued:
(In de .h file)
#protocol OOCameraControllerDelegate
- (void)drawFrame:(CVImageBufferRef) imageBuffer:(BOOL)flip;
#end
and
#interface OOCameraController : UIViewController
{
...
id<OOCameraControllerDelegate> delegate;
}
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
Aditionally, where the second class is initialized:
_hardwareController.delegate = [OOObjectCommandInterface ocInterface];
where _hardwareController is an instance of OOCameraController class.
So, when I try to invoque the delegate object, I do this:
[delegate drawFrame:imageBuffer:flip];
but the function is not executed. Any idea?
P.D.: The function I am calling is a singleton class. Could be any problem there?
Have you set delegate to self in the second class? Create an object in the second class like
#property (nonatomic, readwrite) id<OOCameraControllerDelegate> delegate;
and then [_hardwareController setDelegate:self];
By definition, a singleton is a design patron to access an object, unique, that only can be created 1 time (first get_instance you do). With get_instance, you can access from everywhere, to the functions inside the singleton, so, Why you are not using it directly?
Write something like [[MySingletonClass get_instance] FunctionThatIWantToUse:...]; And don't use a delegate

clarifying on properties in objective C

Sorry for the simple question.
When I see a definition of a property inside the h file, but outside of the class #interface scope, what does it mean ?
#property (nonatomic, readonly) RMMapContents *mapContents;
Here is the code:
#class RootViewController;
#class RMMapContents;
#interface MapTestbedAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
//MAIN VIEW
//==============
RootViewController *rootViewController;
// NETWORK DATA
// =============
NSMutableArray *photoTitles; // Titles of images
NSMutableArray *photoSmallImageData; // Image data (thumbnail)
NSMutableArray *photoURLsLargeImage; // URL to larger image
NSMutableData *receivedData;
NSURLConnection *theConnection;
NSURLRequest *request;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
#property (nonatomic, readonly) RMMapContents *mapContents;
#end
Inside a function I see this line:
- (void)foo:(xyz *)abc{
..
RMMapContents *mapContents = [self mapContents];
..
}
So, taking it from C++, the mapContents seem like it is not a global scope var (after all, that's why they call them properties, right?), but isn't defining the same name again inside the function weird a bit?
I hope someone can clarify a little here.
Thanks!
The scope of the #interface block extends upto the #end keyword and is not restricted to the braces {}.
So the #property declaration lies very much inside the scope of the #interface and like cli_hlt rightly answered, it acts like a substitute to setter and getter methods for the mapContents property.
so a property named mapContents, would have setters and getters which look like this :
- (void)setMapContents; //setter
- (RMMapContents *)mapContents; //getter
and would can be accessed from within the class using these methods:
[self setMapContents:newContents];
AND
RMMapContents *contents = [self mapContents];
Well, a property is not just a variable. A property is a variable plus its setter and getter methods. A property is usually said to be backed by a variable, which usually(but not always) has the same name as the property itself.
So there are basically three scenarios:
The developer has redefined the backing variable, look for something like:#synthesize mapContents=mapContents_, at the beginning of the implementation -> no problem here.
The compiler defined the variable to be something you don't now but not equal to mapContents - > no problem.
The property backing variable is indeed called "mapContents", so then the local definition hides the global definition (look for a compiler warning here). But by calling [self mapContents] you will not access the global variable but call the getter, which in turn will access the class variable (because then the local mapContents is out of scope)
Hope this helps.
global var mapContents is readonly,in foo function , create a new pointer,then you can change the value of inner var.
Look for a method in your class with a name mapContents that will return a initialization to your RMMapContents class.
Basically this line RMMapContents *mapContents = [self mapContents]; says that initializing an instance of RMMapContents called mapContens using the method mapContents.

Object as a data member in Objective C

From what I have experienced it seems as if objects cannot be shared data members in objective c. I know you can init a pointer and alloc the object in each method but I cannot seem to figure out how one can say define a NSMutableString as a data member and allow all of the methods to use and modify its data as in c++. Is this true or am I missing something?
To define an instance variable (member), edit your .h file:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#end
Then, in your .m file, from any instance method (one which begins with -), you can access this variable.
- (void)doThingWithIvar {
[member doThing];
}
If you want to access the variable from outside the object itself, you'll need accessors. You can do this easily with Obj-C properties:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#property (nonatomic, retain) NSObject *member;
#end
And in the .m:
#implementation MyClass
#synthesize member;
// ...
#end
The #synthesize line creates getter/setter methods for the ivar. Then you can use property syntax:
MyClass *thing = ...;
NSLog(#"%#", thing.member); // getting
thing.member = obj; // setting
(Note that I specified (retain) for the #property; if your member isn't an Objective-C object you won't want that. And if your property's class has a mutable counterpart, you'll want (copy) instead.)
It sounds like you want to synthesize (create getter/setter methods) a property for a member variable. I just found this cheat sheet, go down to the section called, "Properties", should give a quick overview.
Other than that Apple's documentation should give you more info.

iPhone Objective-C Basic Example Question (about Properties)

Sorry I couldn't give a more descriptive title. My question (after looking at the code below) is what statusText is. Is it an IBOutlet or a UILabel? How does "#property (retain,nonatomic) UILabel *statusText" work? Does that statement mean that statusText is a property???
Thanks for answering. If there are any questions, please feel free to comment.
Button_FunViewController.h
#interface Button_Fun2ViewController : UIViewController {
IBOutlet UILabel *statusText;
}
#property (retain,nonatomic) UILabel *statusText;
- (IBAction)buttonPressed: (id)sender;
Button_FunViewController.m
#synthesize statusText;
- (IBAction)buttonPressed: (id)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:#"%# button was pressed.", title];
statusText.text = newText;
[newText.release];
}
IBOutlet evaluates to nothing, it's just a hint to Interface Builder that it's a 'connection'.
And yes, statusText is a property then. There are three statements needed for a property (on a non-"modern runtime" system) – an ivar declaration, a #property statement, and a synthesize statement.
statusText is an instance variable of type UILabel*. The IBOutlet keyword simply makes that instance variable available to Interface Builder.
#property declares accessor and/or mutator methods for the given property. It's equivalent to declaring -statusLabel and -setStatusLabel: methods.
You can use #synthesize to automatically implement these -statusLabel and -setStatusLabel: methods. The nonatomic and retain keywords define the behaviour of these automatically-generated methods.
Alternatively, you can implement the -statusLabel and -setStatusLabel: methods yourself.
what is statusText ?
statusText is a UILabel in your code example
Is it an IBOutlet or a UILabel?
Both.
UILabel is a type (a pointer to UILabel component that you use in GUI)
IBOutlet marks variable for Interface Builder application, so that it knows to show it as Outlet. During compilation IBOutlet is compiled out, it is defined in NSNibDeclarations.h as:
#define IBOutlet
How does #property
(retain,nonatomic) UILabel
*statusText work?
You can create accessors (getters/setters) for a variable by hand, no need to use property. You can just have UILabel *statusText and implement your getter/setters by hand.
You can have accessors declared by compiler by defining variable as a #property and then either use #synthesize to create accessors in .m file or again you declare the accessors yourself (you can override default accessors that would be generated)
You can have readwrite or readonly property - meaning either both setter and getter gets generated or only getter.
You can use copy, retain or assign for setter (see more about memory management about the tree optons copy/retain/assign)
There are some other options like nonatomic/atomic which has to do with generating mutexes and lock variable before access and so on (see more about properties)
For example if you have variable
NSString * string;
defining it as readwrite property and then synthesising you get the compiler to generate for you:
#property (copy, readwrite) NSString * string
then using
#synthesize string;
generates something like:
- (NSString *) string
{
return string;
}
- (void)setString:(NSString *)str
{
NSString * copy = [str copy];
[string release];
string = copy;
}
Does that statement mean that
statusText is a property???
Yes you defined it as a property as explained above.
There are couple of concepts involved here.
Definition of variable, defining it as IBOutlet for Interface Builder, declare variables as properties so that compiler generates getters/setters for you, defining type of getters/setters such as access method, memory management and locking.
I hope this explains your questions and if you follow the link you will find the explanation by Apple which I believe is quite clear about how to use properties.
Sorry for the horrible formatting ...
If you put IBOutlet in there like that, it will allow you to link the item while you're in interface builder.
Once you have the IBOutlet setup, you can open that class's nib and then select the File's Owner, then go to the inspector and drag a link from the Connections tab to the object.
This let's you then make changes to the object in your class's code and it will change the linked object. So once you link "statusText" to a specific UILabel, you can then use statusText.text = #"new text" in your class's code to modify the item, for example.
Also, IBAction allows you to make links in the same way. You can drag a connection from a control's event to the file owner and then select any method within that class that has IBAction as a return value.
There are in fact two statusText "things" in your example. There is a statusText object of type UILabel, and there is a statusText function created by #synthesize. When you use statusText from inside a method you are reffering to the object, not the function. Use self.statusText to use the property/function.