Quickly testing iOS and objective-c code - iphone

I was wondering if is there any way to quickly check iOS/objective-c snippets or new code inside my xcode project without having to compile the whole app for that, open up the simulator just to get the NSLog message traced on the console.
I remember when life was easier using ruby's irb or node command for node.js :)
It just gives me the impression that on iOS development you have to learn how to deal with this high complexity and dependencies all the time, but I am just starting on this new world. so any help would be appreciated.
thanks a lot

This timely blog post should help; I'm using it often already.
Update: there is also Code Runner available in Mac App Store - this supports many languages, including Objective-C.

An Xcode project can contain multiple targets. One of the targets can be a test driver in which you can put some class/object/snippet exercise code and drive it from the command-line, results to the debug console. Another option is to create unit test targets.

If the code you're writing has known expected input-output pairs and you're just writing code to make them work, you may want to look into using XCode's unit testing capabilities. A link to a tutorial on using unit tests is at http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/UnitTesting/0-Introduction/introduction.html . If you want to test more interactive things, it is possible to use Instruments (a program in XCode) to automate those tests as well using the Automation instrument. A guide on using Instruments is available at http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/AboutTracing/AboutTracing.html .

Not really—Objective-C is a compiled language, while I believe Ruby is interpreted (which allows for interactive testing like what you describe). You can reduce some of the overhead of building and running your project by just leaving the simulator open; stopping the app from Xcode will close the app but leave the simulator running, meaning that a rebuilt version of the app will launch more quickly when you run it.

Related

How do I get code coverage for tests and build for iPhone in Xcode 4?

I have a couple of projects with unit tests where I want to measure coverage. So I follow the instructions detailed in the How-to for CoverStory and elsewhere on the web.
I add -fprofile-arcs and -ftest-coverage to my test target and the target under test and link to /Developer/usr/lib/libprofile_rt.dylib as specified.
And it works! When I run the tests scheme under the iPhone simulator, I get my code coverage, my tests and app build and run.
But then, when I go back to the Run scheme on actual iPhone hardware, I get a link error for libprofile_rt.dylib. Specifically, I get
ld: library not found for -lprofile_rt
What I suspect here is that libprofile_rt is actually a library for Mac hardware and the iPhone obviously can't link to a binary built for Mac, in general.
My question is How do run on iPhone hardware and get my code coverage?
I suspect I can do this with a copy of my Debug target that is only ever used for testing, but that has the additional trouble of maintaining yet another target. Is there any other way to get this working? Am I doing something silly?
might a little bit late but actually you can. it is explained in this presentation, slides 32 to 34.
took me a fair amount of time to find this hidden gem. hope this might help future visitor of this page.
Possible answer to this problem is here: https://stackoverflow.com/a/5140459
The trick is to get the *.gcda files out of the App's Documents directory via Xcode organizer, since they will, of course, be generated on device.
The answer is: you can't run on your device AND get coverage at the same time. The only way to get coverage is on the simulator: make a copy of your debug target, setup coverage and what not on that target and run that target, with your tests, in the simulator.
I go through all the details in an answer to this question on stackoverflow: Code coverage on iOS Using Xcode 4.2 on Lion

Does Apple ship debugging symbols for the iPhone Simulator APIs?

I am a bit of a iPhone/Mac OS newb.
When I look at a backtrace in the debugger and UIKit or other Apple Libraries are on the call stack I can't see their function arguments because of missing debug symbols.
Does Apple Ship debugging symbols for the iPhone Simulator libraries like UIKit?
Thanks.
No. The OS-level components do not ship with debug symbols, nor have those symbols available to you. On the Mac, the only case I can think of where a special debug version of a framework was made available was for Core Data, but that was more for logging of SQL and other database-related actions.
You can extract a little more information about what's going on in the Simulator using DTrace scripts and custom instruments, which can probe for even private methods and classes. For example, I created a couple of scripts and a custom instrument here that logs out every method that is called in the process of starting up an iPhone application in the Simulator. I describe how that works in this article on MacResearch.

Clean way to programmatically control Safari on iPhone?

The title say's almost all. What I have is short lived server processes that serves a web-based javascript test suite with optionally accompanying back-ends for integration tests (see here for how it's done). I'm looking for a better way to direct the iPhone Emulator's Safari to the URL given.
More, possibly optional background:
Up until iOS SDK 4.0 I've managed to control XCode using AppleScript to start a specially prepared PhoneGap project in this fashion:
AppleScript (referred to as "AS") starts XCode (referred to as "XC")
AS instructs XC to load the previously set up PhoneGap project.
AS instructs XC to launch the PhonePap project.
The PhoneGap project will redirect to the server serving the test suite, and the test suite runs.
In iOS 4.0 SDK, this only works the first round, the next time the test suite runner tries to make XC do the above routine, it fails requesting the previous run to be stopped. And I can't for the life of me find a way to stop the debugging session with AS [1], so I can only run one test suite without manual intervention, witch is pretty much a show stopper for us since we rely a lot on nightly test runs.
Now, what I really want to do is just start the iPhone Emulator, and then start Safari on the emulator with the possibility of sending an arbitrary URL to the emulated Safari.
What I've found so far is iphonesim, but I'm running into this issue and can't get it to properly start pre-compiled apps. A way to circumvent this issue in iphonesim would also be a good way forward.
[1] I'd accept a way to do this as a valid answer too.
I have a very different solution for you.
Write a custom iPhone app that just contains a web view. It will behave pretty much the same as MobileSafari. Then, embed a little web server in the app.
You can then control your app by calling specific urls on that server.
This is a very simple app. Will take you 30 minutes to put together.
This is easily integrated in Xcode or a bigger (Apple) script. You can use commands like curl to open URLs.
This is possible now:
xcrun simctl openurl booted "https://google.com"
https://apple.stackexchange.com/a/198798/144271
Post scriptum: how it was solved in the end.
A while after I asked this question, a user at github added a comment in issue 3 that what you need for iphonesim to work correctly is to give the absolute path to the application to start. So now we have a prebuilt PhoneGap project where a script modifies the contents of the www directory inside build/Debug-iphonesimulator/PhoneGap.app. The absolute path to this directory is then sent to iphonesim, starting up the project now pointing towards the correct server. No AppleScript and no XCode (except for the initial build). The test suite now takes 1/7 of the time it took to run earlier.

