// Save the current graphics state first so we can restore it.
[[NSGraphicsContext currentContext] saveGraphicsState];
// Change the pattern phase.
[[NSGraphicsContext currentContext] setPatternPhase:
NSMakePoint(0,[self frame].size.height)];
// Stick the image in a color and fill the view with that color.
NSImage *anImage = [NSImage imageNamed:@"bricks"];
[[NSColor colorWithPatternImage:anImage] set];
NSRectFill([self bounds]);
// Restore the original graphics state.
[[NSGraphicsContext currentContext] restoreGraphicsState];
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
{
// other stuff might happen here
if ([self showsFirstResponder]) {
// showsFirstResponder is set for us by the NSControl that is drawing us.
NSRect focusRingFrame = cellFrame;
focusRingFrame.size.height -= 2.0f;
[NSGraphicsContextsaveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect: NSInsetRect(focusRingFrame, 4.0f, 4.0f)] fill];
[NSGraphicsContext restoreGraphicsState];
}
// other stuff might happen here
}
for (int i = 0; i < 9; i++ ) {
//create a slot object with a label
//add the object to the slots array
//sprite
[slots addObject: [[[Slot alloc] initWithSprite:[CCSprite spriteWithFile:@"hole.png"]
withPos_x:count%3*cellWidth+offset_x
withPos_y:count/3*cellHeight+offset_y
isOccupied:NO] autorelease]];
//add the sprite to the layer
[self addChild: ((Slot*) [slots objectAtIndex:i]).sprite];
count++;
}
There are many answers to this question, most of which will only work "some" of the time, and unfortunately that's not good enough.
Based on my tests of devices (all phones, at least one of which is not activated):
All devices tested returned a value for TelephonyManager.getDeviceId()
All GSM devices (all tested with a SIM) returned a value for TelephonyManager.getSimSerialNumber()
All CDMA devices returned null for getSimSerialNumber() (as expected)
All devices with a Google account added returned a value for ANDROID_ID
All CDMA devices returned the same value (or derivation of the same value) for both ANDROID_ID and TelephonyManager.getDeviceId() -- as long as a Google account has been added during setup.
I did not yet have a chance to test GSM devices with no SIM, a GSM device with no Google account added, or any of the devices in airplane mode.
So if you want something unique to the device itself, TM.getDeviceId() should be sufficient. Obviously some users are more paranoid than others, so it might be useful to hash 1 or more of these identifiers, so that the string is still virtually unique to the device, but does not explicitly identify the user's actual device. For example, using String.hashCode(), combined with a UUID:
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, tmPhone, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
String deviceId = deviceUuid.toString();
might result in something like: 00000000-54b3-e7c7-0000-000046bffd97
It works well enough for me.
As Richard mentions below, don't forget that you need permission to read the TelephonyManager properties, so add this to your manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
#import "VCExampleViewController.h"
//---import the header file for the view controller---
#import "SecondViewController.h"
@implementation VCExampleViewController
SecondViewController *secondViewController;
//---add the view of the second view controller to the current view---
-(IBAction) displayView:(id) sender{
secondViewController = [[SecondViewController alloc]
initWithNibName:@"SecondView"
bundle:nil];
[self.view addSubview:secondViewController.view];
}
- (void)dealloc {
//---release the memory used by the view controller---
[secondViewController release];
[super dealloc];
}
@end