iphone location service alert call back allow and don't allow handling - objective-c

I have a button in which on click I am authorizing my app to enable location services. I want to capture the allow button event from the alert. As the user press allow, my button color will change to some other color. I am unable to capture the callback of allow button. However I am able to capture don't allow button event with help of this link
Location service iOS alert call back
Please can anyone tell me how to capture the allow button tap and on success I want to do my code.

Implement this:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
//allowed - do your logic
}
else if (status == kCLAuthorizationStatusDenied) {
//denied
}
}
Swift 4/5:
self.locationManager.requestAlwaysAuthorization()
Then handle in delegate method:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
manager.requestAlwaysAuthorization()
case .restricted, .denied:
// location services unauthorized
case .authorizedAlways, .authorizedWhenInUse:
manager.startUpdatingLocation()
#unknown default:
print("New CLLocationManager.authorizationStatus() not handled!")
}
}
Remember to set locationManager delegate:
self.locationManager.delegate = self;
And implement CLLocationManagerDelegate by your class.

Related

FLEX Button event handling

I have a scenario where i have 5 buttons which call the same method when clicked. These buttons are clicked in various conditions, but now i want to know how we determine that which particular button has been clicked, from the called method.
For example, i have been calling chocolate() method when i click the buttons, eclairs, dailrymilk, cadbury, snickers and kitkat. Now i will click anyof these buttons from the UI and i want to know which one is clicked. this event has to be handled in the chocolate() method only.
Please suggest me how can i implement this. I am using Adobe Flex 3
If you are not using the addEventListeners but are setting the click property in your buttons you could do something like that:
<s:Button id="snickers"
click="{chocolate('snickers')}"
label="snickers"/>
<s:Button id="kitkat"
click="{chocolate('kitkat')}"
label="kitkat"/>
private function chocolate(type:String):void
{
trace("button", type, "was clicked");
if(type == "snickers")
{
// do stuff
}
else if(type == "kitkat")
{
// do something else
}
}
if you are working with event listeners you could determine the buttons from their ids, for example:
<s:Button id="snickers"
label="snickers"/>
<s:Button id="kitkat"
label="kitkat"/>
// add your event listeners somewhere like in onCreationComplete
snickers.addEventListener(MouseEvent.CLICK, chocolate);
kitkat.addEventListener(MouseEvent.CLICK, chocolate);
private function chocolate(e:MouseEvent):void
{
// e.target is the component that has dispatched the event (a button in this case)
var type:String = e.target.id;
trace("button", type, "was clicked");
if(type == "snickers")
{
// do stuff
}
else if(type == "kitkat")
{
// do something else
}
}

How to wait for Location Alert ( which is system alert) to show?

I figured out how to dismiss System alert, but I am not able to wait for it to show , since app doesn't see System Alerts. I tried to debug with app.debugDescription and app.alerts.count but no luck.
You should use addUIInterruptionMonitor as #Oletha wrote.
The tricky part here is that system alert buttons don't use Accessibility Identifiers so you have to search for text to tap them. This text is translated to the language you're running your simulator/device, which can be hard if you want to run the test for several languages beside English.
You could use AutoMate framework to simplify this. Here you have an example how to deal with system alerts using AutoMate:
func locationWhenInUse() {
let token = addUIInterruptionMonitor(withDescription: "Location") { (alert) -> Bool in
guard let locationAlert = LocationWhenInUseAlert(element: alert) else {
XCTFail("Cannot create LocationWhenInUseAlert object")
return false
}
locationAlert.allowElement.tap()
return true
}
// Interruption won't happen without some kind of action.
app.tap()
// Wait for given element to appear
wait(forVisibilityOf: locationPage.requestLabel)
removeUIInterruptionMonitor(token)
}
In the example above the locationAlert.allowElement.tap() is possible because AutoMate can handle any language supported by iOS Simulator.
For more examples on how to deal with system alerts using AutoMate please look into: PermissionsTests.swift
Use addUIInterruptionMonitor:withDescription:handler: to register an interruption monitor. To 'wait' for the system alert to appear, use the handler to set a variable when it has been dealt with, and execute a benign interaction with the app when you want to wait for the alert.
You must continue to interact with the app while you are waiting, as interactions are what trigger interruption monitors.
class MyTests: XCTestCase {
let app = XCUIApplication()
func testLocationAlertAppears() {
var hasDismissedLocationAlert = false
let monitor = addUIInterruptionMonitor(withDescription: "LocationPermissions") { (alert) in
// Check this alert is the location alert
let location = NSPredicate(format: "label CONTAINS 'Location'")
if alert.staticTexts.element(matching: location).exists {
// Dismiss the alert
alert.buttons["Allow"].tap()
hasDismissedLocationAlert = true
return true
}
return false
}
// Wait for location alert to be dismissed
var i = 0
while !hasDismissedLocationAlert && i < 20 {
// Do some benign interaction
app.tap()
i += 1
}
// Clean up
removeUIInterruptionMonitor(monitor)
// Check location alert was dismissed
XCTAssertTrue(hasDismissedLocationAlert)
}
}

