我需要知道是否可以在iOS中使用wifi网络获取用户位置。
我读到了重要位置更改的方法,但我不知道用户是否有必要连接到无线网络,或者只是当de设备检测到事件周围的新wifi网络被触发时。
如你所见,我不知道这个主题,有人可以指导我一点吗?
答案 0 :(得分:-1)
Used Reachability class
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;
Call below method in didFinishLaunchingWithOptions
-(void)makeConnectionOverRechability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; //When intenet connectivity change this method call
NSString *remoteHostName = @"www.apple.com";
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[self.hostReachability startNotifier];
[self updateInterfaceWithReachability:self.hostReachability];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
self.wifiReachability = [Reachability reachabilityForLocalWiFi];
[self.wifiReachability startNotifier];
[self updateInterfaceWithReachability:self.wifiReachability];
}
#pragma mark-
#pragma mark Internect Connectivity
-(BOOL)checkInternetConnection
{
Reachability *reach = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [reach currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
return internetStatus;
}
return internetStatus;
}
- (void) reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
if (reachability == self.hostReachability)
{
}
if (reachability == self.internetReachability)
{
[self configureTextField:reachability];
}
if (reachability == self.wifiReachability)
{
[self configureTextField:reachability];
}
}
- (void)configureTextField:(Reachability *)reachability
{
NetworkStatus netStatus = [reachability currentReachabilityStatus];
//check here only wifi connection
switch (netStatus)
{
case NotReachable: {
NSLog(@"not Connected+++++++++++");
break;
}
case ReachableViaWWAN: {
NSLog(@"Connected");
break;
}
case ReachableViaWiFi: {
NSLog(@"Connected");
}
break;
}
}