johnno1962 / injectionforxcode

Runtime Code Injection for Objective-C & Swift
MIT License
6.55k stars 565 forks source link

How to use Tunable Parameters by using objective-c #121

Closed JoySeeDog closed 8 years ago

JoySeeDog commented 8 years ago

using swift,I can use Tunable Parameters ,but use oc,xcode can‘t find function

johnno1962 commented 8 years ago

you need to #import BundleInterface.h into your file or .pch file

#ifdef DEBUG
#define INJECTION_ENABLED

#import "/tmp/injectionforxcode/BundleInterface.h"
#endif
JoySeeDog commented 8 years ago

Yes,I imported ,but it also can't find.and some people has the same question as me. this is my .pch file and main.m file

ifndef Prefix_pch

define Prefix_pch

// Include any system framework and library headers here that should be included in all compilation units. // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.

endif /* Prefix_pch */

// From here to end of file added by Injection Plugin //

ifdef DEBUG

define INJECTION_ENABLED

import "/tmp/injectionforxcode/BundleInterface.h"

endif

int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } // From here to end of file added by Injection Plugin //

ifdef DEBUG

static char _inMainFilePath[] = FILE; static const char *_inIPAddresses[] = {"10.0.0.23", "192.168.0.235", "127.0.0.1", 0};

define INJECTION_ENABLED

import "/tmp/injectionforxcode/BundleInjection.h"

endif

i use two ways. first I use the INColors

the second ways,i use delegate and orrvride the two functions. [NSObject inSetDelegate:self forParameter:0];

but xcode can't find [NSObject inSetDelegate:self forParameter:0];

something wrong with installing? I use Alcatraz to install

johnno1962 commented 8 years ago

Is DEBUG defined? Is your pch being compiled? Include BundleInterface.h in the source you are injecting from if there is any doubt.

JoySeeDog commented 8 years ago

I try to Include BundleInterface.h in the source I injected from,but it does't work. so amazing !

johnno1962 commented 8 years ago

Are thhings not compiling or not the parameters not working? Is the project “patched"

JoySeeDog commented 8 years ago

wonderful ,I install it again.and it work !but the color can't be changed. here are my code ``#import "ViewController.h"

ifdef DEBUG

define INJECTION_ENABLED

import "/tmp/injectionforxcode/BundleInterface.h"

endif

@interface ViewController ()

@end

@implementation ViewController

johnno1962 commented 8 years ago

Colors are different, you need to add to viewDidLoad:

JoySeeDog commented 8 years ago

cool! perfect! thank you johnno! here are my code.

- (void)viewDidLoad {
    [super viewDidLoad];
     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [button setTitle:@"TestButton" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    _label = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
    _label.textColor = [UIColor redColor];
    [self.view addSubview:_label];

    [NSObject inSetDelegate:self forParameter:0];
    [NSObject inSetTarget:self action:@selector(inColor:hasChanged:) forColor:0];

}
- (void) test{
      _label.text = @"hello!";
}

- (void)inParameter:(int)tag hasChanged:(float)value{
    NSLog(@"------%f",value);
}
- (void)inColor:(int)tag hasChanged:(id)value{
    self.view.backgroundColor = (UIColor *)value;
}
johnno1962 commented 8 years ago

Does that work?? It should really be:

    [NSObject inSetTarget:self.view action:@selector(setBackgroundColor:) forColor:0];
JoySeeDog commented 8 years ago

yes ,it work.you can try by it yourself and you sure NSObject can find the function setBackgroundColor?it crash because xccode can not find it. I don't think it a good way!

johnno1962 commented 8 years ago

oops setBackgroundColor:

JoySeeDog commented 8 years ago

It works! so there are two ways!