Equivalent to a Flash projector for iPhone?

Is there any equivalent to a Flash projector for iPhone? Flash projectors basically seem to package script and Flash libraries into one executable file that can be run on a PC. I'm wondering if anyone has made a similar thing for iPhone where I can take my existing code and package it with the necessary iPhone stuff to make a PC executable. Of course hardware-specific things would not be available like accelerometer/phone/gps, etc. but I don't need any of those. If not, is there anyone currently attempting this?
Thanks for the input guys, but I think everyone except Noah is misinterpreting my question. Flash was just an example, if you hate Flash just pretend I said something else. I am wondering if it is possible to make code for iPhone run on a PC in a similar way to the way a projector works for Flash.
Right now, the only way to run an iPhone app outside of an iDevice is to compile it from source for a non-iPhone target, so it depends on what you're using to compile. If you're authoring in XCode, you can target the Simulator (which is mac-only), but Apple doesn't currently have a way to compile for any other targets, or a way to compile an object file that runs on Macs without the simulator. If you're authoring in Flash CS5, of course you can just publish a projector.
Not at this time. However, Flash CS5 will create iPhone applications.
Flash is not available on the iPhone.
Furthermore, any company attempting to make a Flash runtime -- which would require doing bytecode interpretation -- would run up against Apple's developer agreement, which specifically forbids that.
I think Adobe is planning for CS5 the export to iPhone app feature. It essentially compiles flash's runtime to Apple's cocoa touch framework and produces a true iPhone app, thus circumventing the bytecode interpretation clause. Time will tell, time will tell...
--- Thanks for the clarification Myz... WTB Noah's reading skills, I thought you had typo'd the PC part due to the outrageousness of your question.
If by PC you mean a windows binary simulator interpreting .ipa files. No, such thing doesn't exist and I don't expect to see it for decades. The platform is much more harder to emulate than the old SNES/N64 and others.

XCode Test Automation For IPhone

I would like to have my iphone test app to be tested automatically in an IPhone. The following are the steps I would like to have:
compile, link and code sign the iphone app (Xcodebuild)
upload the newly built app to iphone
run the uploaded app in iphone automatically
collect the result from the gdb console
close the app
Right now, I have problem with step 2 and 3 where I cannot do it automatically (I can do it from XCode via "Build and Debug" button. This, however, will require manual clicking).
I did some research on automator and it does not answer my problem. Another option I am thinking about is to have the app compiled for iphone simulator and run it from there, but I am not sure how accurate the test result will be comparing to the real device.
I am new to Mac/IPhone development, perhaps someone has a better way of testing this. Any feedback and input are welcome. Thanks.
The comment section does not provide a good way of display the solution properly. Here is the summary of answer.
The task of building IPhone app, uploading and trigger the debug process on IPhone is done via AppleScript. Here is how the AppleScript looks like:
tell application "Xcode"
open "Users:chuan:Desktop:iphone_manual_client:iphone_manual_client.xcodeproj"
tell project "iphone_manual_client"
clean
build
(* for some reasons, debug will hang even the debug process has completed.
The try block is created to suppress the AppleEvent timeout error
*)
try
debug
end try
end tell
quit
end tell
AppleScript accepts ":" instead of "/" for file and folder separator.
The GDB console output can be captured by setting the GDB option to write it to file. this is done by typing the following command in Terminal:
defaults write com.apple.Xcode PBXGDBDebuggerLogToFile YES
defaults write com.apple.Xcode PBXGDBDebuggerLogFileName <path to my gdb output file>
Lastly, many thanks to various ppl who have helped to solve this problem.
The tool you probably want to use for the build and install is Applescript. Something like:
tell application "Xcode" to launch
I'm not pretending that this is a complete answer; there are still a lot of things to work out. But Applescript is going to be one of your key tools I believe.
Check out UISpec http://code.google.com/p/uispec/
It's a full automation test framework being developed for the iphone.
Check out iphone_testify, it works well with OCUnit and Google Toolbox For Mac, and I think it could be possible extend it for something like UISpec.
Regards
There's a command line tool "xcodebuild" you can call to kick off an XCode build without it being open. There are flags you can use to set targets and so on.
Dr Nic's testing with Ruby may help with some of this
Could you add a build phase to the target that runs a script to upload the binary to the iphone?
Right click the target, Add->New Build Phase->New Run Script Build Phase
xcodebuild will just build the binary and it will not upload the newly compiled binary to iphone.
actually it can upload (after signing it), but not run too bad...
maybe it can be run using gdb once connected to target but how (ip?, usb? usbnet?) ?