我正在与iBeacons合作,并希望在用户靠近iBeacon时显示警报。为此我做了:
for closest in beacons as! [CLBeacon] {
if closest.major == 4660 && closest.minor == 4648 {
var customIcon:UIImage! = UIImage(named: "location2.png")
var customColor:UIColor! = UIColor(red: 63/255.0, green: 172/255.0, blue: 236/255.0, alpha: 1)
var alert = JSSAlertView().show(
self,
title: "You're near the beacon!",
buttonText: "Dismiss",
color: customColor,
iconImage: customIcon
)
alert.setTextTheme(.Light)
}
}
它有效,但问题是它多次显示,一个接一个,用户无法解除此警报。这是因为我的应用每秒都会更新ibeacon的位置以寻找新的ibeacons。
我可以一次显示此提醒吗?
我做下一个:
var flag = 0
if flag == 0 {
var alert = JSSAlertView().show(
self,
title: "You're near the beacon!",
buttonText: "Dismiss",
color: customColor,
iconImage: customIcon
)
alert.setTextTheme(.Light)
}
但它也不起作用
答案 0 :(得分:0)
对不起,似乎我忽略了每当范围改变时调用locationManager(_:didRangeBeacons:inRegion:)
的事实。因此,如果您希望在向类添加属性后发生警报
var isShowing:Bool = false
然后调整代码以考虑布尔值
for closest in beacons as! [CLBeacon] {
if (closest.major == 4660 && closest.minor == 4648) && isShowing == false {
var customIcon:UIImage! = UIImage(named: "location2.png")
var customColor:UIColor! = UIColor(red: 63/255.0, green: 172/255.0, blue: 236/255.0, alpha: 1)
var alert = JSSAlertView().show(
self,
title: "You're near the beacon!",
buttonText: "Dismiss",
color: customColor,
iconImage: customIcon
)
alert.setTextTheme(.Light)
isShowing = true
break
}
}
答案 1 :(得分:0)
这是对@ cream-corn代码的编辑
var shouldShow = true
for closest in beacons as! [CLBeacon] {
if closest.major == 4660 && closest.minor == 4648 {
break
}
}
if (shouldShow == true) {
var customIcon:UIImage! = UIImage(named: "location2.png")
var customColor:UIColor! = UIColor(red: 63/255.0, green: 172/255.0, blue: 236/255.0, alpha: 1)
var alert = JSSAlertView().show(
self,
title: "You're near the beacon!",
buttonText: "Dismiss",
color: customColor,
iconImage: customIcon
)
alert.setTextTheme(.Light)
shouldShow = false
}
答案 2 :(得分:0)
您使用flag
进入正确的轨道。但是如果你将var flag = 0
放在那个方法中,那么flag总是为0
你必须在类中声明flag
,而不是像这样的函数:
class myClass {
var flag = 0
func myFunction() {
/*more code here...*/
if flag == 0 {
var alert = JSSAlertView().show(
self,
title: "You're near the beacon!",
buttonText: "Dismiss",
color: customColor,
iconImage: customIcon
)
alert.setTextTheme(.Light)
}
}
}