Tuesday, October 20, 2009

Xcode 3.2.1 and Jailbroken iPhone 3.1.2 is Build & Go!

XCode 3.2.1 on Snow Leopard plays nicely with jailbroken iPhone OS 3.1.2. You can do Buid & Go and even debug directly on the device. The procedure described for XCode 3.2 and iPhone OS 3.1 is exactly the same. However, I have been asked a couple of times to put it all together and make a step-by-step guide and this seem to be a good occasion.

First things first: a jailbroken iPhone 3.1.2

If your iPhone is already jailbroken, you should know what to do. I used Pwnage Tool 3.1.4 and the good news is that this time there is no need to enter DFU mode!

If your iPhone is still jailed, it would be probably a good idea to read through the Pwnage Tool 3.1.4 release news and maybe have a look at the jailbreaking step-by-step guide which works for me every time.

Once your iPhone is jailbroken, open Cydia go to the "Manage" tab, select "Sources" and add http://iphone.org.hk/apt/. Then install "Installd Patch" (see image). This is crucial for installing your own application on your iPhone using XCode.

Obtain a self-signing identity

Jailbroken or not iPhone needs software to be signed. If this is your first time, you need to create your own signing certificate. The whole process is described in detail in the original Apple document titled "Obtaining Signing Identity".

Make sure you create the certificate in the default "login" keychain. For avoidance of doubt you would probably like to call your identity "iPhone Developer". This EXACT name is being used in most of the online resources I have seen so far.

Once you have the certificate, switch to XCode and go to Project -> Edit Project Settings, scroll to Code Signing / Code Signing Identity / Any iPhone OS Device and change the value to your freshly created identity name, probably "iPhone Developer" (see image). This step needs to be repeated for each of the existing projects. All new projects should get the value automatically.

Make XCode 3.2.1 compile for your iPhone 3.1.2

Self-signing certificate is good enough for you, it should be so for XCode. Let's tell him. In the file:
/Developer/Platforms/iPhoneOS.platform/Info.plist
find line 46 and replace "XCiPhoneOSCodeSignContext" with "XCCodeSignContext" (see image). Repeat the operation for line 79 and save the file.

If you want to stop here because you don't need debugging, just restart XCode and you should be able to use Build & Go to compile and install applications on your iPhone directly from XCode.

Make XCode 3.2.1 debug on your iPhone 3.1.2

There is some small work to be done if you want to make your XCode 3.2.1 debug on your iPhone 3.1.2 for you. First of all you need to have ldid and ldid2. ldid is a replacement for codesign, which adds entitlements necessary for debugging. We have lost the feature in previous part and now it is time to get around. ldid2 is a shell script which makes use of ldid.

After you have downloaded the archive, unpack the files and put them into /usr/local/bin directory. Then make sure they are both executable.

The last step would be to tell XCode it needs to use ldid2 instead of codesign. In the file: /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Plug-ins/iPhoneOS Build System Support.xcplugin/Contents/Resources/iPhoneCodeSign.xcspec find line 12 and replace "/usr/bin/codesign" with "/usr/local/bin/ldid2" (see image).

Now restart XCode and you should be able to do Build & Go and debug directly on your iPhone.

Update (26/10/2009)
Well, almost. Just one tiny thing remaining. For every project you want to debug, you need to add -gta flag to "Other Code Signing Flags" in the project settings.

I hope you found this guide useful, it worked for you and made you a bit happier. Please don't think twice before you leave your comment.

Thanks for reading,
Jacek

Acknowledgements

This guide could not be possible if not for two great articles:

