AVFoundation Speech Synthesis on iOS 8 and XCode 6 - objective-c

I am having problems using AVSpeechSynthesis on iOS 8.0.2 and XCode 5. I tried it on the simulator and got a "Speech initialization error: 2147483665". I then tried it on my iPhone 5 and got no error but no speech.
In my .h file I import
import
and have #property (strong, nonatomic) AVSpeechSynthesizer *synthesizer;
In my .m file I synthesis the synthesizer
I have AVFoundation referenced in my frameworks
On my viewDidLoad I do the following
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
self.synthesizer.delegate = self;
Later on in my code to control what is said I do the following
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:dialog];
utterance.rate = AVSpeechUtteranceDefaultSpeechRate;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:#"en-au"];
[self.synthesizer speakUtterance:utterance];
I have CCViewController : UIViewController in my view controller.
Does anyone know what is wrong? Does AVFoundation work in iOS 8?
I saw a similar post on stack overflow which said to initialize my speaking #" " but that did not work.
Any help would be appreciated.
Thanks,
Greg

What you have should work with a small tweak, I think (see below), but only on a device and not the simulator. I replicated the same error message when I try to run your code or code I have that uses AVSpeachUtterance in the simulator. Not sure if this is a bug or just a limitation of the simulator. To get things working, try commenting out the line:
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:#"en-au"];
Does it work then? Without this line of code things working as they should for me on an iPhone 5 (8.0.2), however if I use any of the codes language codes shown here (What are the BCP-47 voice codes available for iOS 7 AVSpeechSynthesisVoice?), no text is spoken. The only way I can get things to work when I explicitly set the language is if I use #"en-GB", which is also the language the phone is configured for in settings. I can use another language code if I change the language of my phone in settings, say to US english and #"en-US".
As an FYI, the bug that required an initial bit of dummy text, i.e. #" " to get things working seems to be fixed in 8.0.2

Related

Mac app crashes when built with Xcode 9.4.1, runs fine built with Xcode 8.3.3

I am trying to migrate to using Xcode 10 on Mojave, coming from using Xcode 8.3.3 on High Sierra. I have a sizeable app containing a fair bit of legacy (non-ARC) code. The app is currently live in the Mac App Store, built with 8.3.3 on High Sierra.
I encountered problems with Xcode 10 and Mojave which took me down a rabbit hole, so I decided to back up and limit the migration to one step at a time. I'm currently attempting (unsuccessfully) to build and run using Xcode 9.4.1 on High Sierra.
Using exactly the same code base (new repo checkout) my app builds and runs fine using Xcode 8.3.3. It builds fine using 9.4.1, but crashes on running.
The problem appears to be memory/reference-counting related. I always get a crash when the app tries to show a window, and the trace is not very helpful. Runtime debug output consistently indicates that I'm sending a message to a deallocated object. Here's a typical stack trace:
stack trace
Whenever the app tries to show a window, I get messages such as (the old favourite) "message sent to deallocated instance". I don't always recognise the receiver as being one of my properties, so it looks like this is just a symptom, not a cause. Here's a typical message:
*** -[NSCalibratedRGBColor release]: message sent to deallocated instance 0x60000605ce60
I've tried in vain to get anything back from Xcode's diagnostics, such as zombies and malloc scribble. Radio silence. Also the static analyser reports no problems.
All I can think is that manual reference counting is somehow being treated differently by Xcode 9. I've checked through all the build flags to see if Xcode 9 changed anything. No changes. I've checked the release notes and can find no mention of changes to reference counting.
I can push the problem around by commenting out the display of various panels and windows, but the crash always takes the same form.
Has anyone else come across similar issues and found a solution? Am I missing something with Xcode 9? Any help greatly appreciated.
Thanks #sdarlington and Willeke for very helpful answers. I finally found and fixed the problem. I did have to use the caveman approach and gradually remove stuff from a crashing window (as you rightly identified Stephen). Turns out it was a legacy retain/release issue that none of Xcode's diagnostics was catching.
I inherited an old graphics framework that doesn't use ARC. It declares ivars like this:
#interface ColourFiller : NSObject
{
NSColor* m_PrimaryColour;
NSColor* m_SecondaryColour;
}
...
#end
then sets them like this in its init method:
m_PrimaryColour = [NSColor colorWithCalibratedRed:1 green:1 blue:1 alpha:1];
m_SecondaryColour = [NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:1];
Note the lack of manual retain.
So I added one, and all is well:
m_PrimaryColour = [[NSColor colorWithCalibratedRed:1 green:1 blue:1 alpha:1] retain];
m_SecondaryColour = [[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:1] retain];
I have no idea why Xcode wasn't flagging this, or indeed why all previous builds/xcode versions were running fine. This code has happily existed without symptoms for years. Some library changes must have been made by Apple in Xcode 9 that finally exposed it.
Thanks again for your help - I can now progress to Xcode 10 and Mojave.

