launching iOS App from Mac OS X console - ios

in order to automate testing an application, I want to repeatedly launch an iOS app with different arguments to it's main function from my mac; for simplicity let's just say it takes a file as argc and I have 100 files that I need to test. In theory, all I'd want to reproduce is the behaviour of xcode build & run - on the device, not jailbroken; the app is closely tied to the hardware, so the simulator alone will not suffice.
There are a couple of pages that do something similar, but none of them use the device, and none of them actually spell out how the command line on the console would actually look like.
1) http://blog.carbonfive.com/2011/04/06/running-xcode-4-unit-tests-from-the-command-line/
this page sets up a unit test that eventually runs in the simulator; he goes on to say that
The “Run Script” build phase of a unit test build target just runs >“${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests”
I checked that, but neither RunPlatformUnitTests nor RunUnitTests is executed from Xcode 4 (I renamed the scripts to make sure).
2) http://longweekendmobile.com/2011/04/17/xcode4-running-application-tests-from-the-command-line-in-ios/
this page picks up where 1) left but only builds for the simulator and doesn't specify how the app is actually launched. It describes well how it's build from command line though.
3) Debugging iOS app built from commandline
What I see in Xcode 4 is the build phase, which stops at signing and validating the application. The next thing I see is the gdb debugger that runs on the device. However, I don't know how to access debugserver - I didn't jailbrake my iPhone so I have no access to just about anything on it, neither debugserver nor the IP adress.
What I can verify is that gdb is called from xcode4/platforms/iPhoneOS.platform so the remote debugger must attach itself to something, somehow ...

For command line remote GDB (or just upload to device) you can use this little tool, choose the fork closest to what you need, the original author will not support it anymore:
https://github.com/ghughes/fruitstrap
You may not be able to use it as-is (I had to tweak the gdb parameters for my use) but its a good start.
You can further automate things by automatically run this script
for i in $(system_profiler SPUSBDataType | grep "Serial Number: " | grep -Po "[A-Za-z0-9]{40,}$") ; do
echo "Installing on: $i"
#use fruitstrap with device $i
end
this will simply check all USB port and keep only the ones that are currently connected to an iOS device and return its UDID.
From there you should be able to automate the testing with several different arguments for your app.

Related

Is there any way to run unit tests in Xcode without building and launching the simulator?

I'm in a very early stage of developing an iOS application, and right now, I just want to test the logic of my Swift code. I don't care about the actual app yet. I've written some unit tests in my tests file, but whenever I run the test suite, it launches the simulator with an iPod Touch as the target device, even though it has nothing to do with my tests for now. Is there a way to disable that?
I've tried going to Project > General and setting the Host Application to 'None' for tests, but it doesn't seem to be doing anything. And I don't know if this is relevant but I also turned off AuthenticationUITests under Product > Scheme > Edit Scheme.
This was the most relevant post I could come across regarding this topic, but it seems to be outdated because I can't find Bundle Loader and Test Host in that place anymore.
I also tried using xctool as per this post, but when I try brew install xctool, it says:
Warning: xctool has been deprecated because it has an archived upstream repository!
Error: xctool: no bottle available!

How can I debug multi-player game using Firebase and Swift? [duplicate]

