xcode 检测无线网络名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13195717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:00:00  来源:igfitidea点击:

Detect the name of wireless network

iphoneiosxcode

提问by Charles Vincent

Is it possible to run a method that will return the name of the wireless network that the user is connected to? Inside of my app I want to be able to return the name of the wireless network that the user is connected to.

是否可以运行一种方法来返回用户所连接的无线网络的名称?在我的应用程序内部,我希望能够返回用户连接到的无线网络的名称。

回答by Charles Vincent

This worked perfect for me:

这对我来说很完美:

#import <SystemConfiguration/CaptiveNetwork.h>

CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
//    NSLog(@"SSID: %@",CFDictionaryGetValue(myDict, kCNNetworkInfoKeySSID));
NSString *networkName = CFDictionaryGetValue(myDict, kCNNetworkInfoKeySSID);

if ([networkName isEqualToString:@"Hot Dog"])
{
    self.storeNameController = [[StoreDataController alloc] init];
    [self.storeNameController addStoreNamesObject];
}
else {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Connection Failed"
                                                   message: @"Please connect to the Hot Dog network and try again"
                                                  delegate: self
                                         cancelButtonTitle: @"Close"
                                         otherButtonTitles: nil];

    [alert show];

回答by Shamsudheen TK

From Developer.apple, you can use CNCopyCurrentNetworkInfo

Developer.apple,您可以使用CNCopyCurrentNetworkInfo

It Returns the current network info for a given network interface.

它返回给定网络接口的当前网络信息。

CFDictionaryRef CNCopyCurrentNetworkInfo (
   CFStringRef interfaceName
);

It contains a dictionary that containing the interface's current network info. Ownership follows the Create Rule.

它包含一个字典,其中包含接口的当前网络信息。所有权遵循创建规则。

Note:Available in iOS 4.1 and later.

注意:适用于 iOS 4.1 及更高版本

EXAMPLE:

例子:

This example will work fine in real device, It may crash in simulator.

这个例子在真实设备上可以正常工作,在模拟器中可能会崩溃。

Add SystemConfiguration.framework

添加SystemConfiguration.framework

ImportCaptiveNetworkheader same as below

导入CaptiveNetwork头与下面相同

#import <SystemConfiguration/CaptiveNetwork.h>

#import <SystemConfiguration/CaptiveNetwork.h>

Then write the below code.

然后编写下面的代码。

    CFArrayRef myArray = CNCopySupportedInterfaces();
    // Get the dictionary containing the captive network infomation
    CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));    
    NSLog(@"Information of the network we're connected to: %@", captiveNtwrkDict);    
    NSDictionary *dict = (__bridge NSDictionary*) captiveNtwrkDict;
    NSString* ssid = [dict objectForKey:@"SSID"];
    NSLog(@"network name: %@",ssid);

or

或者

Using Bonjour, the application both advertises itself on the local network and displays a list of other instances of this application on the network

使用Bonjour,应用程序既在本地网络上宣传自己,又在网络上显示此应用程序的其他实例的列表

See the sample Witapapplication

查看示例Witap应用程序

回答by Ron Reuter

The BAD NEWS: 'CNCopyCurrentNetworkInfo' is deprecated: first deprecated in iOS 9.0- For captive network applications, this has been completely replaced by <NetworkExtension/NEHotspotHelper.h>. For other applications, there is no direct replacement. Please file a bug describing your use of this API to that we can consider your requirements as this situation evolves.

坏消息:'CNCopyCurrentNetworkInfo' 已被弃用:在 iOS 9.0 中首次被弃用- 对于强制网络应用程序,这已被 <NetworkExtension/NEHotspotHelper.h> 完全取代。对于其他应用程序,没有直接替换。请提交描述您使用此 API 的错误,以便我们可以随着这种情况的发展考虑您的要求。

The GOOD NEWS: although deprecated, it still works in iOS 9, AND it has been un-deprecated in iOS 10.

好消息:虽然已被弃用,但它仍可在 iOS 9 中使用,并且在 iOS 10 中未弃用。

It still works in iOS 13, but only if the app implements Location Services and the user authorizes their usage.

它仍然适用于 iOS 13,但前提是该应用程序实现了定位服务并且用户授权其使用。