41 comments:

  1. Hi, very definitely a useful guide, although I am having one bit of difficulty, after following your instructions (perfectly as far as I can tell, but there is a possibility that I screwed something up) the application refuses to debug. when click run in xcode, the application correctly installs. the app also attempts to launch, but before it finishes launching, it quits and returns to the springboard. the end of the debugger output says the following:

    run
    Running…
    Error launching remote program: failed to get the task for process 715.
    Error launching remote program: failed to get the task for process 715.
    The program being debugged is not being run.
    The program being debugged is not being run.

    I am not sure if this is a problem with your method, or if your instructions did what they are supposed to and the problem lies elsewhere.

    ReplyDelete
  2. Hi Matias, I am really glad you find it useful. I have just realised, you should add flag -gta to "Other COde Signing Flags" in your project. My bad. I am going to post an update in a minute. Thanks a lot!

    ReplyDelete
  3. "-gta" workaround don't work with cocos2d projects. Any ideas?

    ReplyDelete
  4. reaaally nice tutorial, i'm so glad now, thanks a lot !

    ReplyDelete
  5. "Error from Debugger: Error launching remote program: failed to get task for process 454."

    The process number changes each time I try to debug, but the message is the otherwise the same each time. It copies the app to the phone okay, and it tries to run, but fails and returns to springboard. If I run manually by tapping the icon on the phone the app will run fine.

    I am using the -gta flag for sure. Without that ldid wouldn't even run and I noticed ldid2 also looks for this.

    How to get debugging working? Using XCode v3.1.4 on Leopard 10.5.8.

    ReplyDelete
  6. Hi I got the same "failed to get task..." thing as Jacek.... XCode 3.2.1, iPod 3.1.2 OSX 10.6.2 Have any idea?

    ReplyDelete
  7. Thanks, Jacek!!!!

    I got a "failed to get task error.." even if I included "-gta" flag in build settings, but I got the solution...

    A directory has project file should not have white space in it...

    ex) ...../Project/Sample Code/...... -> task error
    ..../Project/SampleCode/... -> working!!!!!

    Thanks!

    ReplyDelete
  8. Thank you very much, you are a life saver! I followed all the steps and this worked perfectly for me!

    ReplyDelete
  9. I'm sorry, but I could not get this to work. I'm getting the same error everybody else gets. of course, the process number is variable, but apart from that, it's the exact same.

    I've first tried to use your guide as is, but that didn't work. Then I tried to make ldid2 automatically add the -gta flag, but I got the same error :( my project path does not contain any whitespace.

    Please, somebody help.

    ReplyDelete
  10. last comment poster here.

    I figured out what my mistake was: I didn't put ldid and ldid2 into /usr/local/bin/, but into /usr/bin. If you don't want to always have to add the -gta flag to the project settings, run "pico /usr/local/bin/ldid2" (or in my case, "pico /usr/bin/ldid2"), and change the line

    "if [[ $hasGTA == 0]]; then"
    to
    "if [[$hasGTA == 1]]; then"

    (this reverses the effect of the -gta flag, making it "assume" this flag, and disabling it if you add the -gta flag.

    It's now working for me, I can Build & Run and debug on device. Thanks a lot.

    ReplyDelete
  11. hello!!

    I need some help please:
    I cannot find usr/local/bin in order to change codesign to ldid2 (there is only usr/bin)...
    and how do I open the xcplugin file (which program do I use) in order to change the line...

    Thanks everyone!

    ReplyDelete
  12. Hi there,

    If there is no /usr/local/bin then you can create one. In order to edit the xcplugin file you can use any text editor you like. If you prefer editor with GUI you can choose for example TextEdit, if you are more into command line solutions you can use vi.

    Thanks for reading my tutorial,
    Jacek

    ReplyDelete
  13. to find /usr/local/bin. On finder go to the GO menu, click Go to folder, and type in /usr/local/ leave off the /bin and it should find it, then bin should be in there.
    For some reason it doesn't let you go straight to it.
    hope this helps :)

    ReplyDelete
  14. Does this work for 3.0 os with sdk 3.1.2?

    ReplyDelete
  15. Jacek,

    thanks for a very nice tutorial.

    I am still having some problems. I have created a self-signed certificate called iPhone Developer in my login keychain.

    In my project settings, I have:
    Code Signing
    Codesigning Entitlements (blank)
    Codesigning Identity (blank)
    Any iPhone OS Device iPhone Developer
    Code Signing Resource Rules Path (blank)
    Other Code Signing Flags (tried blank and -gta)

    I get a build error under Check Dependencies
    "Code Sign Error: A valid provisioning profile matching the application's Identifier 'com.yourcompany.app' could not be found"

    I downloaded ldid and ldid2 to /usr/local/bin and they are both marked executable. I made the change to line 12 also.

    -rwxr-xr-x@ 1 me staff 32016 Oct 21 14:24 ldid
    -rwxr-xr-x@ 1 me staff 437 Jan 14 21:37 ldid2

    I also restarted xcode after making the change to line 12.

    Have I set up something improperly? How do I get a provisioning profile since I'm not a member of the Apple Developer Program and can't ask apple for one?

    Do I need to modify my application's plist Is the Bundle Identifier the problem?

    Thanks very much! P.

    PS - if I change iPhone Developer to Don't Code Sign and build, then my app builds and installs to iphone/ipodtouch 3g ok, but I can't debug

    I get in console:
    error launching remote program: failed to get the task for process XXXX
    The program being debugged is not being run

    Thanks for any help that you can give me.

    ReplyDelete
  16. P. again. I am using Xcode version 3.2.1 and am attempting install/debug on both iphone 3g 16 gb 3.1.2 jailbroken with pwnage tool 3.1.4 and ipod touch 3g 64 gb 3.1.2

    thanks again!

    P.

    ReplyDelete
  17. drat, too fast!

    ipod touch 3g jailbroken with blackra1n. The iphone uses installd fix, the ipod touch 3g uses AppSync. Sorry for making 3 comments!

    P.

    ReplyDelete
  18. P. again,

    I went to here (thanks Jacek for the pointer!): where Felipe Navarro V. writes (Oct. 12, 2009) to change /Developer/Platforms/iPhoneOS.platform/Info.plist to replace 3 occurrences of XCiPhoneOSCodeSignContext with XCCodeSignContext.

    When I change these three things and restart XCode, my provisioning problem disappears, but now I'm stuck with the same problem debugging that I mention above when I would use Don't Code Sign, namely Error Launching Remote Program. I would appreciate any assistance if someone has this working on a similar system

    Thanks!

    Xcode is 64-bit, component versions
    Xcode IDE: 1613.0
    Xcode Core: 1614.0
    ToolSupport: 1591.0

    P.

    ReplyDelete
  19. are links not allowed, the link didn't show it was (this time without enclosing <>):
    http://www.alexwhittemore.com/?p=270

    ReplyDelete
  20. Hi, Jacek!

    It turns out that I am able to debug programs that DON'T have spaces (as discussed above) in their names once I fix /Developer/Platforms/iPhoneOS.platform/Info.plist as I noted above.

    Most of my Xcode projects have spaces in their names. Is there a way to modify ldid/ldid2 so spaces can be used in names OR a way to easily change the spacing in an active Xcode project without having to create a new one and copying everything over?

    thanks! P.

    ReplyDelete
  21. Additional information: the console in organizer for my ipod shows:

    working (can debug), Xcode project called Temp:
    Mon Jan 18 15:52:00 unknown mobile_installationd[2703] : 00808a00 verify_signer_identity: Could not copy validate signature: -402620402
    Mon Jan 18 15:52:00 unknown mobile_installationd[2703] : 00808a00 load_application_info: Could not load signer identity from /private/var/mobile/Applications/13AF6C22-AB29-4A32-94B2-64D2750F38B1/temp.app/temp
    Mon Jan 18 15:52:00 unknown SpringBoard[27] : Reloading and rendering all application icons.
    Mon Jan 18 15:52:00 unknown itunesstored[2704] : MS:Notice: Installing: com.apple.itunesstored [itunesstored] (478.52)
    Mon Jan 18 15:52:06 unknown com.apple.debugserver-43[2746] : debugserver-43 for armv6 Copyright (c) 2007-2009 Apple, Inc. All Rights Reserved.
    Mon Jan 18 15:52:06 unknown com.apple.debugserver-43[2746] : Connecting to com.apple.debugserver service...
    Mon Jan 18 15:52:06 unknown SpringBoard[27] : MultitouchHID(20b910) uilock state: 1 -> 0
    Mon Jan 18 15:52:07 unknown SpringBoard[27] : MultitouchHID(2a3a90) device bootloaded
    Mon Jan 18 15:52:07 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.temp[0x19a7]) Spawned and waiting for the debugger to attach before continuing...
    Mon Jan 18 15:52:07 unknown kernel[0] : launchd[2747] Builtin profile: container (seatbelt)
    Mon Jan 18 15:52:07 unknown com.apple.debugserver-43[2746] : Got a connection, waiting for debugger instructions for task "(null)".
    Mon Jan 18 15:52:07 unknown kernel[0] : launchd[2747] Container: /private/var/mobile/Applications/13AF6C22-AB29-4A32-94B2-64D2750F38B1 (seatbelt)
    Mon Jan 18 15:52:07 unknown SpringBoard[27] : Setting springboard visible = 0
    Mon Jan 18 15:52:12 unknown temp[2747] : MS:Notice: Installing: com.yourcompany.temp [temp] (478.52)

    ReplyDelete
  22. NOT working (Xcode project called Temp Temp)
    Mon Jan 18 15:55:08 unknown mobile_installationd[2753] : 00808a00 verify_signer_identity: Could not copy validate signature: -402620402
    Mon Jan 18 15:55:08 unknown mobile_installationd[2753] : 00808a00 load_application_info: Could not load signer identity from /private/var/mobile/Applications/21632B96-3C50-476C-8CB8-AA062AD28E02/Temp Temp.app/Temp Temp
    Mon Jan 18 15:55:08 unknown SpringBoard[27] : Reloading and rendering all application icons.
    Mon Jan 18 15:55:09 unknown itunesstored[2754] : MS:Notice: Installing: com.apple.itunesstored [itunesstored] (478.52)
    Mon Jan 18 15:55:15 unknown com.apple.debugserver-43[2800] : debugserver-43 for armv6 Copyright (c) 2007-2009 Apple, Inc. All Rights Reserved.
    Mon Jan 18 15:55:15 unknown com.apple.debugserver-43[2800] : Connecting to com.apple.debugserver service...
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.temp[0x19a7]) Bug: launchd_core_logic.c:2649 (23909):10
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.temp[0x19a7]) Working around 5020256. Assuming the job crashed.
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.temp[0x19a7]) Job appears to have crashed: Segmentation fault
    Mon Jan 18 15:55:16 unknown com.apple.debugserver-43[2746] : 1 [0aba/1603]: error: ::read ( 7, 0x28091c, 1024 ) => -1 err = Bad file descriptor (0x00000009)
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.Temp-Temp[0xb602]) Spawned and waiting for the debugger to attach before continuing...
    Mon Jan 18 15:55:16 unknown kernel[0] : launchd[2801] Builtin profile: container (seatbelt)
    Mon Jan 18 15:55:16 unknown kernel[0] : launchd[2801] Container: /private/var/mobile/Applications/21632B96-3C50-476C-8CB8-AA062AD28E02 (seatbelt)
    Mon Jan 18 15:55:16 unknown SpringBoard[27] : Application 'temp' exited abnormally with signal 11: Segmentation fault
    Mon Jan 18 15:55:16 unknown com.apple.debugserver-43[2800] : 1 [0af0/0903]: error: ::task_for_pid ( target_tport = 0x0103, pid = 2801, task => 0x0000 ) 0x000001f5/0x000001f5 0x000001f5/0x000001f5 err = (os/kern) failure (0x00000005)
    Mon Jan 18 15:55:16 unknown com.apple.debugserver-43[2800] : error: MachTask::StartExceptionThread (): task invalid, exception thread start failed.
    Mon Jan 18 15:55:16 unknown com.apple.debugserver-43[2800] : 2 [0af0/0903]: error: ::task_for_pid ( target_tport = 0x0103, pid = 2801, task => 0x0000 ) 0x000001f5/0x000001f5 0x000001f5/0x000001f5 err = (os/kern) failure (0x00000005)
    Mon Jan 18 15:55:16 unknown com.apple.debugserver-43[2800] : 3 [0af0/0903]: RNBRunLoopLaunchInferior DNBProcessLaunch() returned error: 'DRHT'
    Mon Jan 18 15:55:16 unknown com.apple.debugserver-43[2800] : error: failed to launch process (null): failed to get the task for process 2801
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.Temp-Temp[0xb602]) Bug: launchd_core_logic.c:2649 (23909):10
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.Temp-Temp[0xb602]) Working around 5020256. Assuming the job crashed.
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.Temp-Temp[0xb602]) Job appears to have crashed: Segmentation fault
    Mon Jan 18 15:55:16 unknown com.apple.launchd[1] : (UIKitApplication:com.yourcompany.Temp-Temp[0xb602]) Throttling respawn: Will start in 2147483647 seconds
    Mon Jan 18 15:55:16 unknown SpringBoard[27] : Application 'Temp Temp' exited abnormally with signal 11: Segmentation fault
    Mon Jan 18 15:55:16 unknown SpringBoard[27] : Setting springboard visible = 1
    Mon Jan 18 15:55:16 unknown SpringBoard[27] : Setting springboard visible = 0

    thanks, p.

    ReplyDelete
  23. Hi there, thanks for reading.
    Have you installed Installd Patch on your iPhone?

    ReplyDelete
  24. Yes, Jacek, I have. I am able to debug an app created in Xcode 3.2.1 called Test (e.g., with NO SPACE in the name), I can't debug an app created in the same xcode called Test Test (e.g., with a space in the name). I made these test projects Utility Apps but I think the problem would occur with any of them.

    BTW, I have changed everything in the app's info plist to remove a space, I have used project rename in Xcode to give the project with spaces in the name no spaces, and I have also edited Project Edit Active Target to remove spaces. No matter what I've tried, I get the same error: Error Launching Remote Program if the app ever had spaces in the Xcode project name.

    I guess I'll have to copy all the source files into new projects (I hope that I can just copy over the nib file or I'll be remaking all the connections too.) All the projects are under Subversion control, and I don't know how that will affect my ability to copy things over.

    I tried creating projects called Test_Test, Test-Test and Test=Test. They all failed with the same error: Error Launching Remote Program.

    Perhaps you could add a note to your fine article that no spaces and no special characters can be used in your Xcode 3.2.1 project if you want to debug on the device.

    Even with spaces and special characters in the name, I can still install, run and debug in the simulator, and I can still install and run on the device, I simply CANNOT debug on the device.

    You might also want to add the changes for Xcode 3.2.1 (lines 46, 83, 95) to work for debug that I mentioned in my January 15th, 2010, 4:28 am comment.

    Thanks for your assistance and for a VERY helpful article.

    Best regards,

    P.

    ReplyDelete
  25. Jacek,

    I am having various luck creating nospace, nospecial character in name Xcode projects so debugging will work: for the utility-based apps, I can for the most part rename the generated files and then copy over (with copy files checked) the NIB files from the original app, most of the other files are just a copy/paste

    for navigation based apps, this didn't work as their nibs can contain hard-coded file names which no longer apply. I would select all the UI elements from the original NIB and copy them to the clipboard. I would then paste them into the generated NIB, and set up all the various connections again.

    It's not as hard as I thought, but it's not fun! 8-(

    P.

    ReplyDelete
  26. P. wrote "that no spaces and no special characters can be used in your Xcode 3.2.1 project"

    OK, I was half asleep. What I meant to say is that the NAME you give to your to-be-generated project in the Xcode 3.2.1 File, New Project... dialog cannot have spaces or special characters (such as -_= and so on).

    If you're using SCM (subversion in my case), then you CAN check a previously-generated project out to a folder name which DOES have spaces in it if, at that point, the generated code is already safe for debugging.

    Sorry for my confusion, and thanks again for a great article!

    Best regards,

    P.

    ReplyDelete
  27. This comment has been removed by a blog administrator.

    ReplyDelete
  28. Make Money Online,

    Dude, thanks for your comment. I am really sorry I have deleted your post and I must admit that I really felt bad about doing so. It's just that I had to do that as I do not support black hat online marketing. Thanks for coming, anyway.

    ReplyDelete
  29. Jacek,

    Do you know if the 3.2 beta for iPhone/iPodTouch/iPad breaks your fix for debugging?

    Thanks, P.

    ReplyDelete
  30. Jacek,
    thank you for a nice tutorial!
    I can build and run!

    But I have a probrem, I cannot debug...
    If I write a "-gta" flag, Xcode said an error.
    "codesign: Invalid option -- g"

    Do you know this error?
    (I think Xcode doesn't read iPhoneCodeSign.xcspec...)

    Mac: Snow Leopard
    Xcode: 3.2.1
    iPodTouch2G: 3.1.2

    ReplyDelete
  31. THANK YOU EVER SO MUCH FOR THIS !!!
    It worked perfectly for me! This has definitively made my day!

    Thanks for posting this!

    ReplyDelete
  32. Thank you a lot for this man. However, as many posters above, I'm still having the problem of: Error Launching remote program. Failed to get the task for process

    Any idea? I modified the folders to have no white spaces whatsoever, all my projects never had white spaces in them anyway!

    ReplyDelete
  33. Great post. But I'm also unable to get the debugger running. I'm getting the same "Error Launching remote program. Failed to get the task for process " error. I've got ipod touch 3.1.2 jailbroken with blackra1n and has installd patch.

    ReplyDelete
  34. Hi,

    I am trying to edit iPhoneCodeSign.xcspec but it is empty when I open it with nano or vi with the terminal!!! and i dont have the local folder, i have / usr/bin !

    Macbook mb466

    Can somebody help me please?

    ReplyDelete
  35. If Apple has to replace your iphone 3 times, do they provide you with a free upgrade?

    ReplyDelete
  36. How can you the difference between an iphone 3g and an iphone 3gs?

    ReplyDelete
  37. How do you make a calculator app in Xcode calculate negative numbers?

    ReplyDelete