应用程序因从服务器iOS下载图像而被拒绝

时间:2013-11-04 07:31:22

标签: ios iphone ipad uitableview uiimage

我开发了应用程序,我从服务器下载图像并在UITableView

中显示

应用拒绝原因

  

特别是,我们发现在启动和/或内容下载时,您的应用存储了2.67 MB。要检查应用存储的数据量:

     

iOS数据存储指南指出,只有用户使用您的应用创建的内容(例如文档,新文件,编辑等)才能由iCloud备份。

     

您的应用使用的临时文件只应存储在/ tmp目录中;请记得在用户退出应用程序时删除存储在此位置的文件。

     

可以重新创建但必须保持应用程序正常运行的数据 - 或者因为客户希望它可供脱机使用 - 应标记为“不备份”属性。对于NSURL对象,请添加NSURLIsExcludedFromBackupKey属性以防止备份相应的文件。对于CFURLRef对象,请使用相应的kCFURLIsExcludedFromBackupKey属性。

这是我的代码显示我如何从服务器下载数据并显示它:

- (BOOL)fileExist:(NSString *)name  //Check's whether image Exists in Doc Dir.
{
    BOOL theSuccess;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    theSuccess = [fileManager fileExistsAtPath:fullPath];
    if(theSuccess){
        return YES;
    } else {
        return NO;
    }
}

- (void)downloadFile:(NSString *)urlFile withName:(NSString *)fileName //If image not exists it will download image.
{
    NSString *trimmedString = [urlFile stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([trimmedString length]>0)
    {
        HTTPEaterResponse *response = [HTTPEater get:[NSString stringWithFormat:@"%@",trimmedString]];

        if ([response isSuccessful])
        {
            [self saveImage:[[UIImage alloc] initWithData:[response body]] withName:fileName];
        }
    }

}

-(void)saveImage:(UIImage *)image withName:(NSString *)name //After downloading image it stores in Doc dir.
{
    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Documents/" stringByAppendingString:name]];
    [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
}

- (UIImage *)loadImage:(NSString *)name //Used for displaying. 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
    UIImage *img = [UIImage imageWithContentsOfFile:fullPath];

    return img;
}

显示数据的代码:

   - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
   {
       ...


    if ([dicImages valueForKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"merchantimage"]])
    {
        cell.MerchntLogo.image=[dicImages valueForKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"merchantimage"]];
    }
    else
    {
        if (!isDragging_msg && !isDecliring_msg)
        {
            if ([[[msg_array objectAtIndex:indexPath.row] valueForKey:@"merchantimage"] length]!=0)
            {
              [dicImages setObject:[UIImage imageNamed:@"rowDefault.png"] forKey:[[msg_array objectAtIndex:indexPath.row] valueForKey:@"merchantimage"]];
              [self performSelectorInBackground:@selector(downloadImage_3:) withObject:indexPath];
            }
        }
        else
        {
            cell.MerchntLogo.image=[UIImage imageNamed:@"rowDefault.png"];
        }
    }

...

}

-(void)downloadImage_3:(NSIndexPath *)path{

    if ([[[msg_array objectAtIndex:path.row] valueForKey:@"merchantimage"] length]!=0)
    {
        NSString *str=[[msg_array objectAtIndex:path.row] valueForKey:@"merchantimage"];

        UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];
            [dicImages setObject:img forKey:[[msg_array objectAtIndex:path.row] valueForKey:@"merchantimage"]];

        [tblProdDetail performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }

}

请帮我弄清楚我的应用被拒绝的原因,以及我可以采取哪些措施来解决问题。

3 个答案:

答案 0 :(得分:3)

您的应用显然违反了Apple的数据存储指南,该指南规定只有用户生成的数据应存储在Documents文件夹中。此数据会自动备份到iCloud,并且违反5GB上限。如果应用程序在此文件夹中存储了太多数据(如Apple所认为的那样),则可以从App Store拒绝该数据。

您的数据不会被归类为用户生成的内容,并且超过2 MB这是限制。

您可以通过此处引用来阻止备份数据。

https://developer.apple.com/library/ios/qa/qa1719/_index.html

   - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
    {
        const char* filePath = [[URL path] fileSystemRepresentation];

        const char* attrName = "com.apple.MobileBackup";
        u_int8_t attrValue = 1;

        BOOL result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
        return result;
    }

答案 1 :(得分:2)

你需要做他们所说的话。标记文件

只需添加didFinishLaunching

即可
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];

[self applyAttributes:documentsDirectory];

然后实现这个方法

#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{


    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
 }

 -(void)applyAttributes:(NSString *)folderPath
 {
     NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath error:nil];
     NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
     NSString *fileName;

     while (fileName = [filesEnumerator nextObject]) {
   // NSLog(@"apply to %@", [[NSURL fileURLWithPath:[folderPath stringByAppendingPathComponent:fileName]] path]);
    if([self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:[folderPath stringByAppendingPathComponent:fileName]]])
    {
        //NSLog(@"success applying");
    }

    //NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:fileName] error:nil];
    //fileSize += [fileDictionary fileSize];
  }

}



- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
   if([[NSFileManager defaultManager] fileExistsAtPath: [URL path]])
   {
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
   }

   return NO;

}

答案 2 :(得分:1)

使用此方法绕过在iCloud上存储数据

将文件路径传递给此方法

    - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
    {
        const char* filePath = [[URL path] fileSystemRepresentation];

        const char* attrName = "com.apple.MobileBackup";
        u_int8_t attrValue = 1;

        BOOL result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
        return result;
    }