如何在iPhone Simulator中设置位置(在CoreLocation服务中获取)?
如何在iPhone Simulator中设置位置(在CoreLocation服务中获取)?
Answers:
从iOS 5开始,模拟器具有可配置的位置。
在“调试”菜单下,最后一个条目是“位置”;这为您提供了一个子菜单:
通过“自定义位置”,您可以输入纬度/经度值。自行车骑行,城市跑步和高速公路驾驶是对移动位置的模拟(当然,在库比蒂诺)。
当然,这对调试iOS 4(或更早版本)没有帮助。但这是绝对的进步!
在TextEdit文件中创建以下文件,例如将其命名为MyOffice。将扩展名设置为.gpx
<?xml version="1.0"?> <gpx version="1.0" creator="MyName"> <wpt lat="53.936166" lon="27.565370"> <name>MyOffice</name> </wpt> </gpx>
在Xcode中的“模拟”区域中选择 Add GPX File to Project...
在委托回调中,我检查是否正在模拟器中运行(#if TARGET_ IPHONE_SIMULATOR
),如果是,则提供自己的预先查找的纬度/经度。据我所知,没有其他办法。
Debug
在模拟器的菜单下有了一种使用Location
条目的方法,该条目允许设置自定义位置或从一些预定义的游乐设施和位置中进行选择。
在iOS模拟器菜单中,转到调试->位置->自定义位置。您可以在其中设置纬度和经度,并相应地测试应用程序。这适用于mapkit以及CLLocationManager。
Debug -> Location -> Custom Location
Features -> Location -> Custom Location
找出您拥有的XCode版本
$ /usr/bin/xcodebuild -version
在iOS 5之前的版本中,您可以通过以下代码来实现:
我@implementation
在需要虚假标题和位置数据的课程开始之前使用此代码段。
#if (TARGET_IPHONE_SIMULATOR)
@interface MyHeading : CLHeading
-(CLLocationDirection) magneticHeading;
-(CLLocationDirection) trueHeading;
@end
@implementation MyHeading
-(CLLocationDirection) magneticHeading { return 90; }
-(CLLocationDirection) trueHeading { return 91; }
@end
@implementation CLLocationManager (TemporaryLocationFix)
- (void)locationFix {
CLLocation *location = [[CLLocation alloc] initWithLatitude:55.932 longitude:12.321];
[[self delegate] locationManager:self didUpdateToLocation:location fromLocation:nil];
id heading = [[MyHeading alloc] init];
[[self delegate] locationManager:self didUpdateHeading: heading];
}
-(void)startUpdatingHeading {
[self performSelector:@selector(locationFix) withObject:nil afterDelay:0.1];
}
- (void)startUpdatingLocation {
[self performSelector:@selector(locationFix) withObject:nil afterDelay:0.1];
}
@end
#endif
在iOS 5之后,只需在项目中添加一个GPX文件,就可以不断更新位置Hillerød.gpx:
<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode">
<wpt lat="55.93619760" lon="12.29131930"></wpt>
<wpt lat="55.93625770" lon="12.29108330"></wpt>
<wpt lat="55.93631780" lon="12.29078290"></wpt>
<wpt lat="55.93642600" lon="12.29041810"></wpt>
<wpt lat="55.93653420" lon="12.28998890"></wpt>
<wpt lat="55.93660630" lon="12.28966710"></wpt>
<wpt lat="55.93670240" lon="12.28936670"></wpt>
<wpt lat="55.93677450" lon="12.28921650"></wpt>
<wpt lat="55.93709900" lon="12.28945250"></wpt>
<wpt lat="55.93747160" lon="12.28949540"></wpt>
<wpt lat="55.93770000" lon="12.28966710"></wpt>
<wpt lat="55.93785620" lon="12.28977440"></wpt>
<wpt lat="55.93809660" lon="12.28988170"></wpt>
<wpt lat="55.93832490" lon="12.28994600"></wpt>
<wpt lat="55.93845710" lon="12.28996750"></wpt>
<wpt lat="55.93856530" lon="12.29007480"></wpt>
<wpt lat="55.93872150" lon="12.29013910"></wpt>
<wpt lat="55.93886570" lon="12.28975290"></wpt>
<wpt lat="55.93898590" lon="12.28955980"></wpt>
<wpt lat="55.93910610" lon="12.28919500"></wpt>
<wpt lat="55.93861330" lon="12.28883020"></wpt>
<wpt lat="55.93845710" lon="12.28868000"></wpt>
<wpt lat="55.93827680" lon="12.28850840"></wpt>
<wpt lat="55.93809660" lon="12.28842250"></wpt>
<wpt lat="55.93796440" lon="12.28831520"></wpt>
<wpt lat="55.93780810" lon="12.28810070"></wpt>
<wpt lat="55.93755570" lon="12.28790760"></wpt>
<wpt lat="55.93739950" lon="12.28775730"></wpt>
<wpt lat="55.93726730" lon="12.28767150"></wpt>
<wpt lat="55.93707500" lon="12.28760710"></wpt>
<wpt lat="55.93690670" lon="12.28734970"></wpt>
<wpt lat="55.93675050" lon="12.28726380"></wpt>
<wpt lat="55.93649810" lon="12.28713510"></wpt>
<wpt lat="55.93625770" lon="12.28687760"></wpt>
<wpt lat="55.93596930" lon="12.28679180"></wpt>
<wpt lat="55.93587310" lon="12.28719940"></wpt>
<wpt lat="55.93575290" lon="12.28752130"></wpt>
<wpt lat="55.93564480" lon="12.28797190"></wpt>
<wpt lat="55.93554860" lon="12.28833670"></wpt>
<wpt lat="55.93550050" lon="12.28868000"></wpt>
<wpt lat="55.93535630" lon="12.28900190"></wpt>
<wpt lat="55.93515200" lon="12.28936670"></wpt>
<wpt lat="55.93505580" lon="12.28958120"></wpt>
<wpt lat="55.93481550" lon="12.29001040"></wpt>
<wpt lat="55.93468320" lon="12.29033230"></wpt>
<wpt lat="55.93452700" lon="12.29063270"></wpt>
<wpt lat="55.93438280" lon="12.29095450"></wpt>
<wpt lat="55.93425050" lon="12.29121200"></wpt>
<wpt lat="55.93413040" lon="12.29140520"></wpt>
<wpt lat="55.93401020" lon="12.29168410"></wpt>
<wpt lat="55.93389000" lon="12.29189870"></wpt>
<wpt lat="55.93372170" lon="12.29239220"></wpt>
<wpt lat="55.93385390" lon="12.29258530"></wpt>
<wpt lat="55.93409430" lon="12.29295010"></wpt>
<wpt lat="55.93421450" lon="12.29320760"></wpt>
<wpt lat="55.93433470" lon="12.29333630"></wpt>
<wpt lat="55.93445490" lon="12.29350800"></wpt>
<wpt lat="55.93463520" lon="12.29374400"></wpt>
<wpt lat="55.93479140" lon="12.29410880"></wpt>
<wpt lat="55.93491160" lon="12.29419460"></wpt>
<wpt lat="55.93515200" lon="12.29458090"></wpt>
<wpt lat="55.93545250" lon="12.29494570"></wpt>
<wpt lat="55.93571690" lon="12.29505300"></wpt>
<wpt lat="55.93593320" lon="12.29513880"></wpt>
<wpt lat="55.93617360" lon="12.29522460"></wpt>
<wpt lat="55.93622170" lon="12.29537480"></wpt>
<wpt lat="55.93713510" lon="12.29505300"></wpt>
<wpt lat="55.93776000" lon="12.29378700"></wpt>
<wpt lat="55.93904600" lon="12.29531040"></wpt>
<wpt lat="55.94004350" lon="12.29552500"></wpt>
<wpt lat="55.94023570" lon="12.29561090"></wpt>
<wpt lat="55.94019970" lon="12.29591130"></wpt>
<wpt lat="55.94017560" lon="12.29629750"></wpt>
<wpt lat="55.94017560" lon="12.29670520"></wpt>
<wpt lat="55.94017560" lon="12.29713430"></wpt>
<wpt lat="55.94019970" lon="12.29754200"></wpt>
<wpt lat="55.94024780" lon="12.29816430"></wpt>
<wpt lat="55.94051210" lon="12.29842180"></wpt>
<wpt lat="55.94084860" lon="12.29820720"></wpt>
<wpt lat="55.94105290" lon="12.29799270"></wpt>
<wpt lat="55.94123320" lon="12.29777810"></wpt>
<wpt lat="55.94140140" lon="12.29749910"></wpt>
<wpt lat="55.94142550" lon="12.29726310"></wpt>
<wpt lat="55.94147350" lon="12.29687690"></wpt>
<wpt lat="55.94155760" lon="12.29619020"></wpt>
<wpt lat="55.94161770" lon="12.29576110"></wpt>
<wpt lat="55.94148550" lon="12.29531040"></wpt>
<wpt lat="55.94093270" lon="12.29522460"></wpt>
<wpt lat="55.94041600" lon="12.29518170"></wpt>
<wpt lat="55.94056020" lon="12.29398010"></wpt>
<wpt lat="55.94024780" lon="12.29352950"></wpt>
<wpt lat="55.94001940" lon="12.29335780"></wpt>
<wpt lat="55.93992330" lon="12.29325050"></wpt>
<wpt lat="55.93969490" lon="12.29299300"></wpt>
<wpt lat="55.93952670" lon="12.29277840"></wpt>
<wpt lat="55.93928630" lon="12.29260680"></wpt>
<wpt lat="55.93915410" lon="12.29232780"></wpt>
<wpt lat="55.93928630" lon="12.29202740"></wpt>
<wpt lat="55.93933440" lon="12.29174850"></wpt>
<wpt lat="55.93947860" lon="12.29116910"></wpt>
<wpt lat="55.93965890" lon="12.29095450"></wpt>
<wpt lat="55.94001940" lon="12.29061120"></wpt>
<wpt lat="55.94041600" lon="12.29084730"></wpt>
<wpt lat="55.94076450" lon="12.29101890"></wpt>
<wpt lat="55.94080060" lon="12.29065410"></wpt>
<wpt lat="55.94086060" lon="12.29031080"></wpt>
<wpt lat="55.94092070" lon="12.28990310"></wpt>
<wpt lat="55.94099280" lon="12.28975290"></wpt>
<wpt lat="55.94119710" lon="12.28986020"></wpt>
<wpt lat="55.94134130" lon="12.28998890"></wpt>
<wpt lat="55.94147350" lon="12.29007480"></wpt>
<wpt lat="55.94166580" lon="12.29003190"></wpt>
<wpt lat="55.94176190" lon="12.28938810"></wpt>
<wpt lat="55.94183400" lon="12.28893750"></wpt>
<wpt lat="55.94194220" lon="12.28850840"></wpt>
<wpt lat="55.94199030" lon="12.28835820"></wpt>
<wpt lat="55.94215850" lon="12.28859420"></wpt>
<wpt lat="55.94250700" lon="12.28883020"></wpt>
<wpt lat="55.94267520" lon="12.28893750"></wpt>
<wpt lat="55.94284350" lon="12.28902330"></wpt>
<wpt lat="55.94304770" lon="12.28915210"></wpt>
<wpt lat="55.94325200" lon="12.28925940"></wpt>
<wpt lat="55.94348030" lon="12.28953830"></wpt>
<wpt lat="55.94366060" lon="12.28966710"></wpt>
<wpt lat="55.94388890" lon="12.28975290"></wpt>
<wpt lat="55.94399700" lon="12.28994600"></wpt>
<wpt lat="55.94379280" lon="12.29065410"></wpt>
<wpt lat="55.94364860" lon="12.29095450"></wpt>
<wpt lat="55.94350440" lon="12.29127640"></wpt>
<wpt lat="55.94340820" lon="12.29155540"></wpt>
<wpt lat="55.94331210" lon="12.29198450"></wpt>
<wpt lat="55.94315590" lon="12.29269260"></wpt>
<wpt lat="55.94310780" lon="12.29318610"></wpt>
<wpt lat="55.94301170" lon="12.29361530"></wpt>
<wpt lat="55.94292760" lon="12.29408740"></wpt>
<wpt lat="55.94290350" lon="12.29436630"></wpt>
<wpt lat="55.94287950" lon="12.29453800"></wpt>
<wpt lat="55.94283140" lon="12.29533190"></wpt>
<wpt lat="55.94274730" lon="12.29606150"></wpt>
<wpt lat="55.94278340" lon="12.29621170"></wpt>
<wpt lat="55.94280740" lon="12.29649060"></wpt>
<wpt lat="55.94284350" lon="12.29679100"></wpt>
<wpt lat="55.94284350" lon="12.29734890"></wpt>
<wpt lat="55.94308380" lon="12.29837890"></wpt>
<wpt lat="55.94315590" lon="12.29852910"></wpt>
<wpt lat="55.94263920" lon="12.29906550"></wpt>
<wpt lat="55.94237480" lon="12.29910850"></wpt>
<wpt lat="55.94220660" lon="12.29915140"></wpt>
<wpt lat="55.94208640" lon="12.29902260"></wpt>
<wpt lat="55.94196620" lon="12.29887240"></wpt>
<wpt lat="55.94176190" lon="12.29794970"></wpt>
<wpt lat="55.94156970" lon="12.29760640"></wpt>
</gpx>
我使用GPSies.com为gpx数据创建基本文件。不过需要一些清理。
通过运行模拟器并选择文件来激活
(来源:castleandersen.dk)
您可以轻松地将任何所需的位置从macOS Maps应用程序共享到Xcode模拟器。
这样,您可以快速测试某些位置,了解该位置的地理位置(例如,测试地理围栏),并在运行时确定下一步要去哪里(例如,调试和微调内容)。
在MacOS Catalina 10.15.4和Xcode 11.4上对此进行了测试。
您可以将gpx文件添加到项目中并使用它:
编辑方案>选项>允许位置模拟>选择包含以下内容的文件名:
<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode">
<wpt lat="41.92296" lon="-87.63892"></wpt>
</gpx>
(可选)只需对位置管理器返回的经/纬度值进行硬编码。这是旧样式。
因此您不会将其添加到模拟器中,而是添加到您的Xcode项目中。
迟到总比不到好 :)
我刚刚遇到了这个iSimulate,它允许您将虚假位置发送到应用程序。解决方案不是免费的。
> Q: How does iSimulate work?
> A: When added to your project, the iSimulate
> SDK library creates a listening server
> on your iPhone Simulator that waits
> for a connection from an iPhone/iPod
> running the iSimulate client. When
> such connection is established, the
> iSimulate client running on your
> iPhone/iPod captures all data from the
> accelerometer sensor, the touch
> events, the location and device ID and
> streams them to the server. The
> iSimulate SDK library then recreates
> all input events synthetically. This
> is entirely transparent to your
> application and does not interfere
> with your application's functionality.
无论如何,我正计划得到这个。将会更新更多!
您要在哪里设置位置?您可以使用mapkit api显示您的位置。有关如何使用mapkit的更多详细信息,请参见icodeblog.com。您也可以存储所需的坐标,只需创建一个对象CLLocation2D * location; location.longitude =您所需的经度值;location.latitude =您想要的纬度值;