Objective C writing text onscreen (and overview of libraries/frameworks)

I am learning objective C and could use some pointers (pun intended). I have a background in Java. I just finished the book 'Programming with Objective C'. I was trying to use drawWithRect to draw some text onscreen but cannot get this piece of sample code to work.
#import <Cocoa/Cocoa.h>
#import <AppKit/NSStringDrawing.h>
//#import <UIKit/UIKit.h> //this is invalid
//#import <UIKit/UIColor.h> //this is invalid
#import "NSString+Draw.h"
NSDictionary *attributes = #{
NSForegroundColorAttributeName:[UIColor blueColor]
};
[#"Hello" drawWithRect:CGRectMake(20, 20, 50, 50)
options:0 attributes:attributes context:nil];
It doesn't seem to be in the Frameworks folders in my XCode project. I had looked on apple developer site but was swamped by unrelated information.
How do I get this UIKit? Is this an addon? btw why are some imports using quotes and others angle brackets? are they interchangeable?
I don't have a good big picture of the frameworks. What are the key frameworks?
Is there a web based API documentation like Javadocs?
Still at it.
Is there an equivalent of UIKit/UIColor etc for OSX?
- I found it, AppKit/NSColor
Ok After a bit of exploration I got this but it still doesn't write onscreen.
#import <Cocoa/Cocoa.h>
#import <AppKit/NSStringDrawing.h>
#import <AppKit/NSColor.h>
#import "NSString+Draw.h"
#implementation NSString (Draw)
- (void) drawString:(NSString *) myString{
NSDictionary *attributes = #{
NSForegroundColorAttributeName:[NSColor blueColor]
};
[#"Hello" drawInRect:CGRectMake(20, 20, 50, 50) withAttributes:attributes];
}
It looks like you've created a project targeting OSX. UIKit is a framework that is included with iOS only. Create an iOS project to use that framework.
UIKit is the big framework for iOS, and includes things like UIColor (or anything else with a UI prefix).
Docs are online, and can also be found in Xcode in the 'Help' tab
To draw anything on the screen, you need something called a Graphics Context. The Graphics Context gives you an area to draw in and manipulate. Apple does a good job of explaining it in their documentation here. Further research through the Apple documentation turns up a specific section for drawing text (NSString in particular). An excerpt below from the NSString Drawing documentation:
The Application Kit adds three methods to the NSString class to support drawing string objects directly in an NSView instance: drawAtPoint:withAttributes:, drawInRect:withAttributes:, and sizeWithAttributes:.
There are three things you may want to check in order to ensure everything is / will be drawn properly:
You must have an active NSView to draw on. Check that there is an active NSView to draw on.
You may need to add more parameters to your attributes dictionary. I don't see any required keys, but it might be a good idea to add a font parameter and a background color parameter.
Does the text actually fit in the specified bounds? Maybe specify a larger CGRect (at least for testing)?
If you're absolutely frustrated with drawing NSString using AppKit's methods, try the Core Text framework:
Core Text is an advanced, low-level technology for laying out text and handling fonts. The Core Text API, introduced in Mac OS X v10.5 and iOS 3.2, is accessible from all OS X and iOS environments.
Information on Frameworks and general SDK questions:
When you download Xcode, you're also downloading the Development SDK and related documentation too. The Development SDK is mainly comprised of Frameworks (from Apple). A Framework is a huge compiled code library with visible-header files. So, you can't see the implementations, but you can see the headers.
When importing a framework (doesn't matter if it's from Apple or not), use brackets (or a module if you have it enabled in the build settings). When importing files (not frameworks) then just use quotes.
There are a LOT of frameworks for both iOS and OS X. I can't list them all here, but I can tell you the key ones. Here's a link to the iOS developer documentation, including the frameworks. The big four on iOS are Foundation, UIKit, CoreGraphics, and (sometimes) QuartzCore. When you click on your project in the Xcode file navigator, you should see the project settings and a list of frameworks included. Click the plus button to see all Apple frameworks you can add. Most of them are related to system services (ex. GameCenter framework is GameKit, and Passbook framework is PassKit).
Yes! The Apple Developer Documentation is available both online and offline (installed with Xcode). Online documentation can usually be found through googling, but here's a link. It's also in the Xcode Help menu / documentation menu. You can also option-click on any piece of code you want to learn more about.
The equivalent to UIKit (an iOS only framework) for OS X is AppKit. Most of the classes are the same names, just with an NSA prefix instead of a UI prefix. For example, on OS X you'd need NSColor instead of UIColor.
Apart fro needed to use UIKit and an iOS project, it's also beer to just import the top level header, rather than the individual ones.
So just
#import <UIKit/UIKit.h>
Or, if on Xcode 5
#import UIKit;
To do things visually you need a Graphics Context to draw in.
Objective-C does not provide one.
An operating system does through a window server.
AppKit on OS X provides this through NSWindow.
UIKit on iOS provides this with a single implicit window.

