我的應用程序是我之前詢問過的。它是一個XML解析器,它顯示了來自XML的多個項目,等於該應用程序已打開的天數。我的問題主要是這樣的:我在1.0版本中沒有與iCloud建立任何關係。我很快就會發布iPad版本的應用程序,因爲它受歡迎,並且擔心那些在iPhone上使用該應用程序一段時間的應用程序現在將重置爲iPad版本的第1天。我在GitHub上發現了MKiCloudSync,並想知道這是否會很好實現,以便應用始終在所有設備上保持同步?如果確實如此,那麼在應用程序的1.1版本中添加它會自動與現有庫進行同步,還是必須先更新iPhone版本,讓它同步,然後再將它取得iPad版本?基本上只是希望它保持同步。NSUserDefaults和iCloud
這裏是我用來存儲NSUserDefault值的AppDelegate代碼。雖然,一些NSUserDefaults可以在其他類中更改。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(3);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults integerForKey:@"totalDays"]) {
// if there is no value for the key, set it to 1
[defaults setInteger:0 forKey:@"totalDays"];
}
if (![defaults objectForKey:@"currentDate"]) {
[defaults setObject:@"32 01" forKey:@"currentDate"];
}
if (! [defaults boolForKey:@"marked"]) {
[defaults setBool:NO forKey:@"marked"];
}
if (![defaults arrayForKey:@"checkedrows"]) {
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:[defaults arrayForKey:@"checkedrows"]];
}
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd MM"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
NSString *checkdate = [defaults objectForKey:@"currentDate"];
if (![dateString isEqualToString:checkdate]) {
NSInteger currentnumber = [defaults integerForKey:@"totalDays"];
[defaults setObject:dateString forKey:@"currentDate"];
[defaults setInteger:currentnumber+1 forKey:@"totalDays"];
}
[defaults synchronize];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"enteredfore");
NSDate *date = [NSDate date];
// format it
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd MM"];
// convert it to a string
NSString *dateString = [dateFormat stringFromDate:date];
// free up memory
[dateFormat release];
NSString *checkdate = [defaults objectForKey:@"currentDate"];
NSLog(@"hereitis%@", checkdate);
if (![dateString isEqualToString:checkdate]) {
NSInteger currentnumber = [defaults integerForKey:@"totalDays"];
NSLog(@"The current number is %i", currentnumber);
[[NSNotificationCenter defaultCenter] postNotificationName:@"EnteredForeground"
object:nil];
[defaults setObject:dateString forKey:@"currentDate"];
[defaults setInteger:currentnumber+1 forKey:@"totalDays"];
[defaults synchronize];
}
}
感謝
葉這就是我需要的詞:D衝突解決 - 你的應用程序iPhone在這種情況下可以重置! –
關於其他可行的類或者我可以用來處理衝突解決方法的任何建議? – user717452
這個怎麼樣...而不是調用MKiCloudSync時,應用程序變爲活動,我添加一個按鈕到UI調用同步,並有同步它?這樣,用戶可以選擇要同步的設備以及何時進行同步?它不會是一個完美的解決方案,因爲它不會每次都同步,但至少可以解決覆蓋已經運行了50天的新設備的問題,正如Daji-Djan所說。 或...每當應用程序準備關閉時詢問用戶是否要同步數據,都會彈出UIAlertView? – user717452