简体   繁体   中英

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

So I'm using NSKeyedArchiver for a wishlist and it works perfectly on iOS 7 and on simulator, but not on devices with iOS 8.

Here 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 (the view where I choose the car to put in the 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];
}

I got the same error when using NSKeyedArchiver and only since 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];

My guess is that there's a bug in NSKeyedArchiver and relative pathnames don't work. If there are relative pathnames in your search paths, try taking them out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM