Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
swift,在后台检测ibeacons,并在范围内发送通知_Swift_Ibeacon - Fatal编程技术网

swift,在后台检测ibeacons,并在范围内发送通知

swift,在后台检测ibeacons,并在范围内发送通知,swift,ibeacon,Swift,Ibeacon,您好,我是IBeacons的新手,也是swift的初学者,我正在尝试制作一个小应用程序,在应用程序的背景上检测Ibeacon,并在Ibeacon在范围内时发送通知。我成功地做到了这一点,但只有当我在应用程序打开时走路时,我才能让它工作,并在背景上搜索IBeacons,即使我给了应用程序通过使用在后台获取位置的访问权限 if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedAlways) {

您好,我是IBeacons的新手,也是swift的初学者,我正在尝试制作一个小应用程序,在应用程序的背景上检测Ibeacon,并在Ibeacon在范围内时发送通知。我成功地做到了这一点,但只有当我在应用程序打开时走路时,我才能让它工作,并在背景上搜索IBeacons,即使我给了应用程序通过使用在后台获取位置的访问权限

 if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedAlways) {
        locationManager.requestAlwaysAuthorization();
    }
这是我的主要问题,我也有一个副问题,即应用程序不保存人名,如果应用程序关闭并再次打开,应用程序将忘记人名。这是我的代码,我非常感谢您的帮助。如果您有任何参考资料来了解更多关于IBeacons应用程序的信息,我将不胜感激

   import UIKit
   import CoreLocation
   import UserNotifications

   class ViewController: UIViewController, CLLocationManagerDelegate {
   @IBOutlet weak var field: UITextField!
   @IBOutlet weak var textLbl : UILabel!
   var inRoom = false
   var name = ""
   var sendYet = false ;

func sendHiNoti()
{
    name = field.text! ;
    let content = UNMutableNotificationContent()
    content.title = "Heloo "+name
    content.subtitle = "Welcome to your Home"
    content.body = "this messaage to welcome you in Home"
    content.badge = 1
    content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "quiteimpressed.mp3"))
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
    let request = UNNotificationRequest(identifier: "azanSoon", content: content, trigger: trigger)
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("error: \(error)")
        }
    }
}

func sendByeNoti()
{
    name = field.text! ;
    let content = UNMutableNotificationContent()
    content.title = "OH"+name
    content.subtitle = "You are going out already ??"
    content.body = "Take care of your self"
    content.badge = 1
    content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "quiteimpressed.mp3"))
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
    let request = UNNotificationRequest(identifier: "azanSoon", content: content, trigger: trigger)
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("error: \(error)")
        }
    }
}

@IBAction func getText(){
    name = field.text!
    let alert = UIAlertController(title: "Your Name is", message: name, preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
    self.present(alert, animated: true, completion: nil)

}


var locationManager = CLLocationManager()

override func viewDidLoad() {

    super.viewDidLoad()

    locationManager.delegate = self


    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in})

    let uuid = UUID(uuidString: "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0")!
    let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 444, minor: 333, identifier: "abcdefac005b")

    if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedAlways) {
        locationManager.requestAlwaysAuthorization();
    }
    locationManager.startRangingBeacons(in: beaconRegion)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}



