Checking if UIGraphicsBeginImageContextWithOptions is supported - objective-c

I'm working on an iOS app. It currently only works on iOS 4 since I use the following method on several occasions: "UIGraphicsBeginImageContextWithOptions". This method is only available in iOS 4 and therefor my app currently crashes/doesn't work on iPhone OS 3. Aside from this method there is no reason why the app should not work on iPhone OS 3. How do I make a check to see wether or not this method is available ? I've tried the following without succes:
if([self respondsToSelector:#selector(UIGraphicsBeginImageContextWithOptions)]) {
UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0.0); // this will crop
}
else
{
UIGraphicsBeginImageContext(targetSize);
}
I've only tried variations like this:
if([self respondsToSelector:#selector(UIGraphicsBeginImageContextWithOptions:size:opaque:scale:)])
and
if([self respondsToSelector:#selector(UIGraphicsBeginImageContextWithOptions:)])
Without succes. Any help would be appreciated.

UIGraphicsBeginImageContextWithOptions is a C function, so you can't use Objective-C methods like -respondsToSelector: to test its existence.
You could, however, weak link the UIKit framework, and then check if UIGraphicsBeginImageContextWithOptions is NULL:
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(...);
} else {
UIGraphicsBeginImageContext(...);
}

I have the same problem. You could try testing the system version. This seems to work for me on the devices I tested.
char majorVersion = [[[UIDevice currentDevice] systemVersion] characterAtIndex: 0];
if (majorVersion == '2' || majorVersion == '3')
UIGraphicsBeginImageContext(...);
else
UIGraphicsBeginImageContextWithOptions(...);

I know this is an old question, but with new Xcode and iOS versions (upper than 9) any of this methods work for me.
I always check the system version in this way:
NSString *sysver = [[UIDevice currentDevice] systemVersion];
NSArray *versionNums = [sysver componentsSeparatedByString:#"."];
int majorVersion = [versionNums[0] intValue];
if (majorVersion > 3){
UIGraphicsBeginImageContextWithOptions(...);
}
else{
UIGraphicsBeginImageContext(...);
}
I hope this could help anyone.

Related

Detect Whether Mac Has MagSafe Charging Port Programmatically, Or Detect If USB-C Port Exists

I'm trying to figure out a way to determine whether a Mac has a MagSafe charging port programmatically. If that's not possible, alternatively, I suppose detecting whether any USB-C ports exist (and thus MagSafe is not present).
I've searched around the web for APIs for this as well as shell commands but haven't found any good solutions. I need this to work in a sandboxed environment distributed through the Mac App Store. Ideally, any solution would be in pure Cocoa/Objective-C, but I am also fine with achieving this via NSTask or similar.
Thank you so much in advance for any suggestions!
I ended up compiling a list from EveryMac.com of all the models with MagSafe:
- (bool) macHasMagSafe
{
NSString *macModel = #"";
size_t len = 0;
sysctlbyname("hw.model", NULL, &len, NULL, 0);
if (len)
{
char *model = malloc(len*sizeof(char));
sysctlbyname("hw.model", model, &len, NULL, 0);
macModel = [NSString stringWithUTF8String:model];
free(model);
}
else
{
macModel = #"UNKNOWN";
}
NSArray *magSafeModels = [[NSArray alloc] initWithObjects:
// MACBOOK MODELS
#"MacBook7,1",
#"MacBook6,1",
#"MacBook5,2",
#"MacBook5,1",
#"MacBook4,1",
#"MacBook3,1",
#"MacBook2,1",
#"MacBook1,1",
// MACBOOK PRO MODELS
#"MacBookPro11,5",
#"MacBookPro11,4",
#"MacBookPro12,1",
#"MacBookPro11,3",
#"MacBookPro11,2",
#"MacBookPro11,1",
#"MacBookPro10,1",
#"MacBookPro10,2",
#"MacBookPro9,1",
#"MacBookPro9,2",
#"MacBookPro8,3",
#"MacBookPro8,2",
#"MacBookPro8,1",
#"MacBookPro6,1",
#"MacBookPro6,2",
#"MacBookPro7,1",
#"MacBookPro5,2",
#"MacBookPro5,3",
#"MacBookPro5,4",
#"MacBookPro5,5",
#"MacBookPro5,1",
#"MacBookPro4,1",
#"MacBookPro3,1",
#"MacBookPro2,1",
#"MacBookPro2,2",
#"MacBookPro1,2",
#"MacBookPro1,1",
// MACBOOK AIR MODELS
#"MacBookAir7,2",
#"MacBookAir7,1",
#"MacBookAir6,2",
#"MacBookAir6,1",
#"MacBookAir5,2",
#"MacBookAir5,1",
#"MacBookAir4,2",
#"MacBookAir4,1",
#"MacBookAir3,2",
#"MacBookAir3,1",
#"MacBookAir2,1",
#"MacBookAir1,1",
nil];
if ([magSafeModels containsObject:macModel])
{
return YES;
//NSLog(#"This Mac has a MagSafe port");
}
else
{
return NO;
//NSLog(#"This Mac DOES NOT have a MagSafe port");
}
}

PMServerCreatePrinterList(kPMServerLocal, &printerList) returning OSStatus code 1282

Below is my code that is working with Command-Line tool but when I am creating an Cocoa App and I am using below code, not getting all printer queues from Mac OS X.
CFArrayRef printerList;
if (noErr == PMServerCreatePrinterList(kPMServerLocal, &printerList) && (nil != printerList))
{
CFIndex count = CFArrayGetCount(printerList);
NSLog(#"Count = %ld",count);
for (int index = 0; index < count; index++) {
PMPrinter myPrinter = (PMPrinter)CFArrayGetValueAtIndex(printerList, index);
NSString *name = (__bridge NSString *)(PMPrinterGetName(myPrinter));
NSLog(#"%#",name);
}
}
Please help if anyone have this issue.
This is a sandboxing issue. If your application didn't run in the sandbox, it'd work, otherwise you need the entitlement to allow it.
(Domain: org.cups.PrintingPrefs, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null)): accessing preferences outside an application's container requires user-preference-read or file-read-data sandbox access, detaching from cfprefsd
In your target's Capabilities view in Xcode, turn on the "Printing" capability under App Sandbox -> Hardware.

Cocos2d Remove a ChildByName

Hi I'm trying to add multiple nodes that are the same and assign them different names and then remove one of them when the count gets above 2. All of this works except it is crashing when removing the child by name and returning the error "Invalid Name" can anyone help? I think it is a little different because I am using Sprite Builder and also cocos2d removed "removeChildrenByTag"
-(void)addTowers:(NSTimer *) timer
{
towerCount++;
if (_towerDown.children.count >= 2) {
[_towerUp removeChildByName:#"tower1" cleanup:YES];
towerCount = 0;
}
else
{
//Tower Up
towerUp = [CCBReader load:#"TowerUp"];
towerUp.name = [NSString stringWithFormat:#"tower%d",towerCount];
[_towerUp addChild:towerUp z:1 name:towerUp.name];
}
There's a silly bug in the Cocos2d v3 code. Upgrade to the latest version or change
NSAssert( !name, #"Invalid name");
to
NSAssert( name, #"Invalid name");
in removeChildByName

Cocoa iTunes ScriptingBridge - How to play playlist shuffled / unshuffled playlist?

I see there is a property shuffle but it doesn't seem to do anything (it still plays shuffled).
I tried this but it plays shuffled. I'd like it unshuffled. Is this not possible?
iTunesPlaylist *p;
for (iTunesSource *source in [iTunes sources]) {
if ([source kind] == iTunesESrcLibrary) {
p = [[source userPlaylists] objectWithName:playlist];
break;
}
}
if (p != nil) {
p.shuffle = NO;
[p playOnce:NO];
}
A bug in iTunes 11 breaks the AppleScript (and hence Scripting Bridge) Shuffle command.

Get OSX version with objective-c

How would I be able to get the OSX version in objective-c? I would like to avoid using shell commands. E.g "10.5" or "10.4"
NSProcessInfo *pInfo = [NSProcessInfo processInfo];
NSString *version = [pInfo operatingSystemVersionString];
Sorry for the formatting, I'm using my iPad to answer this.
As of 10.10 you can use NSProcessInfo.processInfo.operatingSystemVersion to get a NSOperatingSystemVersion struct.
typedef struct {
NSInteger majorVersion;
NSInteger minorVersion;
NSInteger patchVersion;
} NSOperatingSystemVersion;
There's also a helpful isOperatingSystemAtLeastVersion: method.
NSOperatingSystemVersion minimumSupportedOSVersion = { .majorVersion = 10, .minorVersion = 12, .patchVersion = 0 };
BOOL isSupported = [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:minimumSupportedOSVersion];
You can parse it in this way to get the format you want:
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
NSArray *myarr = [[pinfo operatingSystemVersionString] componentsSeparatedByString:#" "];
NSString *version = [#"Mac OS X " stringByAppendingString:[myarr objectAtIndex:1]];
This f.e. will give you Mac OS X 10.6.8
You can use the Gestalt function to access the components of the OS version.
Old-time users of Gestalt may be amazed to find that it is still available in 64-bit.
See this response using NSAppKitVersionNumber in case you're using AppKit in your app as well (and want to run on 10.8+ as Gestalt is now deprecated):
How to know what Mac OS the app is running on?
add this code after #import
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
after adding above code please add below code where you want to see your os-version
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
NSLog(#"System version :%#",systemVersion);
you can easily get OS-version by above code
Thank you