johnno1962 / injectionforxcode

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

Injection App won't work on iOS 10 simulator #202

Closed saoudrizwan closed 7 years ago

saoudrizwan commented 7 years ago

I'm sorry if I couldn't gather this from the docs, but does code injection not work anymore in iOS 10? Is it simply not possible to use the standalone Injection app for iOS 10 projects?

johnno1962 commented 7 years ago

It should work in the simulator for iOS 10. It no longer works on the device.

saoudrizwan commented 7 years ago

@johnno1962 so I download the injection app and open it. Then I open xcode, open a project, and run the app in the iphone 6 simulator. Then I change the font size of a label, and save. Then I hit 'Inject Source' from the menu bar. Nothing on the device changes, am I doing something wrong?

johnno1962 commented 7 years ago

Are you seeing message in the console? Is the code being executed? A common problem is that for example viewDidLoad has not been called.

saoudrizwan commented 7 years ago

Sorry, should have also added that this is what prints in console:

Compiling /Users/saoud/RealWeather/BasicWeather/BasicWeather/MainViewController.swift
objc[1404]: Class _TtC12BasicWeather18MainViewController is implemented in both /Users/saoud/Library/Developer/CoreSimulator/Devices/6BBA7268-362A-493C-AC24-325FA99E1444/data/Containers/Bundle/Application/097B01FB-E3BC-4899-B014-431A9116C598/BasicWeather.app/BasicWeather (0x1080fd930) and /Users/saoud/Library/Developer/Xcode/DerivedData/BasicWeather-fpueefjsciyxymbpgxzetqwcffhu/Logs/iOSInjectionProject/build/Debug-iphonesimulator/InjectionBundle4.bundle/InjectionBundle (0x11b66ee28). One of the two will be used. Which one is undefined.
14 injections performed so far.
2017-02-16 17:05:28.841 BasicWeather[1404:15131] Ignore any warning, Swizzled BasicWeather.MainViewController 0x11b66ee28 -> 0x1080fd930
saoudrizwan commented 7 years ago

Here's a video of what I'm doing (thanks for helping btw, I would love for this to work for me) https://www.dropbox.com/s/f1o3tnr503q5apo/inject-recording.mp4?dl=0 The video has better quality if you download it (top right button)

saoudrizwan commented 7 years ago

I put this in my code too

func injected() {
    print("I've been injected: \(self)")
}

and the project crashed when I tried injecting source.

johnno1962 commented 7 years ago

There are problems using “-injected” on Swift at the moment. Sorry about that. Can you try using a “INJECTION_BUNDLE_NOTIFICATION” notification to make sure the code gets executed?

saoudrizwan commented 7 years ago

Just tried that, and the notification does get called so the code does get injected. But nothing on the simulator screen updates to reflect the changes.

johnno1962 commented 7 years ago

Have you added a print statement to check the code you expect to be executed is running and changed the text to see if it’s getting injected?

saoudrizwan commented 7 years ago

So I put print("executed") in viewDidLoad and it doesn't print in console when I inject source. So it doesn't call viewDidLoad at all when I inject source. Is that the expected behavior?

johnno1962 commented 7 years ago

We thought about adding calling ViewDidLoad if the class being injected was a subclass of UIViewController but it seemed too “magic” and might confuse people. As things stand it seems to confuse many that it doesn’t. viewDidLoad will be called if you reload the view or call it from a handler of the notification.

saoudrizwan commented 7 years ago

Ahh, so the injection notification's selector method is the code that's being "injected"? That was a bit confusing to gather from the README. I put self.viewDidLoad() in the notification observer's selector method and everything works accordingly now! The simulator updates to reflect the changed properties! Thank you for the clarification good sir. It seems I still have a bit more learning to do about injection and your awesome program.

johnno1962 commented 7 years ago

Hi @saoudrizwan, I’ve uploaded a new version of injection which should fix the crashes you were seeing trying to use the “-injected" method. Is there any chance you could download it and give it a try for me? http://johnholdsworth.com/injection.html

saoudrizwan commented 7 years ago

Tried it and it works! Brilliant work man, keep up the great work!

saoudrizwan commented 7 years ago

Hey @johnno1962 I made a little getting started video for Injection. I'd love for you to check it out and let me know what you think! https://www.youtube.com/watch?v=YOfb722z8WQ

johnno1962 commented 7 years ago

Thanks! It makes the case well. I may put it on the site if that’s ok?

saoudrizwan commented 7 years ago

That'd be awesome!

johnno1962 commented 7 years ago

done.

saoudrizwan commented 7 years ago

@johnno1962 hey John, I just finished writing an article about Injection on medium if you wanna check it out! https://medium.com/@sdrzn/make-ios-apps-faster-20x-faster-e56cc429d8ee

johnno1962 commented 7 years ago

Hi, One thing. With swift you cant add methods (i.e. -injected) to a class without restarting the app otherwise it’s vtable gets out of alignment. It works for you because your injected method was the last one. Other than that your article’s great. Thanks again!

saoudrizwan commented 7 years ago

Ohh okay, I'm gonna remake the video then and address that.

saoudrizwan commented 7 years ago

Hey @johnno1962 if you want, I can make another video that addresses some things my other video didn't. Do you have any recommendations of things to talk about? My medium article got about 350 views with 30 likes so I think keeping things super simple is a good idea when teaching people about injection. (Also, I was thinking that I should make the font a lot bigger in Xcode for the new video.)

johnno1962 commented 7 years ago

Sounds good. I liked your first video though - nice and simple. Making the fonts bigger would be an idea. I’m planning to write a web page this week with the missing FAQ as well..