I'm planning out a game that requires multiple users and I was wondering if there's a way to fire up multiple instances of the iPhone Simulator. I didn't see anything in menus or in Google search results, but I just wanted to make sure I wasn't missing anything.
It is possible but only with multiple users.
Read this: http://www.coderebel.com/2010/08/31/iphone_simulator
Assuming you have two projects (P1,P2) which you want to debug simultaneously, do as follows.
Steps to set up:
Create a new user (U2) on your Mac
Give it R/W access to P2, especially to its build folder.
Download the launcher app from the site above and copy it to Applications.
Steps to debug:
Close Xcode and the simulator if running
Launch the iPhone Launcher.app with U1.
Start Xcode with U1, load P1, start debugging.
Launch the iPhone Launcher.app with U2.
Start Xcode with U2, load P2, start debugging.
Unfortunately it's a P.I.T.A. having to change users every now and then but so far I haven't found any better solution. Hope this helps.
There is a way to do this easily without using multiple users. Keep in mind you will not be connected to the debugger though. Keep in mind the explanation below is for XCode 6+. You can skip this explanation and just scroll down to the wrapping-it-up section below.
From an answer here we can see that you can launch any simulator from command line:
open -a "iOS Simulator" --args -CurrentDeviceUDID
You can find the deviceID (as well as all your simulators) by running the xcrun simctl list command on console. It will generate a list like this. The device ID is the code in braces:
== Runtimes ==
iOS 8.3 (8.3 - 12F69) (com.apple.CoreSimulator.SimRuntime.iOS-8-3)
== Devices ==
-- iOS 8.3 --
iPhone 4s (99Z06AC6-A2D6-46E7-B4F7-BA4F5F3F39C8) (Shutdown)
iPhone 5 (K262AF11-ADD2-4FDA-ACBA-8C80DD9D4AA1) (Shutdown)
iPhone 5s (337KDC51-0A4B-47DB-8966-83562FD92C93) (Shutdown)
iPhone 6 Plus (9GK714E2-F713-4F98-A96E-C72ACD6571A8) (Shutdown)
-- iOS 8.2 --
etc continued....
Therefore to run the simulator using above as example, run this command on console:
open -n /Applications/Xcode.app/Contents/Developer/Applications/iOS\ Simulator.app/ --args -CurrentDeviceUDID '99Z06AC6-A2D6-46E7-B4F7-BA4F5F3F39C8'
We use the -n flag instead because the -a flag specifies the application to use for opening the file, while the -n flag opens a new instance of the application(s) even if one is already running. Use the man open command to know all this stuff.
Also make sure you have the correct path for your simulator.
One more thing to note, if you run the simulator like this, chances are that you have limited simulators. And since its limited, you might want to launch it without interrupting one that's already on the screen. We can solve this from this answer here where he shows you how to create a new simulator and delete one.
So wrapping it all up, here's what you do to launch them.
A) First Create a simulator before launching it.
Usage: simctl create "name" "device type id" "runtime id"
xcrun simctl create "mynewsimulator" "iPhone 6" "com.apple.CoreSimulator.SimRuntime.iOS-8-3"
You will find the runtime id from the command xcrun simctl list command I ran above. After running the above command, the deviceID will be printed on the console screen. Take note of it as you will need as the last argument to launch the simulator shown below. Let's assume this ID was 99Z06AC6-A2D6-46E7-B4F7-BA4F5F3F39C8
B) launch it
open -n /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/ --args -CurrentDeviceUDID '99Z06AC6-A2D6-46E7-B4F7-BA4F5F3F39C8'
C) delete it once you're finished with your simulator
xcrun simctl delete 99Z06AC6-A2D6-46E7-B4F7-BA4F5F3F39C8
All this can be placed in a single .sh file for easy running.
Please note to put wait or sleep commands after each command if you decide to put all this into a sh file.
[EDIT 2017]
I see that XCode 9 will now support multiple simulators. Awaiting to see how it will all work.
As near as I can tell, not only can you not have multiple instances of the simulator running, but when time comes for actual device testing you can't have multiple instances of the debugger running either.
This means to do multi-device apps, you'll need to have a single Mac assigned to a single iPhone/touch device (or simulator instance) each running your app.
I've started looking into whether VMWare or Parallels can somehow be brought into this, but there's not much that can be done without hacking the OS.
I suggest filing a feature request with http://bugreporter.apple.com.
Another option (albeit probably overkill) if you have a Mac OS X server license is to virtualize an instance of Mac OS X Server, and run a second simulator in the VM.
Hmm...I don't think you are missing anything here. As far as I know there is no technology within the SDK that is able to emulate multiple devices -- though that would be a useful feature to have. I think your only options would be to get a second iPhone OS device, or get a friend who also has an iPhone OS device to let you use it for testing.
In XCode 4 you can run multiple instances while having both be in debug mode.
What I did was create a duplicate of my target and ran both the original and the duplicate. If you're using the output window you can toggle which output you are looking at.
Follow these steps to test your app.
You have to do one thing for whole process first :
Go to Project - > Edit Project Settings -> tick on option - Build independent targets in parallel.
Debug code that will create your app in simulator For Example your App name is - Instance
Close Simulator
Go to Project->New Target -> Instance2
Add "Bundle display name : Instance2" in Instance2.info.plist file
Set Executable Name : Instance2
Select Instance2 Target -> Go to Project -> Set Active Target -> Instance2
Build with Instance2
Note : Do all carefully otherwise you have to reset your simulator
For Reset Simulator -> Run Simulator -> iPhone Simulator -> Reset Content & Settings...
Now You have two apps in simulator and you can test app on all instance.
Hope it will help you :)
I managed to get it to work combining answer by Ivsty with this one.
Basically, what you do is create two users (A and B), run simulators in each of them, run VNC server on user B, and then connect from user A to access screen B without needing to switch users.
Yes, now it's possible and is also quite easy.
The only thing you need is xctool, the build tools from Facebook and a ruby gem. The ruby gem helps to create/destroy the simulator needed during the test.
You can read the full documentation on this link:
https://github.com/plu/parallel_ios_tests
I'm using this approach and I run my test on 4 iOS simulator on the same time.
Supported from Xcode 9 on.
(Source: WWDC 2017)

