網頁

顯示具有 UIDevice 標籤的文章。 顯示所有文章
顯示具有 UIDevice 標籤的文章。 顯示所有文章

2012年6月14日 星期四

UIDevice抓取目前設備資訊

【環境:xCode4.3.2 , ios5.1】

<以下範例>

主要文件檔案請參考  UIDevice
 
NSLog(@"this : %@",[[UIDevice currentDevice] model]);
//獲取目前使用的設備
NSLog(@"%@",[[UIDevice currentDevice] name]);
// Name of the phone as named by user
NSLog(@"%@",[[UIDevice currentDevice] uniqueIdentifier]);
// A GUID like string
NSLog(@"%@",[[UIDevice currentDevice] systemName]);
// "iPhone OS"
NSLog(@"%@",[[UIDevice currentDevice] systemVersion]);
// "2.2.1"
NSLog(@"%@",[[UIDevice currentDevice] model]);
// "iPhone" on both devices
NSLog(@"%@",[[UIDevice currentDevice] localizedModel]);
// "iPhone" on both devices 

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

2012年6月13日 星期三

UIDevice 文件

【環境:xCode4.3.2】

說明:其實從xcode中可以看到,但可以從這份文件當中看出,UIDevice可以替我們做什麼事情,包含
UIDeviceOrientation;//旋轉
UIDeviceBatteryState;//電池
UIUserInterfaceIdiom;//裝置(ipad ,iphone)版本判定

你可以抓出一些資訊,像是
name;              // e.g. "My iPhone"
model;             // e.g. @"iPhone", @"iPod touch"
localizedModel;    // localized version of model
systemName;        // e.g. @"iOS"
systemVersion;     // e.g. @"4.0"
orientation;       // return current device orientation.  this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.
uniqueIdentifier  __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_5_0);  // a string unique to each device based on various hardware info.


<以下官方文件>
 
//
//  UIDevice.h
//  UIKit
//
//  Copyright (c) 2007-2011, Apple Inc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKitDefines.h>

typedef enum {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
} UIDeviceOrientation;

typedef enum {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,   // on battery, discharging
    UIDeviceBatteryStateCharging,    // plugged in, less than 100%
    UIDeviceBatteryStateFull,        // plugged in, at 100%
} UIDeviceBatteryState;              // available in iPhone 3.0

typedef enum {
#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    UIUserInterfaceIdiomPhone,           // iPhone and iPod touch style UI
    UIUserInterfaceIdiomPad,             // iPad style UI
#endif
} UIUserInterfaceIdiom;

/* The UI_USER_INTERFACE_IDIOM() macro is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.
 */
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

#define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
#define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)

UIKIT_CLASS_AVAILABLE(2_0) @interface UIDevice : NSObject {
 @private
    NSInteger _numDeviceOrientationObservers;
    float     _batteryLevel;
    struct {
 unsigned int batteryMonitoringEnabled:1;
 unsigned int proximityMonitoringEnabled:1;
 unsigned int expectsFaceContactInLandscape:1;
        unsigned int orientation:3;
        unsigned int batteryState:2;
        unsigned int proximityState:1;
    } _deviceFlags;
}

  (UIDevice *)currentDevice;

@property(nonatomic,readonly,retain) NSString    *name;              // e.g. "My iPhone"
@property(nonatomic,readonly,retain) NSString    *model;             // e.g. @"iPhone", @"iPod touch"
@property(nonatomic,readonly,retain) NSString    *localizedModel;    // localized version of model
@property(nonatomic,readonly,retain) NSString    *systemName;        // e.g. @"iOS"
@property(nonatomic,readonly,retain) NSString    *systemVersion;     // e.g. @"4.0"
@property(nonatomic,readonly) UIDeviceOrientation orientation;       // return current device orientation.  this will return UIDeviceOrientationUnknown unless device orientation notifications are being generated.
@property(nonatomic,readonly,retain) NSString    *uniqueIdentifier  __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_5_0);  // a string unique to each device based on various hardware info.

@property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications;
- (void)beginGeneratingDeviceOrientationNotifications;      // nestable
- (void)endGeneratingDeviceOrientationNotifications;

@property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  // default is NO
@property(nonatomic,readonly) UIDeviceBatteryState          batteryState __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  // UIDeviceBatteryStateUnknown if monitoring disabled
@property(nonatomic,readonly) float                         batteryLevel __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  // 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown

@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is NO
@property(nonatomic,readonly)                            BOOL proximityState __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  // always returns NO if no proximity detector

@property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);

@property(nonatomic,readonly) UIUserInterfaceIdiom userInterfaceIdiom __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2);

- (void)playInputClick __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_2);  // Plays a click only if an enabling input view is on-screen and user has enabled input clicks.

@end

@protocol UIInputViewAudioFeedback <NSObject>
@optional

