IOS free版向正式版传递数据的方法

第一步:在setting里面点击某行调用下面的方法

#ifdef CONTRACTIONM_FREE_2_0_
    else if (indexPath.section == 2) {
        //查询contaactions数据,并把正在进行的数据保存进数据库
        [self fetchedResultsController];
        self.contractionsArray = [[NSMutableArray alloc]initWithArray:[fetchedResultsController fetchedObjects]];
        NSError *error = nil;
        if (![self.managedObjectContext save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
        //获取document目录
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        
        //创建需要保存的数据
        NSData *fileData = [NSData dataWithContentsOfMappedFile:[NSString stringWithFormat:@"%@/ContractionM.sqlite",documentsPath]];
        ContractionMAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
        NSData *imageData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.jpg",documentsPath,appDelegate.contacts.Photo]];
        
        //把需要保存的数据放入到数组中
        NSArray *dataArray = [NSArray arrayWithObjects:fileData,imageData,appDelegate.contacts.Photo, nil];
        
        //归档
        NSData *data_dataArray = [NSKeyedArchiver archivedDataWithRootObject:dataArray];
        //加密
        NSString *encodeData = [GTMBase64 stringByWebSafeEncodingData:data_dataArray padded:YES];
        //拼接待打开的URL,通过此路径可以调用ContractionMAppDelegate文件中的application:handleOpenURL:方法
        //在此方法中进行数据的替换
        NSString *urlSring = [NSString stringWithFormat:@"contractionmonitor://localhost/importDatabase?%@",encodeData];
        NSURL *openURL = [NSURL URLWithString:urlSring];
        self.urlOpen = openURL;

        if ([[UIApplication sharedApplication] canOpenURL:self.urlOpen]) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Migrated Data to Full Version"
                                                                    message:@"All your current data in Full Version will be replaced by Free Version's, are you sure you want to proceed?" 
                                                                   delegate:self 
                                                          cancelButtonTitle:nil
                                                          otherButtonTitles:@"Cancel",@"Yes, please",nil];
                [alertView setTag:111];
                [alertView show];
                [alertView release];
                } 
        else {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"No Full Version was Found!" 
                                                                    message:@"Download the Full Version now and migrate data to it?" 
                                                                   delegate:self 
                                                          cancelButtonTitle:@"No, Later" 
                                                          otherButtonTitles:@"OK",nil];
                [alertView setTag:222];
                [alertView show];
                [alertView release]; 
                }
    }
#endif

第二步:在appDelegate里面创建application:handleOpenURL:,在其中对传递过来的数据库进行替换

//免费版数据替换完整版数据
#ifndef CONTRACTIONM_FREE_2_0_
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if([@"/importDatabase" isEqual:[url path]])  {
        if ((enterView != nil)||(enterView.view.superview != nil))
        {
            [enterView.view removeFromSuperview];
        }     
        
		
		NSString *query = [url query];
        NSData *importUrlData = [GTMBase64 webSafeDecodeString:query];
		
        // NOTE: In practice you will want to prompt the user to confirm before you overwrite their files!
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsPath = [paths objectAtIndex:0];
        //        [importUrlData writeToFile:[NSString stringWithFormat:@"%@/ContractionM.sqlite",documentsPath] atomically:YES];
        
        
        
        NSArray *dataArray = [NSKeyedUnarchiver unarchiveObjectWithData:importUrlData];
        [[dataArray objectAtIndex:0] writeToFile:[NSString stringWithFormat:@"%@/ContractionM.sqlite",documentsPath] atomically:YES];
        if ([dataArray count] == 3) {
            [[dataArray objectAtIndex:1] writeToFile:[NSString stringWithFormat:@"%@/%@.jpg",documentsPath,[dataArray objectAtIndex:2]] atomically:YES];
        }
        
        persistentStoreCoordinator_ = nil;
		managedObjectModel_ = nil;
		managedObjectContext_ = nil;
        
		//数据传入后重新设置settings的默认值
		NSFetchRequest * fetchSetting = [[NSFetchRequest alloc] init];
		NSEntityDescription * entritySetting = [NSEntityDescription entityForName:@"tb_Settings" 
														   inManagedObjectContext:self.managedObjectContext];
		[fetchSetting setEntity:entritySetting];
		NSError * error;
		NSArray * settingArray = [self.managedObjectContext executeFetchRequest:fetchSetting error:&error];
		self.settings = [settingArray objectAtIndex:0];
		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success!" 
													   message:@"Contraction Monitor Free's data has been transferred to Full Version successfully. "
													  delegate:self
											 cancelButtonTitle:@"OK"
											 otherButtonTitles:nil];
		[alert show];
		[alert release];
		//数据传入后刷新home页面
		NSFetchRequest * fetchContractions = [[NSFetchRequest alloc] init];
		NSEntityDescription * entrityContractions = [NSEntityDescription entityForName:@"tb_Contractions" 
																inManagedObjectContext:self.managedObjectContext];
		[fetchContractions setEntity:entrityContractions];
        //		NSArray * contractionsArray = [self.managedObjectContext executeFetchRequest:fetchContractions error:&error];
        //		if (contractionsArray.count>=1) {
        //			self.contractions = [contractionsArray objectAtIndex:contractionsArray.count-1];
        //			if (self.contractions.EndTime == nil) {
        homeViewController =(HomeViewController *)[self.navigationController.viewControllers objectAtIndex:0] ;
        [homeViewController viewWillAppear:YES];
        //}
		//}
		return YES;
    }
    return NO;
}
#endif


但是 对于 Ipad split  版本的数据导入, 需要单独调用各个页面的 数据刷新 方法, 放在 return 之前

// 刷新界面
        [self.masterController.profilesController reloadTableView];
        [self.masterController.reportsController reloadLogs];
        [self.detailController.logsController refreshTable];
//        [self.detailController.reportsController showInfo:nil];
        [self.detailController.recentsController refreshView];






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值