Is it possible to run an app on two devices with one click on the build and run button in Xcode 5?

Is it possible to run an app on two devices with one click on the build and run button?
E.g. I have an application that has to run on two devices to test some networking functions between the devices.
It is really annoying to select each device separately from the scheme selector and then click the "build and run"-button.
So I'd like to configure my build process to let the app automatically build and run on both devices. Is this possible? And if it is, how?
Versions: Xcode5, OSX Mavericks
I found this Question but it doesn't seem to work anymore since I get the the following error message when I open the .app file:
You can’t open the application “Run” because the Classic environment is no longer supported.
Furthermore I can't select the .app file for the launch application service in the automator.
I don't know if it's possible, but perhaps to speed it up, a quick keyboard shortcut:
Switch build target HOLD: cmd + opt + ctrl THEN PRESS [ or ] -- switches target
Then, build it with cmd + r
Not exactly what you're looking for, but should cut your time fairly significantly!
Choose your Device at the top, build and run (Cmnd+R) and while running it, select a different device without stopping the other device, then build and run it again (Cmnd+R) on that device.
It definitely works with a real device and a simulator, but I don't know about 2 real devices.
Goodluck! :)

Xcode: error: failed to launch with duplicated target. Original target runs fine in Xcode/gdb

I have an app that I want to be able to build two different versions. One a "test" version that hits test databases, populates a test analytics backend, etc, and then the "live" version which its the production database and analytics backend.
So I duplicated my target, made appropriate changes, and I can build both, and both get installed on the phone, and I can run both manually on the phone. However, when I try to run it in Xcode on the device in order to debug with gdb or lldb (same error happens with both), the original target runs fine. However, the duplicated target (the "test" one) gives an error when running on the device (both run fine in the simulator, the problem is only running on the device)
lldb gives: [APPNAME] is the overall app name -- the test version has product set to [APPNAME]Test
error: failed to launch '/Users/chad/Library/Developer/Xcode/DerivedData/[PROJNAME]-ezbuaazlwmgdwydjykcufhicaspu/Build/Products/Debug-iphoneos/[APPNAME]Test.app/[APPNAME]Test'
gdb basically gives a dialog box that says that the same path cannot be found.
However, that path actually DOES exist. If I cut and past the path from the Xcode debugger window and go to Terminal and type % ls -a '/Users/chad/Library/Developer/Xcode/DerivedData/[PROJNAME]-ezbuaazlwmgdwydjykcufhicaspu/Build/Products/Debug-iphoneos/[APPNAME]Test.app/[APPNAME]Test'
it finds it just fine. I can also click in Xcode in the file list under the Products folder and click on [APPNAME]Test.app and choose in the contextual menu to "Show in Finder" and it finds it just fine.
AND the app does get copied to the phone and it shows up in the organizer and I can manually launch the app and it runs on the device. It just won't auto launch in Xcode on the device for the copied target, while the original target runs just fine in Xcode.
I'd appreciate any insight into this. This is my first real time playing with multiple targets on an iPhone project I created (versus working on a team on a project someone else created). Thanks.
Wouldn't you know. Xcode crashed (for some totally other reason when I was doing something different unrelated to this) and now both targets will launch and run from Xcode. So I guess the solution is to quit Xcode after duplicating a target if you have problems with the target launching from Xcode.