func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
    print(beacons)
    if(beacons.count > 0){
    if(!sendYet){
    if beacons[0].proximity.rawValue < 2 {
        textLbl.text = "In the room"
        sendHiNoti()
        sendYet = true ;
    }
    }
   else if beacons[0].proximity.rawValue >= 3 {
        textLbl.text = "Outside the room"
        sendByeNoti()
        sendYet = false ;
    }
}
}
}
导入UIKit
导入核心定位
导入用户通知
类ViewController:UIViewController、CLLocationManagerDelegate{
@IBOUTLE弱var字段:UITextField!
@IBL弱var textLbl:UILabel!
房间内的var=假
var name=“”
var sendYet=false;
func sendHiNoti()
{
name=field.text;
let content=UNMutableNotificationContent()
content.title=“Heloo”+名称
content.subtitle=“欢迎来到您的家”
content.body=“欢迎您到家”
content.badge=1
content.sound=UNNotificationSound(名称:UNNotificationSoundName(rawValue:“quiteimpressed.mp3”))
let trigger=UNTimeIntervalNotificationTrigger(时间间隔:2,重复:false)
let request=UNNotificationRequest(标识符:“azanSoon”,内容:content,触发器:trigger)
UnuseNotificationCenter.current()。removeAllPendingNotificationRequests()
UNUserNotificationCenter.current()。在中添加(请求){(错误)
如果let error=error{
打印(“错误:\(错误)”)
}
}
}
func sendByeNoti()
{
name=field.text;
let content=UNMutableNotificationContent()
content.title=“OH”+名称
content.subtitle=“你已经出去了吗??”
content.body=“照顾好自己”
content.badge=1
content.sound=UNNotificationSound(名称:UNNotificationSoundName(rawValue:“quiteimpressed.mp3”))
let trigger=UNTimeIntervalNotificationTrigger(时间间隔:2,重复:false)
let request=UNNotificationRequest(标识符:“azanSoon”,内容:content,触发器:trigger)
UnuseNotificationCenter.current()。removeAllPendingNotificationRequests()
UNUserNotificationCenter.current()。在中添加(请求){(错误)
如果let error=error{
打印(“错误:\(错误)”)
}
}
}
@iAction func getText(){
name=field.text!
让alert=UIAlertController(标题:“您的名字是”,消息:名称,首选样式:UIAlertController.Style.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertAction.style.default,处理程序:nil))
self.present(警报、动画:true、完成:nil)
}
var locationManager=CLLocationManager()
重写func viewDidLoad(){
super.viewDidLoad()
locationManager.delegate=self
UNUserNotificationCenter.current().requestAuthorization(选项:[.alert、.sound、.badge],completionHandler:{didAllow,错误在})
设uuid=uuid(UUIString:“E2C56DB5-DFFB-48D2-B060-D0F5A71096E0”)!
设beaconRegion=CLBeaconRegion(proximityuid:uuid,major:444,minor:333,标识符:“abcdefac005b”)
if(CLLocationManager.authorizationStatus()!=CLAuthorizationStatus.AuthorizationDalWays){
locationManager.requestAlwaysAuthorization();
}
locationManager.startRangingBeacons(位于:信标区域)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
}
func locationManager(manager:CLLocationManager,didRangeBeacons信标:[CLBeacon],区域内:CLBeaconRegion){
打印(信标)
如果(beacons.count>0){
如果(!sendYet){
如果信标[0].approxity.rawValue<2{
textLbl.text=“在房间里”
sendHiNoti()
sendYet=true;
}
}
如果信标[0].approxity.rawValue>=3,则为else{
textLbl.text=“房间外”
sendByeNoti()
sendYet=false;
}
}
}
}

显示的代码使用信标测距
locationManager.startRangingBeacons(在:beaconRegion)
,在前台和后台之间转换后,在后台通常不支持超过10秒

locationManager.requestAlwaysAuthorization()
将仅解锁在后台使用信标监控的功能。信标监控在信标第一次出现(
didnenter(region:region)
)或全部消失(
didExit(region:region)
)时为您提供一次呼叫。 这是唯一在正常情况下在后台工作的BeaconAPI

可以使用两种技术在后台进行超过10秒的信标测距:

  • 在应用程序转换到后台后,您可以通过启动我的博客文章中描述的后台任务,获得180秒的后台范围

  • 你还可以告诉iOS,你是一个位置应用程序,可以解锁无限的背景信标范围。您必须首先实现第1部分中的解决方案。然后,在Info.plist中声明:

    <key>UIBackgroundModes</key>
    <array>
        <string>location</string>
    </array>
    

    非常感谢这对我帮助很大我真的很感激
     // save off name when user fills it in
     UserDefaults.standard.set(name, forKey: "name")
    
     // load back name on app restart
     name = UserDefaults.standard.string(forKey: "name")