以编程方式设置iPhone模拟器位置

10

我刚刚更新到XCode 4.2,发现有一个很酷的功能可以手动设置设备位置。请问有没有办法以编程方式实现相同的功能?我想在一些单元测试中设置位置。


https://dev59.com/bXVC5IYBdhLWcg3wrDRd - Matt Ball
1
我不认为那会有帮助。它所说的是要覆盖CLLocationManager的回调函数。但是我遇到的问题是,在我的单元测试中,回调根本没有出现。我希望有类似这样的东西:[NSApplication setLatitude:lat longitude:lng]。 - JonnyBoy
那么,@JonnyBoy,你解决了这个问题吗?我也很想使用它! - abbood
@abbood 我没有真正满意的解决方案... - JonnyBoy
2个回答

10
下面的AppleScript将让您设置iOS模拟器的位置。这种类型的脚本应该可以集成到单元测试脚本中,或者让您的脚本生成等效的AppleEvents。
tell application "iOS Simulator"
    activate
end tell

tell application "System Events"
    tell process "iOS Simulator"
        tell menu bar 1
            tell menu bar item "Debug"
                tell menu "Debug"
                    tell menu item "Location"
                        click
                        tell menu "Location"
                            click menu item "Custom Location…"
                        end tell
                    end tell
                end tell
            end tell
        end tell

        tell window 1
            set value of text field 1 to "40.69"
            set value of text field 2 to "-74.045"
            click button "OK"
        end tell

    end tell
end tell

听起来不错。虽然我已经不再使用iOS,但是我会把这个交给你,因为它看起来可以达到我所要求的功能。也许还有其他人可以验证一下? - JonnyBoy
iOS Simulator 应该改为 SimulatorDebug 改为 Features。激活模拟器需要2分钟,但我不知道原因。 - matebende

2

您可以使用预处理条件包含;像这样检查TARGET_IPHONE_SIMULATOR宏:

#if TARGET_IPHONE_SIMULATOR
    float longitude = 39.1234;
    // etc    
#else
    float longitude = myLocationManager.longitude
#endif

2
我想我表达不够清楚。我正在寻找一种方法,使CLLocationManager实例能够像在iPhone模拟器上单击Debug->Location->Custom Location一样发送一个http://developer.apple.com/library/ios/DOCUMENTATION/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didUpdateToLocation:fromLocation:。看起来它所做的只是设置一个浮点数。 - JonnyBoy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接