How do I turn off the Accessibility Inspector in the iOS 9 simulator? - ios-simulator

The accessibility inspector is turned on by my KIF tests (apparently it's necessary for KIF to work.) Problem is, its window occludes controls some subsequent UI tests need to tap on and those tests fail.
How can I turn the Accessibility Inspector off when my KIF tests are done with it so my UI Tests can run?
(Turning it off "manually" from the simulator's Settings app is not a solution—I'm looking for something I can call from code, set in the target or...?)

It is not on by default. You must turn it on manually.

I saw the following on Stew Gleadow's blog.
You just need to change the line:
CFPreferencesSetValue(CFSTR("ApplicationAccessibilityEnabled"), kCFBooleanFalse, accessibilityDomain, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
change kCFBooleanTrue to kCFBooleanFalse.
+ (void)_enableAccessibilityInSimulator {
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init];
NSString *appSupportLocation = #"/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport";
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *simulatorRoot = [environment objectForKey:#"IPHONE_SIMULATOR_ROOT"];
if (simulatorRoot) {
appSupportLocation = [simulatorRoot stringByAppendingString:appSupportLocation];
}
void *appSupportLibrary = dlopen([appSupportLocation fileSystemRepresentation], RTLD_LAZY);
CFStringRef (*copySharedResourcesPreferencesDomainForDomain)(CFStringRef domain) = dlsym(appSupportLibrary, "CPCopySharedResourcesPreferencesDomainForDomain");
if (copySharedResourcesPreferencesDomainForDomain) {
CFStringRef accessibilityDomain = copySharedResourcesPreferencesDomainForDomain(CFSTR("com.apple.Accessibility"));
if (accessibilityDomain) {
CFPreferencesSetValue(CFSTR("ApplicationAccessibilityEnabled"), kCFBooleanFalse, accessibilityDomain, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
CFRelease(accessibilityDomain);
}
}
[autoreleasePool drain];
}

Related

Setting eraser type to bitmap for PencilKit (iOS)

Using PencilKit for iOS, how do I set the eraser tool to .bitmap for PKToolPicker?
I can't find any setting for PKToolPicker. Trying to use PKCanvasView to observe and set the tool's eraserType as .bitmap also does not work.
override func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
var tool = toolPicker.selectedTool as? PKEraserTool
if tool != nil {
tool?.eraserType = .bitmap
}
}
The PKEraser is a struct, so when you change its eraserType, you're actually modifying a copy of the tool that's being used in the canvas.
What you need to do is simply set the PKCanvasView tool property and it will work.
override func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
var tool = toolPicker.selectedTool as? PKEraserTool
if tool != nil {
tool?.eraserType = .bitmap
}
// this line below will do the trick
canvasView.tool = tool
}
Let me know if it worked! 😊
Applies to iOS 13 and iOS 14
To set the toolpicker's selected tool as a bitmap eraser (where toolPicker is the PKToolPicker):
toolPicker?.selectedTool = PKEraserTool(.bitmap)
To set the canvas view's tool to a bitmap eraser (where canvasView is the PKCanvasView):
canvasView.tool = PKEraserTool(.bitmap)
This code, based on your example, will keep the toolpicker's erase tool as bitmap(pixel eraser) even if vector(object eraser) was chosen. (tested on iOS 14)
func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
if toolPicker.selectedTool is PKEraserTool {
toolPicker.selectedTool = PKEraserTool(.bitmap)
}
}

NSAttributedString drawRect doesn't draw images on-screen on Mojave

