2014-02-21 183 views
0

運行視圖控制器代碼之前,我有這個代碼的和平得到設備ID:獲取設備ID在IOS

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
    NSString *devicetokenRN = [NSString stringWithFormat:@"%@", deviceToken]; 
    devicetokenRN = [devicetokenRN stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    devicetokenRN = [devicetokenRN stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
    devicetokenRN = [devicetokenRN stringByReplacingOccurrencesOfString:@">" withString:@""]; 


    // send device token for APNS without spaces or <> 
    DeviceID *DeviceIdentifier = [[DeviceID alloc ] init]; 
    // sending it to the Device.h 
    [DeviceIdentifier setDeviceID:devicetokenRN]; 
} 

我需要檢索此的DeviceID作爲我的ViewController的第一件事。由於它運行的視圖控制器之前,因爲如果我在ViewController中使用的NSLog它是NULL,因爲它並沒有執行這個第一它沒有做這個功能。在運行UITableView之前,我需要先獲得一些像這樣的變量。任何想法將是非常感謝。

+0

的registerForRemoteNotificationTypes方法執行fromdidFinishLaunchingWithOptions,這是我可以,謝謝運行它最早的地方。 – user3328028

回答

0

在AppDelegate中獲得整個項目的任何地方,你可以保存在NSUserDefaults的的deviceToken值像

把這些線didRegisterForRemoteNotificationsWithDeviceToken方法在這樣

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 

{ 

    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"DeviceToken"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 

} 

ppdelegate.m也把下面這行代碼在你看來Controller.m或者在viewDidLoad方法

和檢索值。

[[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"]; 
0

您可以獲取和存儲您的DEVICEIDNSUserDefaults

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
    NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor]; 
    NSString *deviceId = [identifierForVendor UUIDString]; 
    NSUserDefaults *userRegistrarionDetails = [NSUserDefaults standardUserDefaults]; 
    [userRegistrarionDetails setObject:deviceId forKey:@"deviceID"]; 
    [userRegistrarionDetails synchronize]; 
} 

,您可以通過

NSUserDefaults *userRegistrarionDetails = [NSUserDefaults standardUserDefaults]; 
NSString * deviceId = [userRegistrarionDetails objectForKey:@"deviceID"]; 
NSLog(@"%@", deviceId); 
+0

這是否意味着其將在視圖控制器直接的方式嗎? – user3328028

+0

的identifierForVendor是不是你需要發送推送通知到手機的識別碼... –