UIImagePickerController on iOS 7: Square mode?

I'm working with iOS 7 now, trying to get the UIImagePickerController to let the user select a square mode since it's a feature in iOS 7 now. Is there something I need to enable to allow this swipe? Relevant code posted below, though it's pretty standard.
UIImagePickerController *cameraView = [[UIImagePickerController alloc] init];
cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraView.delegate = (id)self;
[self presentViewController:cameraView animated:YES completion:nil];
I've been scouring the messy iOS 7 documentation that's available, but I can't even tell if they changed the developer library at all - I don't see ANYTHING new in it.
set allowEditing to YES.
from the result dict use the key UIImagePickerControllerEditedImage
then you will have the squared image.
i find no way to let the user select, which format he want :(
I looked at the iOS 7 header for UIImagePickerController in Xcode, and I do not see new API for 7.0 or a square photo mode. The square photo mode appears to be a feature of the app, not of UIImagePickerController.

Parse Starter Project for iPad

I know it's very vague and is asking a lot but does anyone know how to convert the standard iOS starter project from iPhone to iPad (both is best)? Or does anyone know where I can download one. I am a new iOS developer and am trying to start learning with Parse.
I am referring to this project https://www.parse.com/downloads/ios/parse-starter-project/latest
P.S. Just because this question isn't perfect doesn't mean you have to go and down vote and flag it for removal I don't have a lot of points already no need to lose even more :)
Not being able to see this sample project, it's hard to say for certain what it will take.
At bare minimum go into your project summary, and select "Universal" for the device support.
Above and beyond that, it just depends on what the app is and how it's structured. For NIBs, you will want a NIB for iPhone and one for iPad. I find it easy to abstract this away so that I can simplify my view loading:
MyController *myController = [[MyController alloc] initWithView:#"MyControllerView" bundle:nil];
Then in a category, I'd define initWithView similar to:
#implementation UIViewController (Universal)
-(id) initWithView:(NSString *)view bundle:(NSBundle *)nibBundle{
bool isIpad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
NSString *nibName = [NSString stringWithFormat:#"%#_%#", view, (isIpad ? #"iPad" : #"iPhone")];
return [self initWithNibName:nibName bundle:nibBundle];
}
#end
But, that's just one aspect of supporting both devices. In reality the subject is rather specific to the app you're working on. Things like OS support (e.g., am I only targeting iOS 6 or higher) play a factor in things.
I have solved it now, if anyone needs the files email me turboecreations#iCloud.com I would upload them but I dont want my MediaFire and other accounts to be removed if I run into copyright issues.

Preprocessor Directive in xib?

Despite searching all over the place, I can't find the answer to my question. So let's see how good y'all are. :)
I'm working on an app that uses an NSPopover which is only available in 10.7 Lion but I want the app to compile for 10.5 and higher. I'm using a preprocessor directive to wrap the related popover code which seems to do the trick... However, the last piece I'm still getting errors on is the .zib in Interface Builder. How do I go about cleaning up the errors shown in the Issues Navigator stating "Class Unavailable: NSPopover on Mac OS X versions prior to 10.7"?
#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
#property (assign) IBOutlet NSPopover *popover;
}
#endif
#endif
The above works in xxx.h and xxx.m's, but how do I get around the .xib errors?
Despite the error (Red), it builds successfully. However am I wrong to expect the 10.7 features (popover) to work in 10.7 because they don't... What am I missing here?
You shouldn't use preprocessors for this but check for availability at runtime using NSClassFromString(). The preprocessor runs at compile time, thus it won't detect what system the app is being run on.
Create three nibs, one for each of 10.5, 10.6 and 10.7 and load the one you need (or do it in code), but pick which one at run time, not compile time, e.g.
MyVC *vc = nil;
if (NSClassFromString(#"NSPopover"))
{
vc = [NSViewController initWithNibName:#"MyVC-Lion" bundle:nil];
}
else if (/* check for 10.6+ only features */)
{
vc = [NSViewController initWithNibName:#"MyVC-SL" bundle:nil];
}
else
{
vc = [NSViewController initWithNibName:#"MyVC" bundle:nil];
}
// ...
Not a real answer to your question, apologies, but 2 possible workarounds: isn't it possible to create 2 versions of your xib, and depending on the target, compile on or the other? This would be a bit more work to maintain, but if your UI is pretty stable, this should be the easiest way.
Or you could add your "10.7 specific" UI component(s) programmatically instead of using the IB. If you just have one or a few popovers, it shouldn't be to difficult to do, and the proprocessor guards would work fine.