@property (nonatomic, readonly) BOOL enableInputClicksWhenVisible; // If YES, an input view will enable playInputClick.

@end

UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification;
UIKIT_EXTERN NSString *const UIDeviceBatteryStateDidChangeNotification   __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
UIKIT_EXTERN NSString *const UIDeviceBatteryLevelDidChangeNotification   __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
UIKIT_EXTERN NSString *const UIDeviceProximityStateDidChangeNotification __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); 

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

判定ios版本的方式

【環境:xCode4.2 , ios5.1 , USE ARC】

說明:因ios版本開發種類持續更新,若有特殊需求,要向下相容,可使用此方法

<以下範例>
 
#import "sys/utsname.h"//記得加入此行

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
// iOS 5 code
//代碼範例使用的是navigation中更改背景圖的方法,ios4以前使用的方法很多種,但蘋果希望目前使用這種方法,故以前的某些方法會被禁用
[self.navigationController.navigationBar setBackgroundImage:[UIImageimageNamed:@"top_bk.png"] forBarMetrics:UIBarMetricsDefault];
}
else {
// iOS 4.x code
}

As always , if you have any question , feel free to contact me.
有任何問題,請聯絡我

ios,判斷目前使用的device為ipad or iphone

【環境:xCode4.3.2 , ios5.0 , USE ARC】

以下是library中所註明的資訊,可在xcode中對使用的關鍵字userInterfaceIdiom按下command+滑鼠左鍵去檢視
typedef enum {
#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    UIUserInterfaceIdiomPhone,           // iPhone and iPod touch style UI
    UIUserInterfaceIdiomPad,             // iPad style UI
#endif
} UIUserInterfaceIdiom;

/* The UI_USER_INTERFACE_IDIOM() macro is provided for use when deploying to a version of the iOS less than 3.2. If the earliest version of iPhone/iOS that you will be deploying for is 3.2 or greater, you may use -[UIDevice userInterfaceIdiom] directly.
 */
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)


<以下使用範例>

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
UIViewController*  IPhoneViewController1=[[UIViewController  alloc]init];
self.window.rootViewController=  IPhoneViewController1;
}
else if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) 
{
UIViewController*  IPadViewController1=[[UIViewController  alloc]init];
self.window.rootViewController=  IPadViewController1;
} 

or if you have nib file, you can type
[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];

As always, if you have any question , feel free to contact me.
有任何問題,請聯絡我

2012年5月31日 星期四

網路檢測

20120613新增apple官網範例(使用方式略有差異)

【環境:xCode4.2 , ios5.1】
宣告:利用 UIDevice 的功能
說明:
Connectivity(.h .m)資料來源參考-掌握iPhone SDK 程式開發技巧
利用該原碼提供網路偵測的判斷,使用前須先挂載 SystemConfiguration.framework,挂載方式請參閱
引用方式[UIDevice cellularConnected][UIDevice wiFiConnected][UIDevice networkConnected]即可使用

功能執行:
1.利用淡入淡出視覺頁面


2.切換進入功能頁,點選按鈕可偵測網路....啓動語音或者視訊等相關功能



範例下載

<以下範例>
Connectivity.h
 
#import <SystemConfiguration/SCNetworkReachability.h>

//add SystemConfiguration Framework

@interface UIDevice (DeviceConnectivity)

+(BOOL)cellularConnected; 
+(BOOL)wiFiConnected;
+(BOOL)networkConnected;   

@end
Connectivity.m
#import "Connectivity.h"

#define EXTERNAL_HOST @"google.com"

@implementation UIDevice (DeviceConnectivity)

+(BOOL)cellularConnected{// EDGE or GPRS
  SCNetworkReachabilityFlags    flags = 0;
  SCNetworkReachabilityRef      netReachability = NULL;  
  netReachability     = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [EXTERNAL_HOST UTF8String]);
  if(netReachability){
    SCNetworkReachabilityGetFlags(netReachability, &flags);
    CFRelease(netReachability);
  }
  if(flags & kSCNetworkReachabilityFlagsIsWWAN){
    return YES;
  }
  return NO;
}
+(BOOL)wiFiConnected{
  if([self cellularConnected]){
    return NO;
  }
  return [self networkConnected];
}
+(BOOL)networkConnected{
  SCNetworkReachabilityFlags     flags = 0;
  SCNetworkReachabilityRef       netReachability = NULL;
  BOOL                           retrievedFlags = NO;
  
  netReachability     = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), [EXTERNAL_HOST UTF8String]);
  if(netReachability){
    retrievedFlags      = SCNetworkReachabilityGetFlags(netReachability, &flags);
    CFRelease(netReachability);
  }
  if (!retrievedFlags || !flags){
    return NO;
  }
  return YES;
}
@end