IPhone設備信息獲取與網絡狀態監測

我們在開發過程中有時要獲取設備的一些基本信息,比如系統版本,設備唯一標識符等,但從iOS5之後設備的唯一標識udid不可以代碼獲取了,也就是這句代碼是廢的:[[UIDevice currentDevice] uniqueIdentifier],還有種唯一標識叫UUID,當用戶重裝軟件後,UUID的值就可能會發生改變(基本上可說是百分百會發生改變)。


對於網絡狀態的監聽,其實也很簡單,這裏我們需要藉助第三方類庫AFNetworking,加入其頭文件"AFHTTPSessionManager.h"即可。獲取網絡狀態現在貌似有兩種方法,蘋果的官方演示demo中的方法是使用Reachability,據說很流行,首先你需要下載並導入Reachability,把裏面的Reachability文件拷貝到自己的工程中,並導入SystemConfiguration.framework框架。

下載地址:https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

上述方法我不怎麼熟悉,本文詳細介紹另外一種,實用即可!
直接上代碼:
#import "ViewController.h"
#import "AFHTTPSessionManager.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // NSLog(@"uniqueIdentifier: %@", [[UIDevice currentDevice] uniqueIdentifier]);
    NSLog(@"name: %@", [[UIDevice currentDevice] name]);
    NSLog(@"systemName: %@", [[UIDevice currentDevice] systemName]);
    NSLog(@"systemVersion: %@", [[UIDevice currentDevice] systemVersion]);
    NSLog(@"model: %@", [[UIDevice currentDevice] model]);
    NSLog(@"localizedModel: %@", [[UIDevice currentDevice] localizedModel]);
   // NSLog(@"identifierForVendor:%@",[[UIDevice currentDevice] identifierForVendor]);
   // NSLog(@"%@",[self uuid]);
    _text.text=[self uuid];
    

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //創建網絡監聽管理者對象
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
      //設置監聽
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"未識別的網絡");
                break;
                
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"不可達的網絡(未連接)");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"2G,3G,4G...的網絡");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"wifi的網絡");
                break;
            default:
                break;
        }
    }];
    //開始監聽
    [manager startMonitoring];
}
-(NSString*) uuid {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    return result;
}


打印出來的結果:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章