I have a working app that draws NSAttributedStrings into a custom view. The NSAttributedStrings can included embedded images. This works on versions of macOS prior to Mojave. The app can display the strings on screen, print them, and save them to image files.
This is apparently broken under Mojave. Weirdly, printing and saving to image files still works; but on-screen, the strings display only the text and not the embedded images. Proper space is left for the images, but that space is blank.
I've tested by building a small app that shows a window with an NSTextField (a label) and a custom view. It makes a single NSAttributedString with an embedded image. It applies that string to the attributedStringValue of the label, and also calls drawInRect: on the same string in the drawRect: method of the custom view. In the label, the string is displayed correctly, image and all. But in the custom view, only the text appears, and the space where the image should be is blank.
Anybody got a clue why this is happening on Mojave but not on earlier versions of macOS?
Here is the code that makes the string (and caches it, for re-use):
static NSMutableAttributedString* sgAttrString = nil;
/*
* Creates an attributed string the first time it's called,
* then returns that same string each time it's called.
*/
+ (NSAttributedString*)getAttributedString
{
if (sgAttrString == nil)
{
NSFont* font = [NSFont fontWithName:#"Helvetica" size:24.0];
NSDictionary *attrs = #{
NSFontAttributeName: font
};
sgAttrString = [[NSMutableAttributedString alloc] initWithString:#"Daisy: " attributes:attrs];
NSImage* daisy = [NSImage imageNamed:#"daisy.png"];
[daisy setSize:NSMakeSize(24,24)];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
// I'm aware that attachment.image is available only on macOS 10.11 and later.
// It's not an issue in my real project.
attachment.image = daisy;
NSMutableAttributedString* imageStr = [[NSMutableAttributedString alloc] init];
[imageStr setAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[sgAttrString appendAttributedString:imageStr];
[sgAttrString appendAttributedString: [[NSAttributedString alloc] initWithString:#" !!" attributes:attrs]];
}
return sgAttrString;
}
Here is the code that applies the string to the NSTextField:
NSAttributedString* str = [Utilities getAttributedString];
self.label.attributedStringValue = str;
And here is the code that draws the string in a custom NSView:
NSAttributedString* str = [Utilities getAttributedString];
[str drawInRect:NSMakeRect(50,50, 300, 40)];
Again, this behavior seems to occur only in Mojave! Thanks in advance for any help.

ON/OFF button keeps toggling through states on every touch

I don´t know what I´m doing wrong, maybe I'm sitting here way too long on my project. However, I set up a settings menu in my menuScene and I want to create a simple on-off button with a texture with this code:
if ([settingsMenuNode.name isEqualToString:#"checkmark1"]) {
if (self.checkmarkBanner == NO) {
[self.menuCheckmark1 setTexture:[SKTexture textureWithImageNamed:#"menuCheckmark"]];
self.checkmarkBanner = YES;
NSLog(#"YES");
}
if (self.checkmarkBanner == YES) {
[self.menuCheckmark1 setTexture:[SKTexture textureWithImageNamed:#"menuCheckmarkEmpty"]];
self.checkmarkBanner = NO;
NSLog(#"NO");
}
}
every time I touch on the node, I get both logs for ON & OFF at the same time.
can anyone please help me out?
checkmarkBanner is my BOOL just to set whether a banner notification during the game appears or not.
ok, thank you for your quick answers, so i´ve updated my code to this (i added some NSUserDefaults):
//CheckMarkBanner
if ([settingsMenuNode.name isEqualToString:#"checkmarkBanner"]) {
if (self.checkmarkBanner == YES) {
[self.menuCheckmark1 setTexture:[SKTexture textureWithImageNamed:#"menuCheckmarkEmpty"]];
NSString *offString = #"NO";
[[NSUserDefaults standardUserDefaults]setObject:offString forKey:#"checkmark1Banner"];
self.checkmarkBanner = [[[NSUserDefaults standardUserDefaults]stringForKey:#"checkmark1Banner"]boolValue];
NSLog(#"Banner NO");
}
else {
[self.menuCheckmark1 setTexture:[SKTexture textureWithImageNamed:#"menuCheckmark"]];
NSString *onString = #"YES";
[[NSUserDefaults standardUserDefaults]setObject:onString forKey:#"checkmark1Banner"];
self.checkmarkBanner = [[[NSUserDefaults standardUserDefaults]stringForKey:#"checkmark1Banner"]boolValue];
NSLog(#"Banner YES");
}
}

View-based NSOutlineView without NIB?

NSOutlineView is a subclass of NSTableView. And currently, NSTableView supports two implementations.
Cell-based.
View-based.
To make OSX 10.8 Finder style side bar (with automatic gray Icon styling), need to use view-based table view with source-list highlight style.
With NIBs, this is typical job. Nothing hard. (see SidebarDemo) But I want to avoid any NIBs or Interface Builder. I want make the side bar purely programmatically.
In this case, I have big problem. AFAIK, there's no way to supply prototype view for specific cell. When I open .xib file, I see <tableColumn> is containing <prototypeCellViews>. And this specifies what view will be used for the column. I can't find how to set this programmatically using public API.
As a workaround, I tried to make cell manually using -[NSTableView makeViewWithIdentifier:owner:] and -[NSTableView viewAtColumn:row:makeIfNecessary:], but none of them returns view instance. I created a NSTableCellView, but it doesn't have image-view and text-field instances. And I also tried to set them, but the fields are marked as assign so the instances deallocated immediately. I tried to keep it by forcing retaining them, but it doesn't work. NSTableView doesn't manage them, so I am sure that table view don't like my implementation.
I believe there's a property to set this prototype-view for a column. But I can't find them. Where can I find the property and make system-default NSOutlineView with source-list style programmatically?
If you follow the example in SidebarDemo, they use a subclass of NSTableCellView for the detail rows. In order to emulate the InterfaceBuilder mojo, you can hook everything together in the constructor. The rest is the same as the demo (see outlineView:viewForTableColumn:item:).
#interface SCTableCellView : NSTableCellView
#end
#implementation SCTableCellView
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
[self setAutoresizingMask:NSViewWidthSizable];
NSImageView* iv = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 6, 16, 16)];
NSTextField* tf = [[NSTextField alloc] initWithFrame:NSMakeRect(21, 6, 200, 14)];
NSButton* btn = [[NSButton alloc] initWithFrame:NSMakeRect(0, 3, 16, 16)];
[iv setImageScaling:NSImageScaleProportionallyUpOrDown];
[iv setImageAlignment:NSImageAlignCenter];
[tf setBordered:NO];
[tf setDrawsBackground:NO];
[[btn cell] setControlSize:NSSmallControlSize];
[[btn cell] setBezelStyle:NSInlineBezelStyle];
[[btn cell] setButtonType:NSMomentaryPushInButton];
[[btn cell] setFont:[NSFont boldSystemFontOfSize:10]];
[[btn cell] setAlignment:NSCenterTextAlignment];
[self setImageView:iv];
[self setTextField:tf];
[self addSubview:iv];
[self addSubview:tf];
[self addSubview:btn];
return self;
}
- (NSButton*)button {
return [[self subviews] objectAtIndex:2];
}
- (void)viewWillDraw {
[super viewWillDraw];
NSButton* btn = [self button];
...
Here's #jeberle's code re-written in Swift 4 (five years later!):
class ProgrammaticTableCellView: NSTableCellView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.autoresizingMask = .width
let iv: NSImageView = NSImageView(frame: NSMakeRect(0, 6, 16, 16))
let tf: NSTextField = NSTextField(frame: NSMakeRect(21, 6, 200, 14))
let btn: NSButton = NSButton(frame: NSMakeRect(0, 3, 16, 16))
iv.imageScaling = .scaleProportionallyUpOrDown
iv.imageAlignment = .alignCenter
tf.isBordered = false
tf.drawsBackground = false
btn.cell?.controlSize = .small
// btn.bezelStyle = .inline // Deprecated?
btn.cell?.isBezeled = true // Closest property I can find.
// btn.cell?.setButtonType(.momentaryPushIn) // Deprecated?
btn.setButtonType(.momentaryPushIn)
btn.cell?.font = NSFont.boldSystemFont(ofSize: 10)
btn.cell?.alignment = .center
self.imageView = iv
self.textField = tf
self.addSubview(iv)
self.addSubview(tf)
self.addSubview(btn)
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var button: NSButton {
get {
return self.subviews[2] as! NSButton
}
}
}
Edit: I found a link (that will inevitably rot away – it was last revised in 2011) to Apple's SidebarDemo that #jeberle based his code on.
In addition to #jeberle 's answer, I need to note something more.
The key to keep the text-field and image-view is adding them as subviews of the NSTableCellView.
Set NSTableView.rowSizeStyle to a proper value (non-Custom which is default value) to make the table-view layout them automatically. Otherwise, you have to layout them completely yourself.
Do not touch frame and autoresizing stuffs if you want to use predefined NSTableViewRowSizeStyle value. Otherwise, the layout might be broken.
You can adjust row-height by providing private func outlineView(outlineView: NSOutlineView, heightOfRowByItem item: AnyObject) -> CGFloat delegate method. Setting NSTableView.rowHeight is not a good idea because it needs NSTableView.rowSizeStyle set to Custom which will turn off cell text/image layout management provided by default.
You can reuse row/cell views by settings NSView.identifier property. (example)

Redraw NSTableView with new data from file when NSViewController re-loaded?

I have a Mac OS X Document based app that has multiple NSViewControllers that I switch between and each view displays data from plist files in NSTableViews based on the user selections in the previous NSViewController's NSTableView. The problem I have is that I can't figure out what function can be called, every time a NSViewController gets loaded, to read the correct data from a file to display in the NSTableView. For UIViewControllers I used the function family of viewDidLoad, viewWillAppear, but I haven't been able to find the corresponding functions for NSViewController.
Currently I am using awakeFromNib, which works fine, but only the first time the NSViewController gets loaded. I've tried loadView, but that collapses the NSView. I assume that I need to do more setup to use loadView.
I'm using the View Swapping code from Hillegass's book Cocoa Programming for MAC OS X which switches ViewControllers with the following code:
- (void)displayViewController:(ManagingViewController *)vc
curBox: (NSBox *)windowBox
{
// End editing
NSWindow *w = [windowBox window];
BOOL ended = [w makeFirstResponder:w];
if (!ended) {
NSBeep();
return;
}
NSView *v = [vc view];
NSSize currentSize = [[windowBox contentView] frame].size;
NSSize newSize = [v frame].size;
float deltaWidth = newSize.width - currentSize.width;
float deltaHeight = newSize.height - currentSize.height;
NSRect windowFrame = [w frame];
windowFrame.size.height += deltaHeight;
windowFrame.origin.y -= deltaHeight;
windowFrame.size.width += deltaWidth;
[windowBox setContentView:nil];
[w setFrame:windowFrame
display:YES
animate:YES];
[windowBox setContentView:v];
// Put the view controller in the responder chain
[v setNextResponder:vc];
[vc setNextResponder:windowBox];
}
and puts the NSView Controller in the responder chain.
Is there some function I can call to setup the view every time I swap NSViewControllers? Can I check that a NSViewController has become the firstResponder?
This post provided the answer.
I added the following code:
- (void)viewWillLoad {
}
- (void)viewDidLoad {
}
- (void)loadView {
[self viewWillLoad];
[super loadView];
[self viewDidLoad];
}
and at the beginning of displayViewController I added
[vc loadView]

Resources