• iOS NFC


    #import <CoreNFC/CoreNFC.h>
    
    
    @interface ViewController ()<NFCNDEFReaderSessionDelegate>
    
    @property (nonatomic,retain) UIButton *beginTestBtn;
    
    @property (nonatomic,retain) UILabel *textLabel;
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        _beginTestBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
        [_beginTestBtn setTitle:@"开始读取" forState:UIControlStateNormal];
        [_beginTestBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _beginTestBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        [_beginTestBtn addTarget:self action:@selector(beginTestBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
        
        _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 180, 200, 50)];
        _textLabel.text = @"待读取";
        _textLabel.textColor = [UIColor orangeColor];
        [self.view addSubview:_textLabel];
        
        NSLog(@"进入VC");
        
        [self.view addSubview:_beginTestBtn];
        [self.view addSubview:_textLabel];
        
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    -(void)beginTestBtnAction
    {
        
        /**
         三个参数
         第一个参数:代理对象
         第二个参数:线程
         第三个参数:Session读取一个还是多个NDEF。YES:读取一个结束,NO:读取多个
         */
        
        NFCNDEFReaderSession *session = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT) invalidateAfterFirstRead:YES];
        
        [session beginSession];
        
    }
    
    
    /**
     代理
     */
    - (void) readerSession:(nonnull NFCNDEFReaderSession *)session didDetectNDEFs:(nonnull NSArray<NFCNDEFMessage *> *)messages {
        
        __weak typeof(self) weakself=self;
        dispatch_async(dispatch_get_main_queue(), ^{
            weakself.textLabel.text = @"读取成功";
        });
        
        for (NFCNDEFMessage *message in messages) {
            for (NFCNDEFPayload *payload in message.records) {
                NSLog(@"Payload data:%@",payload.payload);
            }
        }
    }
    
    - (void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(NSError *)error{
        
         NSLog(@"error:%@",error);
        
        __weak typeof(self) weakself=self;
        dispatch_async(dispatch_get_main_queue(), ^{
            weakself.textLabel.text = @"读取失败";
        });
        
        
    }
  • 相关阅读:
    关于二叉树遍历的一点想法
    Mysqldump导入数据库很慢的解决办法
    javascript笔记收集
    再次讨论二叉树--如何根据先序和中序推选后序
    一道图的题目-拓扑序概念
    一道哈夫曼二叉树题目--稍微容易一点
    一道哈夫曼树的题目--好不容易
    证明二叉树节点数公式
    一道二叉树题目---顺序存储二叉树位置同层的关系
    POJ 3253 Fence Repair(贪心)
  • 原文地址:https://www.cnblogs.com/jukaiit/p/10501722.html
Copyright © 2020-2023  润新知