{"id":190928,"date":"2022-10-17T12:46:03","date_gmt":"2022-10-17T04:46:03","guid":{"rendered":"https:\/\/www.idc.net\/help\/190928\/"},"modified":"2022-10-17T12:46:03","modified_gmt":"2022-10-17T04:46:03","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91ios%e6%95%99%e7%a8%8b%ef%bc%9aios%e5%ae%9a%e4%bd%8d%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/190928\/","title":{"rendered":"\u540e\u6d6a\u4e91IOS\u6559\u7a0b\uff1aiOS\u5b9a\u4f4d\u64cd\u4f5c"},"content":{"rendered":"<h2>IOS\u5b9a\u4f4d\u64cd\u4f5c<\/h2>\n<hr>\n<h2>\u7b80\u4ecb<\/h2>\n<p>\u5728IOS\u4e2d\u901a\u8fc7CoreLocation\u5b9a\u4f4d\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u7528\u6237\u5f53\u524d\u4f4d\u7f6e\uff0c\u540c\u65f6\u80fd\u5f97\u5230\u88c5\u7f6e\u79fb\u52a8\u4fe1\u606f\u3002<\/p>\n<h3>\u5b9e\u4f8b\u6b65\u9aa4<\/h3>\n<p>1\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684View based application\uff08\u89c6\u56fe\u5e94\u7528\u7a0b\u5e8f\uff09\u3002<\/p>\n<p>2\u3001\u62e9\u9879\u76ee\u6587\u4ef6\uff0c\u7136\u540e\u9009\u62e9\u76ee\u6807\uff0c\u7136\u540e\u6dfb\u52a0CoreLocation.framework,\u5982\u4e0b\u6240\u793a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/atts.IDC.NET.cn\/attachments\/day_171121\/201711211709001646.jpg\" class=\"aligncenter\"><\/p>\n<p>3\u3001\u5728ViewController.xib\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u6807\u7b7e\uff0c\u521b\u5efaibOutlet\u540d\u4e3alatitudeLabel\u548clongtitudeLabel\u7684\u6807\u7b7e<\/p>\n<p>4\u3001\u73b0\u5728\u901a\u8fc7\u9009\u62e9\" File-&gt; New -&gt; File... -&gt; \"\u9009\u62e9Objective C class \u5e76\u5355\u51fb\u4e0b\u4e00\u6b65<\/p>\n<p>5\u3001\u628a\"sub class of\"\u4f5c\u4e3aNSObject\uff0c\u5c06\u7c7b\u547d\u540d\u4e3aLocationHandler<\/p>\n<p>6\u3001\u9009\u62e9\u521b\u5efa<\/p>\n<p>7\u3001\u66f4\u65b0LocationHandler.h\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>#import &lt;Foundation\/Foundation.h&gt;\n#import &lt;CoreLocation\/CoreLocation.h&gt;\n\n@protocol LocationHandlerDelegate &lt;NSObject&gt;\n\n@required\n-(void) didUpdateToLocation:(CLLocation*)newLocation \n fromLocation:(CLLocation*)oldLocation;\n@end\n\n@interface LocationHandler : NSObject&lt;CLLocationManagerDelegate&gt;\n{\n    CLLocationManager *locationManager;\n}\n@property(nonatomic,strong) id&lt;LocationHandlerDelegate&gt; delegate;\n\n+(id)getSharedInstance;\n-(void)startUpdating;\n-(void) stopUpdating;\n\n@end\n<\/pre>\n<p>8\u3001\u66f4\u65b0LocationHandler.m,\u5982\u4e0b\u6240\u793a <\/p>\n<pre>#import \"LocationHandler.h\"\nstatic LocationHandler *DefaultManager = nil;\n\n@interface LocationHandler()\n\n-(void)initiate;\n\n@end\n\n@implementation LocationHandler\n\n+(id)getSharedInstance{\n    if (!DefaultManager) {\n        DefaultManager = [[self allocWithZone:NULL]init];\n        [DefaultManager initiate];\n    }\n    return DefaultManager;\n}\n-(void)initiate{\n    locationManager = [[CLLocationManager alloc]init];\n    locationManager.delegate = self;\n}\n\n-(void)startUpdating{\n    [locationManager startUpdatingLocation];\n}\n\n-(void) stopUpdating{\n    [locationManager stopUpdatingLocation];\n}\n-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:\n (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{\n    if ([self.delegate respondsToSelector:@selector\n    (didUpdateToLocation:fromLocation:)]) \n    {\n        [self.delegate didUpdateToLocation:oldLocation \n        fromLocation:newLocation];\n\n    }\n}\n\n@end\n<\/pre>\n<p>9\u3001\u66f4\u65b0ViewController.h,\u5982\u4e0b\u6240\u793a<\/p>\n<pre>#import &lt;UIKit\/UIKit.h&gt;\n#import \"LocationHandler.h\"\n@interface ViewController : UIViewController&lt;LocationHandlerDelegate&gt;\n{\n    IBOutlet UILabel *latitudeLabel;\n    IBOutlet UILabel *longitudeLabel;\n}\n@end\n<\/pre>\n<p>10\u3001\u66f4\u65b0ViewController.m,\u5982\u4e0b\u6240\u793a<\/p>\n<pre>#import \"ViewController.h\"\n\n@interface ViewController ()\n\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    [[LocationHandler getSharedInstance]setDelegate:self];\n    [[LocationHandler getSharedInstance]startUpdating];\n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n-(void)didUpdateToLocation:(CLLocation *)newLocation \n fromLocation:(CLLocation *)oldLocation{\n    [latitudeLabel setText:[NSString stringWithFormat:\n    @\"Latitude: %f\",newLocation.coordinate.latitude]];\n    [longitudeLabel setText:[NSString stringWithFormat:\n    @\"Longitude: %f\",newLocation.coordinate.longitude]];\n\n}\n\n@end\n<\/pre>\n<h3>\u8f93\u51fa<\/h3>\n<p>\u5f53\u6211\u4eec\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa:<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/atts.IDC.NET.cn\/attachments\/day_171121\/201711211709102328.jpg\" class=\"aligncenter\"><\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>IOS\u5b9a\u4f4d\u64cd\u4f5c \u7b80\u4ecb \u5728IOS\u4e2d\u901a\u8fc7CoreLocation\u5b9a\u4f4d\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u7528\u6237\u5f53\u524d\u4f4d\u7f6e\uff0c\u540c\u65f6\u80fd\u5f97\u5230\u88c5\u7f6e\u79fb\u52a8\u4fe1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":190929,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189686],"tags":[],"class_list":["post-190928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=190928"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/190928\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/190929"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=190928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=190928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=190928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}