Install iOS app into xcode simulator?

We're iOS (and other mobile platform) developers and our sales folks routinely need to provide demos of our apps for clients. What we're trying to do is automate a process so sales people can go to a self-serve website and feed the app into their simulator.
Using xcodebuild we can kick off the build process and then present it as a secure download link (via our intranet) but all that lets them do is install it to their iOS device. This is OK except not everyone on the road has an iOS device (or some have old iPod touches, and that's super slow). So, I was thinking that there's gotta be a way to get it installed in their simulator.
I see that others have hacked it in there by zipping up a simulator directory and placing it on another:
http://discussions.apple.com/thread.jspa?messageID=7680994
Set target to simulator, release.
deploy. stop iphone simulator. zip
your app from ~/Library/Application
Support/iPhone
Simulator/User/Applications/ send it
to someone else, and let that person
know to unzip it in that folder, than
start iphone simulator - and you're
done.
I guess we can do this, but it doesn't seem deterministic (or at least, a lot harder to script). I'd prefer to work with the .app but if the only way to do it is with this hacky copy and paste operation, that'll be what we have to do.
Any thoughts?
This could work: https://github.com/landonf/simlaunch/
I have not tried it yet by myself, but from video looks like the application converts .app compiled for simulator to a launcher, which can be used to run the app in simulator without installing it through xcode.
It probably uses the same kind of approach for simulating the app without installing it first that is described here: How to install iPhone application in iPhone Simulator
I know this isn't a direct answer to your question, but have you thought about having a server (eg a Mac mini) that has all the installed software on it? Then they could just use some form of screen sharing to run the app remotely.
I've used the simulator build mechanism above with a number of clients and once they figure out the right folder to drop the actual folder into, it's gone very well.
It's a little bit of a hack as you say but in some cases it also beats having to deal with ad hoc provisioning etc when it's less important to have it on an actual device itself.
I haven't tried it yet but I've heard pretty good things about Test Flight (http://testflightapp.com/) also as a way to distribute demos/betas so that might be worth a look as well.
I just developed a pair of scripts to (a) extract an app from a iOS simulator, and (b) install it on another iOS simulator. https://github.com/christopher-prince/SimAppScripts
Locate the .app bundle in your built products directory, typically under Debug-iphonesimulator or Release-iphonesimulator. Drag and drop that bundle onto the Simulator window and it will be installed (you may need to swipe on the home screen to show the icon).
You can also script this from the command line using xcrun simctl install. The xcrun simctl launch command can also start the application.
Most build systems can be automated (or you can use a post-build script from within Xcode, ordered as the last step) to create a ZIP of the app bundle and put it somewhere for download.
(If having the Archive workflow support creating Simulator IPAs would be useful for you please file an enhancement request at https://bugreport.apple.com)
check out waxsim:
https://github.com/square/WaxSim
http://cocoamanifest.net/articles/2011/12/running-your-ios-app-in-the-simulator-from-the-command-line.html
If you build it for i386 and distribute the .app, I believe this will work.

Resources