Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

One more time...

  • Loading branch information...
commit 2f93e222f48e92232c4f722274ffcec6f4e15ccb 1 parent 93e5ea6
Erica Sadun authored
90 UIDevice-Hardware.h
... ... @@ -1,90 +0,0 @@
1   -/*
2   - Erica Sadun, http://ericasadun.com
3   - iPhone Developer's Cookbook, 3.0 Edition
4   - BSD License, Use at your own risk
5   - */
6   -
7   -#import <UIKit/UIKit.h>
8   -
9   -#define IPHONE_1G_NAMESTRING @"iPhone 1G"
10   -#define IPHONE_3G_NAMESTRING @"iPhone 3G"
11   -#define IPHONE_3GS_NAMESTRING @"iPhone 3GS"
12   -#define IPHONE_UNKNOWN_NAMESTRING @"Unknown iPhone"
13   -
14   -#define IPOD_1G_NAMESTRING @"iPod touch 1G"
15   -#define IPOD_2G_NAMESTRING @"iPod touch 2G"
16   -#define IPOD_UNKNOWN_NAMESTRING @"Unknown iPod"
17   -
18   -#define IPOD_FAMILY_UNKNOWN_DEVICE @"Unknown device in the iPhone/iPod family"
19   -
20   -#define IPHONE_SIMULATOR_NAMESTRING @"iPhone Simulator"
21   -
22   -typedef enum {
23   - UIDeviceUnknown,
24   - UIDeviceiPhoneSimulator,
25   - UIDevice1GiPhone,
26   - UIDevice1GiPod,
27   - UIDevice3GiPhone,
28   - UIDevice3GSiPhone,
29   - UIDevice2GiPod,
30   - UIDeviceUnknowniPhone,
31   - UIDeviceUnknowniPod,
32   -} UIDevicePlatform;
33   -
34   -typedef enum {
35   - UIDeviceFirmware2,
36   - UIDeviceFirmware3,
37   -} UIDeviceFirmware;
38   -
39   -enum {
40   - UIDeviceSupportsTelephony = 1 << 0,
41   - UIDeviceSupportsSMS = 1 << 1,
42   - UIDeviceSupportsStillCamera = 1 << 2,
43   - UIDeviceSupportsAutofocusCamera = 1 << 3,
44   - UIDeviceSupportsVideoCamera = 1 << 4,
45   - UIDeviceSupportsWifi = 1 << 5,
46   - UIDeviceSupportsAccelerometer = 1 << 6,
47   - UIDeviceSupportsLocationServices = 1 << 7,
48   - UIDeviceSupportsGPS = 1 << 8,
49   - UIDeviceSupportsMagnetometer = 1 << 9,
50   - UIDeviceSupportsBuiltInMicrophone = 1 << 10,
51   - UIDeviceSupportsExternalMicrophone = 1 << 11,
52   - UIDeviceSupportsOPENGLES1 = 1 << 12,
53   - UIDeviceSupportsOPENGLES2 = 1 << 13,
54   - UIDeviceBuiltInSpeaker = 1 << 14,
55   - UIDeviceSupportsVibration = 1 << 15,
56   - UIDeviceBuiltInProximitySensor = 1 << 16,
57   - UIDeviceSupportsAccessibility = 1 << 17,
58   - UIDeviceSupportsVoiceControl = 1 << 18,
59   - UIDeviceSupportsBrightnessSensor = 1 << 19,
60   -};
61   -
62   -@interface UIDevice (Hardware)
63   -
64   -+ (NSString *) platform;
65   -+ (NSUInteger) platformType;
66   -+ (NSUInteger) platformCapabilities;
67   -+ (NSString *) platformString;
68   -+ (NSString *) platformCode;
69   -
70   -+ (NSArray *) capabilityArray;
71   -
72   -+ (NSUInteger) cpuFrequency;
73   -+ (NSUInteger) busFrequency;
74   -+ (NSUInteger) totalMemory;
75   -+ (NSUInteger) userMemory;
76   -
77   -+ (NSNumber *) totalDiskSpace;
78   -+ (NSNumber *) freeDiskSpace;
79   -
80   -+ (NSString *) macaddress;
81   -@end
82   -
83   -@interface UIDevice (Orientation)
84   -- (BOOL) isLandscape;
85   -- (BOOL) isPortrait;
86   -- (NSString *) orientationString;
87   -@property (nonatomic, readonly) BOOL isLandscape;
88   -@property (nonatomic, readonly) BOOL isPortrait;
89   -@property (nonatomic, readonly) NSString *orientationString;
90   -@end
407 UIDevice-Hardware.m
... ... @@ -1,407 +0,0 @@
1   -/*
2   - Erica Sadun, http://ericasadun.com
3   - iPhone Developer's Cookbook, 3.0 Edition
4   - BSD License, Use at your own risk
5   - */
6   -
7   -// Thanks to Emanuele Vulcano, Kevin Ballard/Eridius, Ryandjohnson, Matt Brown, etc.
8   -// TTD: - Bluetooth? Screen pixels? Dot pitch? Accelerometer? GPS enabled/disabled
9   -
10   -#include <sys/sysctl.h>
11   -#include <net/if.h>
12   -#include <net/if_dl.h>
13   -#import "UIDevice-Hardware.h"
14   -
15   -@implementation UIDevice (Hardware)
16   -
17   -/*
18   - Platforms
19   -
20   - iPhone1,1 -> iPhone 1G
21   - iPhone1,2 -> iPhone 3G
22   - iPhone2,1 -> iPhone 3GS
23   -
24   - iPod1,1 -> iPod touch 1G
25   - iPod2,1 -> iPod touch 2G
26   -
27   - i386 -> iPhone Simulator
28   -*/
29   -
30   -
31   -#pragma mark sysctlbyname utils
32   -+ (NSString *) getSysInfoByName:(char *)typeSpecifier
33   -{
34   - size_t size;
35   - sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
36   - char *answer = malloc(size);
37   - sysctlbyname(typeSpecifier, answer, &size, NULL, 0);
38   - NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
39   - free(answer);
40   - return results;
41   -}
42   -
43   -+ (NSString *) platform
44   -{
45   - return [self getSysInfoByName:"hw.machine"];
46   -}
47   -
48   -#pragma mark sysctl utils
49   -+ (NSUInteger) getSysInfo: (uint) typeSpecifier
50   -{
51   - size_t size = sizeof(int);
52   - int results;
53   - int mib[2] = {CTL_HW, typeSpecifier};
54   - sysctl(mib, 2, &results, &size, NULL, 0);
55   - return (NSUInteger) results;
56   -}
57   -
58   -+ (NSUInteger) cpuFrequency
59   -{
60   - return [self getSysInfo:HW_CPU_FREQ];
61   -}
62   -
63   -+ (NSUInteger) busFrequency
64   -{
65   - return [self getSysInfo:HW_BUS_FREQ];
66   -}
67   -
68   -+ (NSUInteger) totalMemory
69   -{
70   - return [self getSysInfo:HW_PHYSMEM];
71   -}
72   -
73   -+ (NSUInteger) userMemory
74   -{
75   - return [self getSysInfo:HW_USERMEM];
76   -}
77   -
78   -+ (NSUInteger) maxSocketBufferSize
79   -{
80   - return [self getSysInfo:KIPC_MAXSOCKBUF];
81   -}
82   -
83   -#pragma mark file system -- Thanks Joachim Bean!
84   -
85   -+ (NSNumber *) totalDiskSpace
86   -{
87   - NSDictionary *fattributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:NSHomeDirectory()];
88   - return [fattributes objectForKey:NSFileSystemSize];
89   -}
90   -
91   -+ (NSNumber *) freeDiskSpace
92   -{
93   - NSDictionary *fattributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath:NSHomeDirectory()];
94   - return [fattributes objectForKey:NSFileSystemFreeSize];
95   -}
96   -
97   -#pragma mark platform type and name utils
98   -+ (NSUInteger) platformType
99   -{
100   - NSString *platform = [self platform];
101   - if ([platform isEqualToString:@"iPhone1,1"]) return UIDevice1GiPhone;
102   - if ([platform isEqualToString:@"iPhone1,2"]) return UIDevice3GiPhone;
103   - if ([platform isEqualToString:@"iPhone2,1"]) return UIDevice3GSiPhone;
104   -
105   - if ([platform isEqualToString:@"iPod1,1"]) return UIDevice1GiPod;
106   - if ([platform isEqualToString:@"iPod2,1"]) return UIDevice2GiPod;
107   -
108   - if ([platform hasPrefix:@"iPhone"]) return UIDeviceUnknowniPhone;
109   - if ([platform hasPrefix:@"iPod"]) return UIDeviceUnknowniPod;
110   -
111   - if ([platform hasSuffix:@"86"]) return UIDeviceiPhoneSimulator;
112   - return UIDeviceUnknown;
113   -}
114   -
115   -+ (NSString *) platformString
116   -{
117   - switch ([self platformType])
118   - {
119   - case UIDevice1GiPhone: return IPHONE_1G_NAMESTRING;
120   - case UIDevice3GiPhone: return IPHONE_3G_NAMESTRING;
121   - case UIDevice3GSiPhone: return IPHONE_3GS_NAMESTRING;
122   - case UIDeviceUnknowniPhone: return IPHONE_UNKNOWN_NAMESTRING;
123   -
124   - case UIDevice1GiPod: return IPOD_1G_NAMESTRING;
125   - case UIDevice2GiPod: return IPOD_2G_NAMESTRING;
126   - case UIDeviceUnknowniPod: return IPOD_UNKNOWN_NAMESTRING;
127   -
128   - case UIDeviceiPhoneSimulator: return IPHONE_SIMULATOR_NAMESTRING;
129   -
130   - default: return IPOD_FAMILY_UNKNOWN_DEVICE;
131   - }
132   -}
133   -
134   -#pragma mark platform capabilities
135   -+ (NSUInteger) platformCapabilities
136   -{
137   - switch ([self platformType])
138   - {
139   - case UIDevice1GiPhone:
140   - return
141   - (UIDeviceSupportsTelephony |
142   - UIDeviceSupportsSMS |
143   - UIDeviceSupportsStillCamera |
144   - // UIDeviceSupportsAutofocusCamera |
145   - // UIDeviceSupportsVideoCamera |
146   - UIDeviceSupportsWifi |
147   - UIDeviceSupportsAccelerometer |
148   - UIDeviceSupportsLocationServices |
149   - // UIDeviceSupportsGPS |
150   - // UIDeviceSupportsMagnetometer |
151   - UIDeviceSupportsBuiltInMicrophone |
152   - UIDeviceSupportsExternalMicrophone |
153   - UIDeviceSupportsOPENGLES1 |
154   - // UIDeviceSupportsOPENGLES2 |
155   - UIDeviceBuiltInSpeaker |
156   - UIDeviceSupportsVibration |
157   - UIDeviceBuiltInProximitySensor |
158   - // UIDeviceSupportsAccessibility |
159   - // UIDeviceSupportsVoiceControl |
160   - UIDeviceSupportsBrightnessSensor);
161   -
162   - case UIDevice3GiPhone:
163   - return
164   - (UIDeviceSupportsTelephony |
165   - UIDeviceSupportsSMS |
166   - UIDeviceSupportsStillCamera |
167   - // UIDeviceSupportsAutofocusCamera |
168   - // UIDeviceSupportsVideoCamera |
169   - UIDeviceSupportsWifi |
170   - UIDeviceSupportsAccelerometer |
171   - UIDeviceSupportsLocationServices |
172   - UIDeviceSupportsGPS |
173   - // UIDeviceSupportsMagnetometer |
174   - UIDeviceSupportsBuiltInMicrophone |
175   - UIDeviceSupportsExternalMicrophone |
176   - UIDeviceSupportsOPENGLES1 |
177   - // UIDeviceSupportsOPENGLES2 |
178   - UIDeviceBuiltInSpeaker |
179   - UIDeviceSupportsVibration |
180   - UIDeviceBuiltInProximitySensor |
181   - // UIDeviceSupportsAccessibility |
182   - // UIDeviceSupportsVoiceControl |
183   - UIDeviceSupportsBrightnessSensor);
184   -
185   - case UIDevice3GSiPhone:
186   - return
187   - (UIDeviceSupportsTelephony |
188   - UIDeviceSupportsSMS |
189   - UIDeviceSupportsStillCamera |
190   - UIDeviceSupportsAutofocusCamera |
191   - UIDeviceSupportsVideoCamera |
192   - UIDeviceSupportsWifi |
193   - UIDeviceSupportsAccelerometer |
194   - UIDeviceSupportsLocationServices |
195   - UIDeviceSupportsGPS |
196   - UIDeviceSupportsMagnetometer |
197   - UIDeviceSupportsBuiltInMicrophone |
198   - UIDeviceSupportsExternalMicrophone |
199   - UIDeviceSupportsOPENGLES1 |
200   - UIDeviceSupportsOPENGLES2 |
201   - UIDeviceBuiltInSpeaker |
202   - UIDeviceSupportsVibration |
203   - UIDeviceBuiltInProximitySensor |
204   - UIDeviceSupportsAccessibility |
205   - UIDeviceSupportsVoiceControl |
206   - UIDeviceSupportsBrightnessSensor);
207   -
208   - case UIDeviceUnknowniPhone: return 0;
209   -
210   - case UIDevice1GiPod:
211   - return
212   - (// UIDeviceSupportsTelephony |
213   - // UIDeviceSupportsSMS |
214   - // UIDeviceSupportsStillCamera |
215   - // UIDeviceSupportsAutofocusCamera |
216   - // UIDeviceSupportsVideoCamera |
217   - UIDeviceSupportsWifi |
218   - UIDeviceSupportsAccelerometer |
219   - UIDeviceSupportsLocationServices |
220   - // UIDeviceSupportsGPS |
221   - // UIDeviceSupportsMagnetometer |
222   - // UIDeviceSupportsBuiltInMicrophone |
223   - UIDeviceSupportsExternalMicrophone |
224   - UIDeviceSupportsOPENGLES1 |
225   - // UIDeviceSupportsOPENGLES2 |
226   - // UIDeviceBuiltInSpeaker |
227   - // UIDeviceSupportsVibration |
228   - // UIDeviceBuiltInProximitySensor |
229   - // UIDeviceSupportsAccessibility |
230   - // UIDeviceSupportsVoiceControl |
231   - UIDeviceSupportsBrightnessSensor);
232   -
233   - case UIDevice2GiPod:
234   - return
235   - (// UIDeviceSupportsTelephony |
236   - // UIDeviceSupportsSMS |
237   - // UIDeviceSupportsStillCamera |
238   - // UIDeviceSupportsAutofocusCamera |
239   - // UIDeviceSupportsVideoCamera |
240   - UIDeviceSupportsWifi |
241   - UIDeviceSupportsAccelerometer |
242   - UIDeviceSupportsLocationServices |
243   - // UIDeviceSupportsGPS |
244   - // UIDeviceSupportsMagnetometer |
245   - // UIDeviceSupportsBuiltInMicrophone |
246   - UIDeviceSupportsExternalMicrophone |
247   - UIDeviceSupportsOPENGLES1 |
248   - // UIDeviceSupportsOPENGLES2 |
249   - UIDeviceBuiltInSpeaker |
250   - // UIDeviceSupportsVibration |
251   - // UIDeviceBuiltInProximitySensor |
252   - // UIDeviceSupportsAccessibility |
253   - // UIDeviceSupportsVoiceControl |
254   - UIDeviceSupportsBrightnessSensor);
255   -
256   - case UIDeviceUnknowniPod: return 0;
257   -
258   - case UIDeviceiPhoneSimulator:
259   - return
260   - (// UIDeviceSupportsTelephony |
261   - // UIDeviceSupportsSMS |
262   - // UIDeviceSupportsStillCamera |
263   - // UIDeviceSupportsAutofocusCamera |
264   - // UIDeviceSupportsVideoCamera |
265   - UIDeviceSupportsWifi |
266   - // UIDeviceSupportsAccelerometer |
267   - UIDeviceSupportsLocationServices |
268   - // UIDeviceSupportsGPS |
269   - // UIDeviceSupportsMagnetometer |
270   - // UIDeviceSupportsBuiltInMicrophone |
271   - // UIDeviceSupportsExternalMicrophone |
272   - UIDeviceSupportsOPENGLES1 |
273   - // UIDeviceSupportsOPENGLES2 |
274   - UIDeviceBuiltInSpeaker);
275   - // UIDeviceSupportsVibration |
276   - // UIDeviceBuiltInProximitySensor |
277   - // UIDeviceSupportsAccessibility |
278   - // UIDeviceSupportsVoiceControl |
279   - // UIDeviceSupportsBrightnessSensor;
280   -
281   - default: return 0;
282   - }
283   -}
284   -
285   -+ (NSArray *) capabilityArray
286   -{
287   - NSUInteger flags = [self platformCapabilities];
288   - NSMutableArray *array = [NSMutableArray array];
289   -
290   - if (flags & UIDeviceSupportsTelephony) [array addObject:@"Telephony"];
291   - if (flags & UIDeviceSupportsSMS) [array addObject:@"SMS"];
292   - if (flags & UIDeviceSupportsStillCamera) [array addObject:@"Still Camera"];
293   - if (flags & UIDeviceSupportsAutofocusCamera) [array addObject:@"AutoFocus Camera"];
294   - if (flags & UIDeviceSupportsVideoCamera) [array addObject:@"Video Camera"];
295   - if (flags & UIDeviceSupportsWifi) [array addObject:@"WiFi"];
296   - if (flags & UIDeviceSupportsAccelerometer) [array addObject:@"Accelerometer"];
297   - if (flags & UIDeviceSupportsLocationServices) [array addObject:@"Location Services"];
298   - if (flags & UIDeviceSupportsGPS) [array addObject:@"GPS"];
299   - if (flags & UIDeviceSupportsMagnetometer) [array addObject:@"Magnetometer"];
300   - if (flags & UIDeviceSupportsBuiltInMicrophone) [array addObject:@"Built-in Microphone"];
301   - if (flags & UIDeviceSupportsExternalMicrophone) [array addObject:@"External Microphone Support"];
302   - if (flags & UIDeviceSupportsOPENGLES1) [array addObject:@"OpenGL ES 1.x"];
303   - if (flags & UIDeviceSupportsOPENGLES2) [array addObject:@"OpenGL ES 2.x"];
304   - if (flags & UIDeviceBuiltInSpeaker) [array addObject:@"Built-in Speaker"];
305   - if (flags & UIDeviceSupportsVibration) [array addObject:@"Vibration"];
306   - if (flags & UIDeviceBuiltInProximitySensor) [array addObject:@"Proximity Sensor"];
307   - if (flags & UIDeviceSupportsAccessibility) [array addObject:@"Accessibility"];
308   - if (flags & UIDeviceSupportsVoiceControl) [array addObject:@"Voice Control"];
309   - if (flags & UIDeviceSupportsBrightnessSensor) [array addObject:@"Brightness Sensor"];
310   -
311   - return array;
312   -}
313   -
314   -#pragma mark MAC addy
315   -// Return the local MAC addy
316   -// Courtesy of FreeBSD hackers email list
317   -+ (NSString *) macaddress
318   -{
319   - int mib[6];
320   - size_t len;
321   - char *buf;
322   - unsigned char *ptr;
323   - struct if_msghdr *ifm;
324   - struct sockaddr_dl *sdl;
325   -
326   - mib[0] = CTL_NET;
327   - mib[1] = AF_ROUTE;
328   - mib[2] = 0;
329   - mib[3] = AF_LINK;
330   - mib[4] = NET_RT_IFLIST;
331   -
332   - if ((mib[5] = if_nametoindex("en0")) == 0) {
333   - printf("Error: if_nametoindex error\n");
334   - return NULL;
335   - }
336   -
337   - if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
338   - printf("Error: sysctl, take 1\n");
339   - return NULL;
340   - }
341   -
342   - if ((buf = malloc(len)) == NULL) {
343   - printf("Could not allocate memory. error!\n");
344   - return NULL;
345   - }
346   -
347   - if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
348   - printf("Error: sysctl, take 2");
349   - return NULL;
350   - }
351   -
352   - ifm = (struct if_msghdr *)buf;
353   - sdl = (struct sockaddr_dl *)(ifm + 1);
354   - ptr = (unsigned char *)LLADDR(sdl);
355   - NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
356   - free(buf);
357   - return [outstring uppercaseString];
358   -}
359   -
360   -+ (NSString *) platformCode
361   -{
362   - switch ([self platformType])
363   - {
364   - case UIDevice1GiPhone: return @"M68";
365   - case UIDevice3GiPhone: return @"N82";
366   - case UIDevice3GSiPhone: return @"N88";
367   - case UIDeviceUnknowniPhone: return nil;
368   -
369   - case UIDevice1GiPod: return @"N45";
370   - case UIDevice2GiPod: return @"N72";
371   - // case UIDevice3GiPod: return @"N80";
372   - case UIDeviceUnknowniPod: return IPOD_UNKNOWN_NAMESTRING;
373   -
374   - case UIDeviceiPhoneSimulator: return IPHONE_SIMULATOR_NAMESTRING;
375   -
376   - default: return IPOD_FAMILY_UNKNOWN_DEVICE;
377   - }
378   -}
379   -@end
380   -
381   -@implementation UIDevice (Orientation)
382   -- (BOOL) isLandscape
383   -{
384   - return (self.orientation == UIDeviceOrientationLandscapeLeft) || (self.orientation == UIDeviceOrientationLandscapeRight);
385   -}
386   -
387   -- (BOOL) isPortrait
388   -{
389   - return (self.orientation == UIDeviceOrientationPortrait) || (self.orientation == UIDeviceOrientationPortraitUpsideDown);
390   -}
391   -
392   -- (NSString *) orientationString
393   -{
394   - switch ([[UIDevice currentDevice] orientation])
395   - {
396   - case UIDeviceOrientationUnknown: return @"Unknown";
397   - case UIDeviceOrientationPortrait: return @"Portrait";
398   - case UIDeviceOrientationPortraitUpsideDown: return @"Portrait Upside Down";
399   - case UIDeviceOrientationLandscapeLeft: return @"Landscape Left";
400   - case UIDeviceOrientationLandscapeRight: return @"Landscape Right";
401   - case UIDeviceOrientationFaceUp: return @"Face Up";
402   - case UIDeviceOrientationFaceDown: return @"Face Down";
403   - default: break;
404   - }
405   - return nil;
406   -}
407   -@end
40 UIDevice-IOKitExtensions.h
... ... @@ -1,40 +0,0 @@
1   -/*
2   - Erica Sadun, http://ericasadun.com
3   - iPhone Developer's Cookbook, 3.0 Edition
4   - BSD License, Use at your own risk
5   - */
6   -
7   -/*
8   - http://broadcast.oreilly.com/2009/04/iphone-dev-iokit---the-missing.html
9   -
10   - In Xcode, I was surprised to see that Apple didn't include IOKit header files. When I tried to
11   - add #import <IOKit/IOKit.h>, the file could not be found. So I manually put together a simple
12   - header file by hand, added IOKit to my frameworks and attempted to compile.
13   -
14   - As you can see from the screenshot at the top of this post, I failed to do so. Xcode complained
15   - that the IOKit framework could not be found. Despite being filed as public, IOKit is apparently
16   - not meant to be used by the general public. As iPhone evangelist Matt Drance tweeted,
17   - "IOKit is not public on iPhone. Lack of headers and docs is rarely an oversight."
18   -
19   - In the official docs, I found a quote that described IOKit as such: "Contains interfaces used by
20   - the device. Do not include this framework directly." So in the end, my desire to access that IOKit
21   - information was thwarted. For whatever reason, Apple has chosen to list it as a public framework
22   - but the reality is that it is not.
23   -*/
24   -
25   -#import <UIKit/UIKit.h>
26   -
27   -#define SUPPORTS_IOKIT_EXTENSIONS 1
28   -
29   -/*
30   - * To use, you must add the (semi)public IOKit framework before compiling
31   - */
32   -
33   -#if SUPPORTS_IOKIT_EXTENSIONS
34   -@interface UIDevice (IOKit_Extensions)
35   -+ (NSString *) imei;
36   -+ (NSString *) serialnumber;
37   -+ (NSString *) backlightlevel;
38   -@end
39   -#endif
40   -
120 UIDevice-IOKitExtensions.m
... ... @@ -1,120 +0,0 @@
1   -/*
2   - Erica Sadun, http://ericasadun.com
3   - iPhone Developer's Cookbook, 3.0 Edition
4   - BSD License, Use at your own risk
5   - */
6   -
7   -#import "UIDevice-IOKitExtensions.h"
8   -#include <sys/types.h>
9   -#include <sys/sysctl.h>
10   -#import <mach/mach_host.h>
11   -#include <netinet/in.h>
12   -#include <arpa/inet.h>
13   -#include <netdb.h>
14   -#include <ifaddrs.h>
15   -#include <sys/socket.h>
16   -#include <net/if.h>
17   -#include <net/if_dl.h>
18   -#include <ifaddrs.h>
19   -
20   -#if SUPPORTS_IOKIT_EXTENSIONS
21   -#pragma mark IOKit miniheaders
22   -
23   -#define kIODeviceTreePlane "IODeviceTree"
24   -
25   -enum {
26   - kIORegistryIterateRecursively = 0x00000001,
27   - kIORegistryIterateParents = 0x00000002
28   -};
29   -
30   -typedef mach_port_t io_object_t;
31   -typedef io_object_t io_registry_entry_t;
32   -typedef char io_name_t[128];
33   -typedef UInt32 IOOptionBits;
34   -
35   -CFTypeRef
36   -IORegistryEntrySearchCFProperty(
37   - io_registry_entry_t entry,
38   - const io_name_t plane,
39   - CFStringRef key,
40   - CFAllocatorRef allocator,
41   - IOOptionBits options );
42   -
43   -kern_return_t
44   -IOMasterPort( mach_port_t bootstrapPort,
45   - mach_port_t * masterPort );
46   -
47   -io_registry_entry_t
48   -IORegistryGetRootEntry(
49   - mach_port_t masterPort );
50   -
51   -CFTypeRef
52   -IORegistryEntrySearchCFProperty(
53   - io_registry_entry_t entry,
54   - const io_name_t plane,
55   - CFStringRef key,
56   - CFAllocatorRef allocator,
57   - IOOptionBits options );
58   -
59   -kern_return_t mach_port_deallocate
60   -(ipc_space_t task,
61   - mach_port_name_t name);
62   -
63   -
64   -@implementation UIDevice (IOKit_Extensions)
65   -#pragma mark IOKit Utils
66   -NSArray *getValue(NSString *iosearch)
67   -{
68   - mach_port_t masterPort;
69   - CFTypeID propID = (CFTypeID) NULL;
70   - unsigned int bufSize;
71   -
72   - kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
73   - if (kr != noErr) return nil;
74   -
75   - io_registry_entry_t entry = IORegistryGetRootEntry(masterPort);
76   - if (entry == MACH_PORT_NULL) return nil;
77   -
78   - CFTypeRef prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, (CFStringRef) iosearch, nil, kIORegistryIterateRecursively);
79   - if (!prop) return nil;
80   -
81   - propID = CFGetTypeID(prop);
82   - if (!(propID == CFDataGetTypeID()))
83   - {
84   - mach_port_deallocate(mach_task_self(), masterPort);
85   - return nil;
86   - }
87   -
88   - CFDataRef propData = (CFDataRef) prop;
89   - if (!propData) return nil;
90   -
91   - bufSize = CFDataGetLength(propData);
92   - if (!bufSize) return nil;
93   -
94   - NSString *p1 = [[[NSString alloc] initWithBytes:CFDataGetBytePtr(propData) length:bufSize encoding:1] autorelease];
95   - mach_port_deallocate(mach_task_self(), masterPort);
96   - return [p1 componentsSeparatedByString:@"\0"];
97   -}
98   -
99   -+ (NSString *) imei
100   -{
101   - NSArray *results = getValue(@"device-imei");
102   - if (results) return [results objectAtIndex:0];
103   - return nil;
104   -}
105   -
106   -+ (NSString *) serialnumber
107   -{
108   - NSArray *results = getValue(@"serial-number");
109   - if (results) return [results objectAtIndex:0];
110   - return nil;
111   -}
112   -
113   -+ (NSString *) backlightlevel
114   -{
115   - NSArray *results = getValue(@"backlight-level");
116   - if (results) return [results objectAtIndex:0];
117   - return nil;
118   -}
119   -@end
120   -#endif
73 UIDevice-Reachability.h
... ... @@ -1,73 +0,0 @@
1   -/*
2   - Erica Sadun, http://ericasadun.com
3   - iPhone Developer's Cookbook, 3.0 Edition
4   - BSD License for anything not specifically marked as developed by a third party.
5   - Apple's code excluded.
6   - Use at your own risk
7   - */
8   -
9   -#import <UIKit/UIKit.h>
10   -
11   -#define SUPPORTS_UNDOCUMENTED_API 1
12   -
13   -@protocol ReachabilityWatcher <NSObject>
14   -- (void) reachabilityChanged;
15   -@end
16   -
17   -
18   -@interface UIDevice (Reachability)
19   -+ (NSString *) stringFromAddress: (const struct sockaddr *) address;
20   -+ (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address;
21   -
22   -+ (NSString *) hostname;
23   -+ (NSString *) getIPAddressForHost: (NSString *) theHost;
24   -+ (NSString *) localIPAddress;
25   -+ (NSString *) localWiFiIPAddress;
26   -+ (NSString *) whatismyipdotcom;
27   -
28   -+ (BOOL) hostAvailable: (NSString *) theHost;
29   -+ (BOOL) networkAvailable;
30   -+ (BOOL) activeWLAN;
31   -+ (BOOL) activeWWAN;
32   -+ (BOOL) performWiFiCheck;
33   -
34   -+ (BOOL) forceWWAN; // via Apple
35   -+ (void) shutdownWWAN; // via Apple
36   -
37   -+ (BOOL) scheduleReachabilityWatcher: (id) watcher;
38   -+ (void) unscheduleReachabilityWatcher;
39   -
40   -#ifdef SUPPORTS_UNDOCUMENTED_API
41   -// Don't use this code in real life, boys and girls. It is not App Store friendly.
42   -// It is, however, really nice for testing callbacks
43   -// DEVICE ONLY!!!!
44   -+ (void) setAPMode: (BOOL) yorn;
45   -#endif
46   -@end
47   -
48   -#ifdef SUPPORTS_UNDOCUMENTED_API
49   -@interface NSHost : NSObject
50   -{
51   - NSArray *names;
52   - NSArray *addresses;
53   - void *reserved;
54   -}
55   -
56   -+ (id)currentHost;
57   -+ (void)_fixNSHostLeak;
58   -+ (id)hostWithName:(id)fp8;
59   -+ (id)hostWithAddress:(id)fp8;
60   -+ (BOOL)isHostCacheEnabled;
61   -+ (void)setHostCacheEnabled:(BOOL)fp8;
62   -+ (void)flushHostCache;
63   -- (BOOL)isEqualToHost:(id)fp8;
64   -- (id)name;
65   -- (id)names;
66   -- (id)address;
67   -- (id)addresses;
68   -- (id)description;
69   -- (void)dealloc;
70   -
71   -@end
72   -#endif
73   -
325 UIDevice-Reachability.m
... ... @@ -1,325 +0,0 @@
1   -/*
2   - Erica Sadun, http://ericasadun.com
3   - iPhone Developer's Cookbook, 3.0 Edition
4   - BSD License for anything not specifically marked as developed by a third party.
5   - Apple's code excluded.
6   - Use at your own risk
7   - */
8   -
9   -#import <SystemConfiguration/SystemConfiguration.h>
10   -#include <arpa/inet.h>
11   -#include <netdb.h>
12   -#include <net/if.h>
13   -#include <ifaddrs.h>
14   -#import <dlfcn.h>
15   -#import "UIDevice-Reachability.h"
16   -#import "wwanconnect.h"
17   -
18   -@implementation UIDevice (Reachability)
19   -SCNetworkConnectionFlags connectionFlags;
20   -SCNetworkReachabilityRef reachability;
21   -
22   -#pragma mark Class IP and Host Utilities
23   -// This IP Utilities are mostly inspired by or derived from Apple code. Thank you Apple.
24   -
25   -+ (NSString *) stringFromAddress: (const struct sockaddr *) address
26   -{
27   - if(address && address->sa_family == AF_INET) {
28   - const struct sockaddr_in* sin = (struct sockaddr_in*) address;
29   - return [NSString stringWithFormat:@"%@:%d", [NSString stringWithUTF8String:inet_ntoa(sin->sin_addr)], ntohs(sin->sin_port)];
30   - }
31   -
32   - return nil;
33   -}
34   -
35   -+ (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address
36   -{
37   - if (!IPAddress || ![IPAddress length]) {
38   - return NO;
39   - }
40   -
41   - memset((char *) address, sizeof(struct sockaddr_in), 0);
42   - address->sin_family = AF_INET;
43   - address->sin_len = sizeof(struct sockaddr_in);
44   -
45   - int conversionResult = inet_aton([IPAddress UTF8String], &address->sin_addr);
46   - if (conversionResult == 0) {
47   - NSAssert1(conversionResult != 1, @"Failed to convert the IP address string into a sockaddr_in: %@", IPAddress);
48   - return NO;
49   - }
50   -
51   - return YES;
52   -}
53   -
54   -+ (NSString *) hostname
55   -{
56   - char baseHostName[255];
57   - int success = gethostname(baseHostName, 255);
58   - if (success != 0) return nil;
59   - baseHostName[255] = '\0';
60   -
61   - #if !TARGET_IPHONE_SIMULATOR
62   - return [NSString stringWithFormat:@"%s.local", baseHostName];
63   - #else
64   - return [NSString stringWithFormat:@"%s", baseHostName];
65   - #endif
66   -}
67   -
68   -+ (NSString *) getIPAddressForHost: (NSString *) theHost
69   -{
70   - struct hostent *host = gethostbyname([theHost UTF8String]);
71   - if (!host) {herror("resolv"); return NULL; }
72   - struct in_addr **list = (struct in_addr **)host->h_addr_list;
73   - NSString *addressString = [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding];
74   - return addressString;
75   -}
76   -
77   -+ (NSString *) localIPAddress
78   -{
79   - struct hostent *host = gethostbyname([[self hostname] UTF8String]);
80   - if (!host) {herror("resolv"); return nil;}
81   - struct in_addr **list = (struct in_addr **)host->h_addr_list;
82   - return [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding];
83   -}
84   -
85   -// Matt Brown's get WiFi IP addy solution
86   -// Author gave permission to use in Cookbook under cookbook license
87   -// http://mattbsoftware.blogspot.com/2009/04/how-to-get-ip-address-of-iphone-os-v221.html
88   -+ (NSString *) localWiFiIPAddress
89   -{
90   - BOOL success;
91   - struct ifaddrs * addrs;
92   - const struct ifaddrs * cursor;
93   -
94   - success = getifaddrs(&addrs) == 0;
95   - if (success) {
96   - cursor = addrs;
97   - while (cursor != NULL) {
98   - // the second test keeps from picking up the loopback address
99   - if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0)
100   - {
101   - NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
102   - if ([name isEqualToString:@"en0"]) // Wi-Fi adapter
103   - return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)];
104   - }
105   - cursor = cursor->ifa_next;
106   - }
107   - freeifaddrs(addrs);
108   - }
109   - return nil;
110   -}
111   -
112   -+ (NSString *) whatismyipdotcom
113   -{
114   - NSError *error;
115   - NSURL *ipURL = [NSURL URLWithString:@"http://www.whatismyip.com/automation/n09230945.asp"];
116   - NSString *ip = [NSString stringWithContentsOfURL:ipURL encoding:1 error:&error];
117   - return ip ? ip : [error localizedDescription];
118   -}
119   -
120   -+ (BOOL) hostAvailable: (NSString *) theHost
121   -{
122   -
123   - NSString *addressString = [self getIPAddressForHost:theHost];
124   - if (!addressString)
125   - {
126   - printf("Error recovering IP address from host name\n");
127   - return NO;
128   - }
129   -
130   - struct sockaddr_in address;
131   - BOOL gotAddress = [self addressFromString:addressString address:&address];
132   -
133   - if (!gotAddress)
134   - {
135   - printf("Error recovering sockaddr address from %s\n", [addressString UTF8String]);
136   - return NO;
137   - }
138   -
139   - SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&address);
140   - SCNetworkReachabilityFlags flags;
141   -
142   - BOOL didRetrieveFlags =SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
143   - CFRelease(defaultRouteReachability);
144   -
145   - if (!didRetrieveFlags)
146   - {
147   - printf("Error. Could not recover network reachability flags\n");
148   - return NO;
149   - }
150   -
151   - BOOL isReachable = flags & kSCNetworkFlagsReachable;
152   - return isReachable ? YES : NO;;
153   -}
154   -
155   -#pragma mark Checking Connections
156   -
157   -+ (void) pingReachabilityInternal
158   -{
159   - if (!reachability)
160   - {
161   - BOOL ignoresAdHocWiFi = NO;
162   - struct sockaddr_in ipAddress;
163   - bzero(&ipAddress, sizeof(ipAddress));
164   - ipAddress.sin_len = sizeof(ipAddress);
165   - ipAddress.sin_family = AF_INET;
166   - ipAddress.sin_addr.s_addr = htonl(ignoresAdHocWiFi ? INADDR_ANY : IN_LINKLOCALNETNUM);
167   -
168   - /* Can also create zero addy
169   - struct sockaddr_in zeroAddress;
170   - bzero(&zeroAddress, sizeof(zeroAddress));
171   - zeroAddress.sin_len = sizeof(zeroAddress);
172   - zeroAddress.sin_family = AF_INET; */
173   -
174   - reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&ipAddress);
175   - CFRetain(reachability);
176   - }
177   -
178   - // Recover reachability flags
179   - BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(reachability, &connectionFlags);
180   - if (!didRetrieveFlags) printf("Error. Could not recover network reachability flags\n");
181   -}
182   -
183   -+ (BOOL) networkAvailable
184   -{
185   - [self pingReachabilityInternal];
186   - BOOL isReachable = ((connectionFlags & kSCNetworkFlagsReachable) != 0);
187   - BOOL needsConnection = ((connectionFlags & kSCNetworkFlagsConnectionRequired) != 0);
188   - return (isReachable && !needsConnection) ? YES : NO;
189   -}
190   -
191   -+ (BOOL) activeWWAN
192   -{
193   - if (![self networkAvailable]) return NO;
194   - return ((connectionFlags & kSCNetworkReachabilityFlagsIsWWAN) != 0);
195   -}
196   -
197   -+ (BOOL) activeWLAN
198   -{
199   - return ([UIDevice localWiFiIPAddress] != nil);
200   -}
201   -
202   -
203   -#pragma mark WiFi Check and Alert
204   -+ (void) showAlert: (id) formatstring,...
205   -{
206   - va_list arglist;
207   - if (!formatstring) return;
208   - va_start(arglist, formatstring);
209   - id outstring = [[[NSString alloc] initWithFormat:formatstring arguments:arglist] autorelease];
210   - va_end(arglist);
211   -
212   - UIAlertView *av = [[[UIAlertView alloc] initWithTitle:outstring message:nil delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease];
213   - [av show];
214   -}
215   -
216   -+ (BOOL) performWiFiCheck
217   -{
218   - if (![self networkAvailable] || ![self activeWLAN])
219   - {
220   - [self performSelector:@selector(showAlert:) withObject:@"This application requires WiFi. Please enable WiFi in Settings and run this application again." afterDelay:0.5f];
221   - return NO;
222   - }
223   - return YES;
224   -}
225   -
226   -#pragma mark Forcing WWAN connection. Courtesy of Apple. Thank you Apple.
227   -MyStreamInfoPtr myInfoPtr;
228   -static void myClientCallback(void *refCon)
229   -{
230   - int *val = (int*)refCon;
231   - printf("myClientCallback entered - value from refCon is %d\n", *val);
232   -}
233   -
234   -+ (BOOL) forceWWAN
235   -{
236   - int value = 0;
237   - myInfoPtr = (MyStreamInfoPtr) StartWWAN(myClientCallback, &value);
238   - NSLog(@"%@", myInfoPtr ? @"Started WWAN" : @"Failed to start WWAN");
239   - return (!(myInfoPtr == NULL));
240   -}
241   -
242   -+ (void) shutdownWWAN
243   -{
244   - if (myInfoPtr) StopWWAN((MyInfoRef) myInfoPtr);
245   -}
246   -
247   -#pragma mark Monitoring reachability
248   -static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkConnectionFlags flags, void* info)
249   -{
250   - NSAutoreleasePool *pool = [NSAutoreleasePool new];
251   - [(id)info performSelector:@selector(reachabilityChanged)];
252   - [pool release];
253   -}
254   -
255   -+ (BOOL) scheduleReachabilityWatcher: (id) watcher
256   -{
257   - if (![watcher conformsToProtocol:@protocol(ReachabilityWatcher)])
258   - {
259   - NSLog(@"Watcher must conform to ReachabilityWatcher protocol. Cannot continue.");
260   - return NO;
261   - }
262   -
263   - [self pingReachabilityInternal];
264   -
265   - SCNetworkReachabilityContext context = {0, watcher, NULL, NULL, NULL};
266   - if(SCNetworkReachabilitySetCallback(reachability, ReachabilityCallback, &context))
267   - {
268   - if(!SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetCurrent(), kCFRunLoopCommonModes))
269   - {
270   - NSLog(@"Error: Could not schedule reachability");
271   - SCNetworkReachabilitySetCallback(reachability, NULL, NULL);
272   - return NO;
273   - }
274   - }
275   - else
276   - {
277   - NSLog(@"Error: Could not set reachability callback");
278   - return NO;
279   - }
280   -
281   - return YES;
282   -}
283   -
284   -+ (void) unscheduleReachabilityWatcher
285   -{
286   - SCNetworkReachabilitySetCallback(reachability, NULL, NULL);
287   - if (SCNetworkReachabilityUnscheduleFromRunLoop(reachability, CFRunLoopGetCurrent(), kCFRunLoopCommonModes))
288   - NSLog(@"Unscheduled reachability");
289   - else
290   - NSLog(@"Error: Could not unschedule reachability");
291   -
292   - CFRelease(reachability);
293   - reachability = nil;
294   -}
295   -
296   -#ifdef SUPPORTS_UNDOCUMENTED_API
297   -#define SBSERVPATH "/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices"
298   -#define UIKITPATH "/System/Library/Framework/UIKit.framework/UIKit"
299   -
300   -// Don't use this code in real life, boys and girls. It is not App Store friendly.
301   -// It is, however, really nice for testing callbacks
302   -+ (void) setAPMode: (BOOL) yorn
303   -{
304   - mach_port_t *thePort;
305   - void *uikit = dlopen(UIKITPATH, RTLD_LAZY);
306   - int (*SBSSpringBoardServerPort)() = dlsym(uikit, "SBSSpringBoardServerPort");
307   - thePort = (mach_port_t *)SBSSpringBoardServerPort();
308   - dlclose(uikit);
309   -
310   - // Link to SBSetAirplaneModeEnabled
311   - void *sbserv = dlopen(SBSERVPATH, RTLD_LAZY);
312   - int (*setAPMode)(mach_port_t* port, BOOL yorn) = dlsym(sbserv, "SBSetAirplaneModeEnabled");
313   - setAPMode(thePort, yorn);
314   - dlclose(sbserv);
315   -}
316   -
317   -// This uses the NSHost class illicitly
318   -+ (BOOL) networkAvailableByNSHost
319   -{
320   - // Unavailable has only one address: 127.0.0.1
321   - return !(([[[NSHost currentHost] addresses] count] == 1) &&
322   - [[UIDevice localIPAddress] isEqualToString:@"127.0.0.1"]);
323   -}
324   -#endif
325   -@end
1  do-git
@@ -5,7 +5,6 @@ if ($#argv != 1) then
5 5 exit 1
6 6 endif
7 7
8   -cp ../UIDevice/*.[hm] .
9 8 rm -rf build
10 9 rm .DS_Store
11 10 rm */.DS_Store

0 comments on commit 2f93e22

Please sign in to comment.
Something went wrong with that request. Please try again.