How to detect which button is clicked on built-in UIAlertViewController alerts like Location permission alert in iOS10? - delegates

Ok as I know this function:
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
}
is depreciated now, then how can I detect which button is pressed by the user on built-in alerts of iOS SDK, like the alert that appear to ask permissions, in my case I want to detect Location Permission Alert's clicked button index?
see the screenshot below.
I don't want solution like these delegates:
didUpdateLocations
didFailWithError
I just want replacement of
clickedButtonAt
to detect (iOS SDK generated alerts) button actions.

There is no simple replacement for clickedButtAt. It doesn't work that way. You don't do it like you do with UIAlertController. You have to use all of the proper CLLocationManager methods and delegates.
First, create an instance of CLLocationManager and set its delegate. Then check the current authorization status using the authorizationStatus() method. If the result is notDetermined then you call the requestWhenInUseAuthorization method.
You find out what choice the user made by implementing the associated CLLocationManagerDelegate method locationManager(_:didChangeAuthorization:). This will tell you whether the user chose to allow or deny access.

Related

Unity - Detect if Game Center is available

My Game is using Unity API to call Game Center and I want to use it like "Archero".
i want to make it possible:
if Game Center is available: login and show banner...etc.
if not: just give me a callback, DO NOT show the Game Center Authenticate Panel.
but no matter how I call the authentication function, it ALWAYS pop the Authenticate panel.
is there any solution to detect if the Game Center is available?
By the way, someones said that when user cancel Authentication 3 times, Game Center will block user.
If i can do that by code will be pretty useful, i can use it to deal with my problems
Here is my Authenticate Code:
public void OnClick_Authenticate()
{
try
{
Social.localUser.Authenticate(ProcessAuthentication);
}
catch
{
ActiveCallback();
}
}
I don't think that is possible/intended. You're talking about the iOS "popup" right?
Like derHugo says, check for internet connectivity is your best option
From what I can tell you have to create your custom plugin to achieve what Archero is doing.
At the end of the authentication process, iOS returns the viewController with the "login" form if the user is not authenticated.
On iOS the developer can decide to present this viewController or not. The social plugin doesn't have this option, they always present the login viewController if returned by the system... if you implement your plugin you could decide not to show it... ending up with what Archero is doing.

keyDown(with:) Not Called on Custom ViewController or View, Only WindowController

I have moved most of the core functionality of my non-document based macOS app to a custom, embedded framework.
The app code has a standard main storyboard with an initial window, and the window has a "window content" relationship/segue into a storyboard reference pointing to a storyboard inside the embedded framework. Therein lies a custom NSViewController subclass and a custom NSView subclass.
I want to group all the input event handling code inside the framework, which means implementing mouseDown(with:) on the custom NSView subclass, and --lo and behold-- it gets called when I click inside the app window. So far, so good.
Next, I implemented keyDown(with:) to similarly handle keyboard input. However, at runtime, it does not get called and instead, I hear the annoying beep (NSBeep).
I tried implementing keyDown(with:) on the view controller instead, but it's all the same.
Finally, I tried implementing the key handler on my NSWindowController subclass instead, and that does work.
So I could get around this by forwarding the event like so:
class WindowController: NSWindowController {
override func keyDown(with event: NSEvent) {
contentViewController?.view.keyDown(with: event)
}
}
, but it is very inelegant. I would prefer to not pollute the app code with input logic.
This doesn't seem to have anything to do with embedding frameworks, however. I put together a minimal project from the 'Cocoa App' template and confirmed that indeed keyDown(with:) only gets called if implemented on the window controller code, but not on the view or view controller side.
How can I get keyDown(with:) to be called on the view or view controller (not the window or window controller) in a storyboard-based app? (so I can move it from the main app to my embedded framework).
Edit: The question has been marked as duplicate. I tried the solutions pointed in answers to the other question (namely, override acceptsFirstResponder to return true). This solves the problem in my minimal demo project, but when I tried it on my full app, it still does not work (I did see that question and did try to override acceptsFirstResponder in my app before posting this question).
I will now try to modify my minimal poeject to see if I can reporduce the issue in the main app.
Edit 2: I have refactored the minimal project to:
Send the view controller to a separate storyboard,
Send the view controller's storyboard and represented classes (custom view, custom view controller) to a separate, embedded framework.
Now the basic setup mirrors that of my app in all that seems to matter, but still can not reproduce the issue in the minimal project. I will investiate further...
Edit 3: I haven't been able to reproduce the issue on the minimal project.
On my app's custom view, I implemented:
public override var acceptsFirstResponder: Bool {
return true
}
public override func performKeyEquivalent(with event: NSEvent) -> Bool {
let retVal = super.performKeyEquivalent(with: event)
return retVal
}
On startup, acceptsFirstResponder is called twice.
When hitting any key, performKeyEquivalent(with:) is called twice, too. Inspectig the intermediate variable retVal above reveals that the super class's implementation always returns false. After returning from this method, NSBeep() is called and keyDown(with:) isn't.
If instead of super.performKeyEquivalent(with:) I force-return true, I can avert the call to NSBeep() (but keyDown(with:) is still not called...)
Edit 4 (Final):
Out of desperation, I cleared the "Custom Class" field of the window controller's Identity Inspector in my app's main storyboard (to the default NSWindowController).
Suddenly, my custom view's keyDown(with:) starts getting called.
I reinstated the custom class to confirm.
It still works.
I clean the build folder and try again.
It still works.
Now I can no longer reproduce the issue even on my main app. I really don't know what to say...

