通过使用这个 Cocoapod 连接到物理网,将 UriBeacon 功能添加到您的应用中。
pod 'UriBeacon'
在您的项目目录中运行:
pod init
打开新建的 Podfile 并添加以下行:
# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
use_frameworks!
target '<<App Name Here>>' do
pod 'UriBeacon'
end
现在下载并安装 Pod:
pod install
这里是一个示例视图控制器:
//
// ViewController.swift
// Physical Web
//
// Created by Tanner Nelson on 3/31/15.
// Copyright (c) 2015 Blue Bite. All rights reserved.
//
import UIKit
import UriBeacon
class ViewController: UIViewController, UriBeacon.DiscoveryDelegate {
var beaconScanner: UriBeacon.Scanner?
override func viewDidLoad() {
super.viewDidLoad()
self.beaconScanner = UriBeacon.Scanner(delegate: self)
self.beaconScanner?.startScanning()
}
func updateURIs() {
print("URIs: ")
println(self.beaconScanner?.uris)
}
func scanner(scanner: Scanner, discoveredURI: URI) {
self.updateURIs()
}
func scanner(scanner: Scanner, lostURI: URI) {
self.updateURIs()
}
}
注意,您可以直接通过添加以下代码使用 Google 的 UriBeacon SDK:
import GoogleUriBeacon
这为您提供了对所有底层 Google SDK 类的访问,如 UBUriBeacon 和 UBUriBeaconScanner。