SFSpeechRecognizer on MacOS not available despite successful authorization - macos

I am trying to get a clumsy Objective-C proof-of-concept example to run with SFSpeechRecognizer on Catalina transcribing a local audio file.
After some googling I have managed to get the authorization to work by adding an Info.plist with NSSpeechRecognitionUsageDescription and I get the authorization dialog and the correct SFSpeechRecognizerAuthorizationStatus (SFSpeechRecognizerAuthorizationStatusAuthorized).
However, my SFSpeechRecognizer instance still is unavailable. I suspect, I must be making a stupid mistake due to lack of basic Objective-C knowledge.
Any hints greatly appreciated.
Here's my code:
//
// main.m
// SpeechTestCatalina
//
#import <Foundation/Foundation.h>
#import <Speech/Speech.h>
void transcribeTestFile(){
NSLocale *locale =[[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
SFSpeechRecognizer *speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:locale];
NSLog(#"Locale %#, %#", speechRecognizer.locale.languageCode, speechRecognizer.locale.countryCode);
NSLog(#"Available %hhd", speechRecognizer.available);
NSLog(#"Auth status %ld", [SFSpeechRecognizer authorizationStatus]);
NSLog(#"Supports on device %hhd", speechRecognizer.supportsOnDeviceRecognition);
if(speechRecognizer.isAvailable && speechRecognizer.supportsOnDeviceRecognition){
NSString *audioFilePath = #"/Users/doe/speech-detection/speech_sample.wav";
NSURL *url = [[NSURL alloc] initFileURLWithPath:audioFilePath];
NSLog(#"Analyzing %# in language %#", url, locale.languageCode);
SFSpeechURLRecognitionRequest *urlRequest = [[SFSpeechURLRecognitionRequest alloc] initWithURL:url];
urlRequest.requiresOnDeviceRecognition = true;
urlRequest.shouldReportPartialResults = YES; // YES if animate writting
[speechRecognizer recognitionTaskWithRequest: urlRequest resultHandler: ^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error){
NSString *transcriptText = result.bestTranscription.formattedString;
if(!error){
NSLog(#"Transcript: %#", transcriptText);
} else {
NSLog(#"Error: %#", error);
}
}];
} else {
NSLog(#"speechRecognizer is not available on this device");
}
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus authStatus) {
NSLog(#"Status: %ld", (long)authStatus);
switch (authStatus) {
case SFSpeechRecognizerAuthorizationStatusAuthorized:
//User gave access to speech recognition
NSLog(#"Authorized");
transcribeTestFile();
break;
case SFSpeechRecognizerAuthorizationStatusDenied:
//User denied access to speech recognition
NSLog(#"SFSpeechRecognizerAuthorizationStatusDenied");
break;
case SFSpeechRecognizerAuthorizationStatusRestricted:
//Speech recognition restricted on this device
NSLog(#"SFSpeechRecognizerAuthorizationStatusRestricted");
break;
case SFSpeechRecognizerAuthorizationStatusNotDetermined:
//Speech recognition not yet authorized
break;
default:
NSLog(#"Default");
break;
}
}];
NSLog(#"Sleeping");
[NSThread sleepForTimeInterval:20.0f];
}
return 0;
}
The output when I run it is:
2020-01-26 17:48:39.454809+0100 SpeechTestCatalina[3623:82404] Sleeping
2020-01-26 17:48:41.182459+0100 SpeechTestCatalina[3623:82811] Status: 3
2020-01-26 17:48:41.182562+0100 SpeechTestCatalina[3623:82811] Authorized
2020-01-26 17:48:41.186933+0100 SpeechTestCatalina[3623:82811] Locale en, US
2020-01-26 17:48:41.190973+0100 SpeechTestCatalina[3623:82811] Available 0
2020-01-26 17:48:41.191269+0100 SpeechTestCatalina[3623:82811] Auth status 3
2020-01-26 17:48:41.197965+0100 SpeechTestCatalina[3623:82811] Supports on device 0
2020-01-26 17:48:41.198065+0100 SpeechTestCatalina[3623:82811] speechRecognizer is not available on this device
Program ended with exit code: 0

You aren't getting the callback because your binary does not have a runloop. I'll take the response from this different question but with the same answer:
Callbacks in most Apple frameworks are delivered through your application's main run loop. If your command-line tool does not have a run loop, it cannot receive callbacks that are sent this way.
Without a runloop, the only way for the framework to invoke your callback would be to run it on another thread, which could lead to weird behaviour in an application that didn't expect that.
You can manually pump the runloop by inserting this code before the end of main:
NSRunLoop* runloop = [NSRunLoop currentRunLoop];
[runloop runUntilDate:[NSDate distantFuture]];
This will prevent your application from exiting; you'll need to update your logic to know when speech recognition is finished and restructure that with a while loop or something - but I assume the logic inside your "real" application is different than this toy sample.
The message:
AddInstanceForFactory: No factory registered for id F8BB1C28-BAE8-11D6-9C31-00039315CD46 HALC_ShellDriverPlugIn::Open: Can't get a pointer to the Open routine
that appears in your console a meaningless; it's some log statement leaking out of the system frameworks and you can disregard it.
Finally, for clarification on a couple other points:
"Enable Ask Siri" was required to be enabled in System Preferences > Siri for speech recognition to be available
There is a potential issue where the device may report that "on device recognition" is not available the first time you check, despite being supported for the chosen locale

Related

Authorization by AEDeterminePermissionToAutomateTarget waits infinit time

I use this method to check Apple Event (Automation) permission:
bool checkSIPforAppIdentifier(const QString &appId)
{
OSStatus status = noErr;
if (#available(macOS 10.14, *)) {
NSAppleEventDescriptor *targetAppEventDescriptor;
targetAppEventDescriptor = [NSAppleEventDescriptor descriptorWithBundleIdentifier:appId.toNSString()];
status = AEDeterminePermissionToAutomateTarget(targetAppEventDescriptor.aeDesc, typeWildCard, typeWildCard, true);
}
return status == noErr;
}
The problem is that the execution freezes at API: AEDeterminePermissionToAutomateTarget and the user is not prompted for authorization.
usage example:
checkSIPforAppIdentifier("com.microsoft.Word");
I have inserted necessary key in info.plist:
<key>NSAppleEventsUsageDescription</key>
<string>XXX uses this feature to do do Typography actions.</string>
My App is not sandboxed.
Hardened Runtime open Apple Events
enter image description here

Create a RACSignal from rac_GET after pushing a button, cancelling previous requests

After going through the tutorial I am trying my first exercise in Reactive Cocoa going. The goal is to have a button download something from the internet using AFNetworking and these ReactiveCocoa wrappers.
I came up with this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = nil;
RACSignal *buttonSignal = [self.stepperButton rac_signalForControlEvents:UIControlEventTouchUpInside];
RACSignal *getSignal = [buttonSignal map:^id(UIButton *button) {
return [manager rac_GET:#"https://farm3.staticflickr.com/2815/13668440264_e6403b3100_o_d.jpg" parameters:nil];
}];
RACSignal *latestSignal = [getSignal switchToLatest];
[latestSignal subscribeNext:^(id x) {
NSLog(#"x: %#",x);
}];
This seems to do a couple of things that I want:
It downloads the image from flickr as requested
It cancels the previous request when clicking on the button too fast (switchToLatest was the key here)
But it fails on other things:
I can not seem to get valid responses in my subscribeNext. x is always null.
Whenever a request fails the signal goes in error state and pushing the button does not trigger any new GETs anymore.
I guess I am missing lots of things here since I am new to Reactive Cocoa but maybe there are people that are willing to give hints to get me going in the right direction?
Are there other approaches that I fail to see?
This seems to work:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = nil;
RACSignal *buttonSignal = [self.stepperButton rac_signalForControlEvents:UIControlEventTouchUpInside];
RACSignal *getSignal = [buttonSignal map:^id(UIButton *button) {
return [[manager rac_GET:#"https://farm3.staticflickr.com/2815/13668440264_e6403b3100_o_d.jpg" parameters:nil] catch:^RACSignal *(NSError *error) {
NSLog(#"catch %#",error);
return [RACSignal empty];
}];
}];
RACSignal *latestSignal = [getSignal switchToLatest];
[latestSignal subscribeNext:^(NSData *data) {
NSLog(#"dowloaded %d bytes",data.length);
}];
Thanks Stackoverflow similar questions! Powerful stuff.
The catch should be on the rac_GET. Before I had been trying to do things with catch but on the buttonSignal pipe.
And the reason x was always null was because I did not have a serializer set up on the manager.
I thought about deleting this question, but maybe there are people that still have remarks on the solution?

Storing UIManagedDocuments when uibiquity container (iCloud) is not available

I've managed to understand how to incorporate UIManagedDocument into a simple test application and it works as expected! However, now I'm adding support to this basic application so it will work if the user does not want to use iCloud.
So when the URLForUbiquityContainerIdentifier: method returns 'nil', I return the URL of the local documents directory using the suggested method
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
return [NSURL fileURLWithPath:documentsDirectoryPath];
However, when I try saving a UIManagedDocument to the local URL (such as: file://localhost/var/mobile/Applications/some-long-identifier/Documents/d.dox) I get the following error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'
Using this save method:
if (![[NSFileManager defaultManager] fileExistsAtPath:self.managedDocument.fileURL.path]) {
[self.documentDatabase saveToURL:self.managedDocument.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success) {
//
// Add default database stuff here.
//
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self.documentDatabase.managedObjectContext performBlock:^{
[Note newNoteInContext:self.managedDocument.managedObjectContext];
}];
});
} else {
NSLog(#"Error saving %#", self.managedDocument.fileURL.lastPathComponent);
}
}];
}
It turns out my persistent store options contained the keys used for the ubiquitous store. These shouldn't be in the documents persistent store options.

iOS 6 contacts access alert never showed on debug

My app on the appstore is accessing the iPhone contacts, after the users downloaded it on iOS 6 it can't access the iPhone contacts while its working fine on iOS 5
the problem is the new privacy settings apple has put in iOS 6 .. so after searching i found out that i have to do the following in my code to be able to access the user contacts:
//in order to test addressbook availability we have to attempt to create an addressbook instance using ABAddressBookCreateWithOptions
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef,
^(bool granted, CFErrorRef error) {
if (granted)
[self loadContacts];
});
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self loadContact];
} else {
}
#endif //end iOS6+
//ABAddressBookCreateWithOptions not available or succeeded. return YES;
[self loadContacts];
My problem now is while debugging on the device, the alert is not showing, i don't know why ?
I know that the above code should work fine, but only when the app is submitted to the appstore but i want to test that in debug mode before submission ?
Any advice ?
Appreciate your support.
Thanks.
I have managed to get it resolved
Here is the new code after a slight modification:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
__block MyClassType *controller = self;
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef,
^(bool granted, CFErrorRef error) {
if (granted)
[controller loadContacts];
});
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self loadContacts];
} else {
}
#else
[self loadContacts];
#endif
The key to be able to test it is to Reset the Privacy and location settings from Settings>>General>>Reset>>Reset Location & Privacy
It worked fine with me.
Resetting Location & Privacy doesn't work for me.
My authorization status is always kABAuthorizationStatusAuthorized, regardless of whether I clear simulator settings and then reset Location and Privacy.

Client-to-client messaging in cocoa?

erm, now I trying to do a messaging between both client instead of client to server. So if I'm not wrong we can't startup service individually but how to see whether is setting up individually or both connect to same service?
My Code for startup the service:
-(void)startService {
// Start listening socket
NSError *error;
self.listeningSocket = [[[AsyncSocket alloc]initWithDelegate:self] autorelease];
if ( ![self.listeningSocket acceptOnPort:0 error:&error] ) {
NSLog(#"Failed to create listening socket");
return;
}
// Advertise service with bonjour
NSString *serviceName = [NSString stringWithFormat:#"Aho- %#",
[[NSProcessInfo processInfo] hostName]];
connectedService = [[NSNetService alloc] initWithDomain:#"" type:#"_cocoaforsci._tcp."
name:serviceName
port:1234];
connectedService.delegate = self;
[connectedService publish];
}
Any suggestion?
Based on your latest comment, I recommend this article (source is linked near the beginning):
Tutorial: Networking and Bonjour on iPhone
I believe most (if not all) of the article applies just as well to the Mac platform as it does to the iPhone platform.

Resources