How to intercept Cmd+Q

I am working on Win/MacOS game app.
When user closes the app, I want to show a confirmation popup sorta "Are you sure you want to leave?".
On Windows, I listen to WM_CLOSE message to intercept Alt-F4. Then, if user selects "Yes, I want to leave" -- I call PostQuitMessage(0); to exit the app.
How do I intercept Cmd+Q on Mac OS? And, if user selects "Yes, I want to leave" -- how do I close the app (i.e. how to perform the same action as Cmd+Q does by default)?
MacOS part is in written in plain Objective-C and I am looking for an answer in Objective-C.
Implement the delegate method applicationShouldTerminate of NSApplication and show a custom modal alert. Depending on the answer return NSTerminateNow, NSTerminateCancel or NSTerminateLater.
In case of NSTerminateLater you can later call [NSApp replyToApplicationShouldTerminate:YES]; to finally quit the app.

Android 6 runtime permission for accessing gallery

I got questions about Android 6 (Marshmallow) runtime permission. If user wants to pick a photo from gallery, should we ask for READ_EXTERNAL_STORAGE permission?
Seems like I could access the gallery even though I turn off the Storage permission.
You need to ask for READ_EXTERNAL_STORAGE. You will be able to access the gallery without it, but if you want to do anything with the media you get from the gallery you will need the READ permission.
A quick test on what happens in onActivityResult after an image has been picked form the gallery:
Permission Denial: reading com.android.providers.media.MediaProvider
uri content://media/external/images/media from pid=8405, uid=10177
requires android.permission.READ_EXTERNAL_STORAGE, or
grantUriPermission()
For custom permission,you may use runtime permission if you are using Android 6.0 or above.This code may help you .
If your app doesn't already have the permission it needs, the app must
call one of the requestPermissions() methods to request the
appropriate permissions. Your app passes the permissions it wants, and
also an integer request code that you specify to identify this
permission request. This method functions asynchronously: it returns
right away, and after the user responds to the dialog box, the system
calls the app's callback method with the results, passing the same
request code that the app passed to requestPermissions().
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
To Know more about runtime permission
https://developer.android.com/training/permissions/requesting.html

Opening a url on launch

What method must I implement in my cocoa application’s delegate so that on launch, it’ll open a url? (http/https, in this case) I’ve already implemented the url schemes, I just need to know how I can get my application to open on a url notification.
Update: I’m sorry, I wasn’t very clear. My application IS a browser that support https/http urls, but can only open them when it’s already running. What can I do to implement support for open urls in my app on launch?
When an application finishes launching on OS X, NSApp (the global NSApplication instance for the program) sends its delegate the applicationDidFinishLaunching: message (via the notification system). You can implement that method in your delegate to handle the notification and open a browser window in response, using NSWorkspace. Something like the following would work:
// Your NSApp delegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:#"http://www.example.com/"]];
}
It's not a delegate method. You need to implement an Apple Event handler for the getURL event.
As luck would have it, this is exactly the case Apple uses to demonstrate implementing an Apple Event handler.
I already had implemented the getURL event, so that alone isn’t enough to get the application to open a url on launch. The trick is that the AppleEvent must be installed in applicationWillFinishLaunching: not applicationDidFinishLaunching:. Otherwise, the event isn’t sent at all because the app hasn’t registered it in time.
To implement a protocol handler that you can select (in Safari preferences, for example) as the "default browser" and which will launch in response to HTTP / HTTPS, you need to do a few things.
Add .scriptSuite and .scriptTerminology files to your project resources. These will tell Mac OS X that you'll be handling the GetURL command.
Add a CFBundleURLTypes key to your Info.plist file listing the "URL Schemes" that your app will handle.
Also in Info.plist, add the NSAppleScriptEnabled key with the value YES.
Add a new class to your application as a subclass of NSScriptCommand and implement the -(id)performDefaultImplementation selector. From within this function you will find the clicked URL in [self directParameter]. Pass this on to your app's URL handler!
For the full details check out the article:
http://www.xmldatabases.org/WK/blog/1154_Handling_URL_schemes_in_Cocoa.item

Resources