如何在我的iOS应用程序中以编程方式获取MAC地址

Bal*_*een 2 objective-c ios

我是Objective-C的新手,实际上我想在我的设备连接到WiFi时显示mac地址,我已经看到很多关于此的答案,但我无法得到我想要的东西.所以任何人都可以以教程的形式指导我.先感谢您.

Nim*_*ekh 6

ios 7及更高版本无法使用Mac地址.

Apple Said

在iOS 7及更高版本中,如果您要求提供iOS设备的MAC地址,系统将返回值02:00:00:00:00:00.如果需要识别设备,请改用UIDevice的identifierForVendor属性.(需要为自己的广告目的使用标识符的应用应考虑使用ASIdentifierManager的advertisingIdentifier属性.)

检查页面底部的Apple Document.

那么更好的解决方案.以下代码返回唯一地址.

#import "UIDevice+Identifier.h"

- (NSString *) identifierForVendor1
{
     if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
          return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
     }
     return @"";
}
Run Code Online (Sandbox Code Playgroud)

呼叫方法

NSString *like_UDID=[NSString stringWithFormat:@"%@",
                [[UIDevice currentDevice] identifierForVendor1]];

NSLog(@"%@",like_UDID);
Run Code Online (Sandbox Code Playgroud)

另外一个解决方案访问这里

编辑

的UIDevice + Identifier.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface UIDevice (IdentifierAddition)
- (NSString *) identifierForVendor1;
@end
Run Code Online (Sandbox Code Playgroud)

的UIDevice + Identifier.m

#import "UIDevice+Identifier.h"

@implementation UIDevice (IdentifierAddition)
- (NSString *) identifierForVendor1
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
        return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    }
    return @"";
}

@end
Run Code Online (Sandbox Code Playgroud)

调用上面的函数.

ViewController.m

#import "ViewController.h"
#import "UIDevice+Identifier.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *like_UDID=[NSString stringWithFormat:@"%@",
                         [[UIDevice currentDevice] identifierForVendor1]];


    NSLog(@"%@",like_UDID);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Run Code Online (Sandbox Code Playgroud)