Nskeyedarchiver doesn't work on ios8 but works on ios7 and simulator

So, I use NSKeyedArchiver

for wishlist and it works fine on iOS 7 and on simulator, but not on iOS 8 devices.

Here is the code I'm using:

wishlist.m

- (void)viewDidLoad
{
    NSString *voirexpo = @"viensdeexpo";
    [[NSUserDefaults standardUserDefaults] setObject:voirexpo forKey:@"voirexpo"];
    [[NSUserDefaults standardUserDefaults] synchronize];


    [super viewDidLoad];

    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    refait=0;
    refait2=0;
    wishlist * car2=[NSKeyedUnarchiver unarchiveObjectWithFile:@"myWishlist1.bin"];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSData * objectData = [defaults objectForKey:@"myWishlist1.bin"];
    if(objectData != nil)
    {
        wishlist * car3 = [NSKeyedUnarchiver
                               unarchiveObjectWithData:objectData];
        NSLog(@"YOOOOOOUUUUHOOOOO   %@", [car3 description]);

    }
    NSLog(@"YOOOOOOUUUUHOOOOO   %@", [car2 description]);


    rent=@"0";

    self.navigationItem.backBarButtonItem =
    [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                     style:UIBarButtonItemStyleBordered
                                    target:nil
                                    action:nil];

}

-(NSString*)pathForCacheFile:(NSString*)fileName
{

    NSArray*documentDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,    NSUserDomainMask, YES);
    NSString*path = nil;
    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];
    }
    return [NSString stringWithFormat:@"%@/%@", path, fileName];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if(_dataSource)
    {
        _dataSource=nil;
    }


    _dataSource=[NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForCacheFile:@"myWishlist1.bin"]];

    tableview.scrollEnabled=YES;

    [tableview reloadData];

} 

      

cardescription.m (view where I select a car to put in my wishlist)

-(NSString*)pathForCacheFile:(NSString*)fileName
{

    NSArray*    documentDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString*   path = nil;

    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];
    }

    return [NSString stringWithFormat:@"%@/%@", path, fileName];
}

static wishlist *mywishlist;
-(void)saveInWishlist
{
    if(mywishlist)
        mywishlist=nil;
    mywishlist=[wishlist new];

    mywishlist.exponom=wishexpo;
    mywishlist.idvoiture=TelephoneCellulaire;
    mywishlist.imvoiture=dataimage;
    mywishlist.objid=objectId;

    {
        NSMutableArray *temp=[NSKeyedUnarchiver unarchiveObjectWithFile:[self pathForCacheFile:@"myWishlist1.bin"]];


        //        mywishlist.imageSrc=filePath;

        if(temp.count==0 || temp==nil)
        {
            NSMutableArray*newArr=[NSMutableArray array];

            [newArr addObject:mywishlist];

            [NSKeyedArchiver archiveRootObject:newArr toFile:[self pathForCacheFile:@"myWishlist1.bin"]];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            // userObject is the object we want to save...
            [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:newArr] forKey:@"myWishlist1.bin"];
            [defaults synchronize];

        }
        else
        {
            [temp addObject:mywishlist];
            [NSKeyedArchiver archiveRootObject:temp toFile:[self pathForCacheFile:@"myWishlist1.bin"]];
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            // userObject is the object we want to save...
            [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:temp] forKey:@"myWishlist1.bin"];
            [defaults synchronize];
        }
    }
}

- (IBAction)BACKO:(id)sender {

    [self saveInWishlist];

    NSLog(@"ICIII %@",wishexpo);

    wishlist * car2=[[wishlist alloc]init];
    car2.exponom=wishexpo;
    car2.idvoiture=TelephoneFix;
    car2.imvoiture=dataimage;
    car2.objid=objectId;
    [NSKeyedArchiver archiveRootObject:car2 toFile:@"
"];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wishlist"
                                                    message:@"This car has been successfully added to your wishlist"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

}

-(NSString*)pathForCacheFile1:(NSString*)fileName
{

    NSArray*documentDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString*path = nil;
    if (documentDir) 
    {
        path = [documentDir objectAtIndex:0];
    }
    return [NSString stringWithFormat:@"%@/%@", path, fileName];
}

      

+3


source to share


1 answer


I have the same error when using NSKeyedArchiver

and only with iOS8. In my case, I was doing this:

[defaultManager changeCurrentDirectoryPath:customDirectory];
[NSKeyedArchiver archiveRootObject:blankDictionary toFile:filename];

      

But it worked when I did this:



NSString fullPath = [NSString stringWithFormat:@"%@/%@", customDirectory, filename];
[NSKeyedArchiver archiveRootObject:blankDictionary toFile:fullPath];

      

I am guessing the error is in NSKeyedArchiver

and relative paths are not working. If there are relative paths in the search path, try taking them out.

+1


source







All Articles