swift textView doesn`t show data when back to application while receiving bluetooth data

I am designing a app to receive data from BLE device.
and there is a textView to show what the data the app received.
Everything is okay, but when I press the button on the corner left to second scene and then back to the first scene, the data doesn`t show on the textView anymore. But from debug area I could see the app is receiving data
here`s the part of code on showing data on TextView
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print("did update")
if let error = error {
print("Error discovering services: \(error.localizedDescription)")
return
}
if let stringFromData = String(data: characteristic.value!, encoding: NSUTF8StringEncoding) {
if stringFromData.rangeOfString("tem:") != nil {
print("tem")
print("Received: \(stringFromData)")
textView.text = stringFromData
} else if stringFromData.rangeOfString("gas:") != nil{
print("gas reveived")
print("Received: \(stringFromData)")
textView.text = stringFromData
} else {
print("format wrong")
}
}
}
here`s the shot images of the app
before re-enter application image
after re-enter application image
hope to get the answer, thank you
I found a possible solution to fix this problem.
I add an action to the button,
#IBAction func onSettingButton(sender: UIButton) {
centralManager.cancelPeripheralConnection(discoveredPeripheral!)
}
when I press button on the first scene, it will cancel BLE Connection, and when I back to the first scene, viewDidLoad() will initial bluetooth setting. therefore and the problem is solved.
But I still want to know why this case happen?

How to change PreferredStatusBarStyle programmatically

I'd like to change the color of status bar from white to black by press button, programmatically only in a single-ViewController
This is the code:
- (UIStatusBarStyle)preferredStatusBarStyle {
NSLog(#"PreferredStatusBarStyle");
if(nav_bar.alpha==1)
{
NSLog(#"->UIStatusBarStyleBlackOpaque");
return UIStatusBarStyleBlackOpaque;
}
else
{
NSLog(#"->UIStatusBarStyleLightContent");
return UIStatusBarStyleLightContent;
}}
then When I press a button action is:
[self setNeedsStatusBarAppearanceUpdate];
But this doesn't work!
When I press the button log write the correct status according to navbar.alpha, but statusbar text color remain UIStatusBarStyleBlackOpaque like when view appear.
setStatusBarStyle:animated: has been deprecated. In iOS9 you can achieve the same thing using preferredStatusBarStyle and setNeedsStatusBarAppearanceUpdate.
In your View Controller:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
if (condition) {
return .LightContent
}
return .Default
}
And then when your condition changes:
self.setNeedsStatusBarAppearanceUpdate()
On Swift 4:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Hope it helps anybody else to find this post.
what you need to do, is to call the -setStatusBarStyle:animated: method thru the shared application, like this
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
you can use it without the animated parameter as well. keep in mind that UIStatusBarStyleBlackOpaque is deprecated in iOS 7, documentation says you wanna use UIStatusBarStyleLightContent instead
edit:
sorry, if u wanna use preferredStatusBarStyle, take a look at this preferredStatusBarStyle isn't called

How to call different button click event method in if condition?

I am beginner of iPhone. I have create five button. button click event perform go to other class in that how to call button click event method..?
it is in ViewController class
(IBAction)onclickbutton1
{
}
(IBAction)onclickbutton2
{
}
and this onclickbutton1 and onclickbutton2 method how to call in other class in if condition
so, that condition of onclickbutton1 is true perform onclickbutton event
Just add a test within the event handling methods:
- (IBAction)onclickbutton1
{
if (conditionIsTrue)
[OtherClass doThing1];
else
[OtherClass doThing2];
}
- (IBAction)onclickbutton2
{
if (conditionIsTrue)
[OtherClass doThing1];
else
[OtherClass doThing2];
}