Ios 正在下载用于在本地通知上显示的图像不工作

Ios 正在下载用于在本地通知上显示的图像不工作,ios,objective-c,cocoa-touch,unusernotificationcenter,Ios,Objective C,Cocoa Touch,Unusernotificationcenter,我正在开发一个框架,该框架具有从FCM接收消息并将其转换为本地通知的功能,在我收到的其中一个键中有一个指向图像的URL。因此,在苹果的文档中做了一些研究,发现图像应该存储在设备中(我使用的是模拟器),然后可以使用它,因此,我实现了一种下载随机图像(.png)的方法。该方法工作正常,我对其进行了测试,并且图像位于指定位置,问题是,当调用attachmentWithIdentifier:identifier URL:options:error:method时,从NSError*指针打印userInf

我正在开发一个框架,该框架具有从FCM接收消息并将其转换为本地通知的功能,在我收到的其中一个键中有一个指向图像的URL。因此,在苹果的文档中做了一些研究,发现图像应该存储在设备中(我使用的是模拟器),然后可以使用它,因此,我实现了一种下载随机图像(.png)的方法。该方法工作正常,我对其进行了测试,并且图像位于指定位置,问题是,当调用
attachmentWithIdentifier:identifier URL:options:error:
method时,从NSError*指针打印userInfo字典时,我得到了这个结果

{NSLocalizedDescription = "Invalid attachment file URL";}
我附上我的代码:

UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
if (settings.alertSetting == UNNotificationSettingEnabled)
{

    NSDictionary * remoteMessageData = [remoteMessage appData];

    NSString * imageHttpUrl = [remoteMessageData objectForKey:@"im"];

    NSURL * imageURL = [self getStorageFilePath:imageHttpUrl];

    [self downloadImageFromURL:imageHttpUrl withFullPath:imageURL.absoluteString withCompletitionHandler:^(NSHTTPURLResponse *httpResponse) {

        UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];

            content.title = [remoteMessageData objectForKey:@"ti"];
            content.body = [remoteMessageData objectForKey:@"bd"];

        if(httpResponse != nil)
        {
            if (httpResponse.statusCode == 200)
            {
                NSString * identifier = @"ImageIdentifier";
                NSArray<UNNotificationAttachment*> * attachments = [NSArray arrayWithObjects:[self getAttachments:imageURL withIdentifier:identifier], nil] ;
                content.attachments = attachments;
            }

        }

        content.userInfo = remoteMessageData;

        UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];

        //Create and register a request notification
        NSString * uuidString = [[NSUUID UUID] UUIDString];

        UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:uuidString content:content trigger:trigger];


        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            //Handle error
            if(error != nil)
            {
                NSLog(@"Dictionary: %@",[error userInfo]);
            }
        }];



- (NSURL *) getStorageFilePath : (NSString *)imageStringURL{

if(imageStringURL == nil)
{
    return nil;
}

NSURL * imageURL = [NSURL URLWithString:imageStringURL];

NSString * fileName = [imageURL lastPathComponent];

NSLog(@"fileName %@",fileName);

NSArray * systemPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"systemPaths %@",systemPaths);

NSString * tempDirectoryStringPath = [[[NSURL URLWithString:[systemPaths objectAtIndex:0]] absoluteString] stringByAppendingString:@"/"];

NSString * fullPath = [tempDirectoryStringPath stringByAppendingString:fileName];

NSLog(@"Full PATH: %@",fullPath);

return [NSURL URLWithString:fullPath];}

-(void) downloadImageFromURL : (NSString *)httpURL withFullPath:(NSString * )fullPath withCompletitionHandler:(void (^) (NSHTTPURLResponse * httpResponse) )taskResult{

if(httpURL == nil || fullPath == nil )
{
    taskResult(nil);
    return;
}

NSString *strImgURLAsString = httpURL;

strImgURLAsString = [strImgURLAsString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

NSURL * imgURL = [NSURL URLWithString:strImgURLAsString];

NSLog(@"Full PATH inside downloading: %@",fullPath);

NSURLSessionDataTask * downloadPhotoTask = [[NSURLSession sharedSession] dataTaskWithURL:imgURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;

    if (httpResponse.statusCode == 200)
    {
        [data writeToFile:fullPath atomically:YES];



        NSLog(@"Download run successfully");


    }else{
        NSLog(@"Download could not be completed");


    }

    taskResult(httpResponse);

} ];

[downloadPhotoTask resume];


  }

- (UNNotificationAttachment *) getAttachments: (NSURL *)attachmentURL withIdentifier : (NSString*)identifier
{

    NSError * error;

    UNNotificationAttachment * icon =  [UNNotificationAttachment attachmentWithIdentifier:identifier URL: attachmentURL options:nil error:&error];

    NSLog(@"Icon is : %@",[error userInfo]);

    UNNotificationAttachment* attachments = icon;

    return (attachments);
}
UNUserNotificationCenter*center=[UNUserNotificationCenter currentNotificationCenter];
if(settings.alertSetting==UNNotificationSettingEnabled)
{
NSDictionary*remoteMessageData=[remoteMessage appData];
NSString*imageHttpUrl=[remoteMessageData objectForKey:@“im”];
NSURL*imageURL=[self-getStorageFilePath:imageHttpUrl];
[自下载ImageFromUrl:imageHttpUrl with FullPath:imageURL.absoluteString with CompletionHandler:^(NSHTTPPURLResponse*httpResponse){
UNMutableNotificationContent*内容=[[UNMutableNotificationContent alloc]init];
content.title=[remoteMessageData objectForKey:@“ti”];
content.body=[remoteMessageData objectForKey:@“bd”];
if(httpResponse!=nil)
{
如果(httpResponse.statusCode==200)
{
NSString*标识符=@“ImageIdentifier”;
NSArray*attachments=[NSArray arrayWithObjects:[self-getAttachments:imageURL with identifier:identifier],nil];
content.attachments=附件;
}
}
content.userInfo=remoteMessageData;
UNTimeIntervalNotificationTrigger*trigger=[UntimeIntervalNotificationTriggerWithTimeInterval:10次重复:否];
//创建并注册请求通知
NSString*uuidString=[[NSUID UUID]uuidString];
UNNotificationRequest*request=[UnnotificationRequestRequestWithIdentifier:UUIString内容:内容触发器:触发器];
[center addNotificationRequest:request withCompletionHandler:^(N错误*_可为空错误){
//处理错误
如果(错误!=nil)
{
NSLog(@“Dictionary:%@,[error userInfo]);
}
}];
-(NSURL*)getStorageFilePath:(NSString*)imageStringURL{
if(imageStringURL==nil)
{
返回零;
}
NSURL*imageURL=[NSURL URLWithString:imageStringURL];
NSString*文件名=[imageURL lastPathComponent];
NSLog(@“fileName%@”,fileName);
NSArray*systemPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSLog(@“系统路径%@”,系统路径);
NSString*tempDirectoryStringPath=[[NSURL URLWithString:[systemPaths objectAtIndex:0]]absoluteString]stringByAppendingString:@/“];
NSString*fullPath=[tempDirectoryStringPath stringByAppendingString:fileName];
NSLog(@“完整路径:%@”,完整路径);
返回[nsurlUrlWithString:fullPath];}
-(void)下载ImagefromURL:(NSString*)带完整路径的httpURL:(NSString*)带完整路径处理程序的完整路径:(void(^)(NSHTTPURLResponse*httpResponse))任务结果{
if(httpURL==nil | | fullPath==nil)
{
任务结果(无);
返回;
}
NSString*strImgURLAsString=httpURL;
strImgURLAsString=[strImgURLAsString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet-URLQueryAllowedCharacterSet]];
NSURL*imgURL=[NSURL URLWithString:strImgURLAsString];
NSLog(@“下载内的完整路径:%@”,完整路径);
NSURLSessionDataTask*downloadPhotoTask=[[NSURLSession sharedSession]dataTaskWithURL:imgURL completionHandler:^(NSData*\u可为空数据,NSURLRResponse*\u可为空响应,NSError*\u可为空错误){
NSHTTPURLResponse*httpResponse=(NSHTTPURLResponse*)响应;
如果(httpResponse.statusCode==200)
{
[数据写入文件:完整路径原子:是];
NSLog(@“下载运行成功”);
}否则{
NSLog(@“下载无法完成”);
}
任务结果(httpResponse);
} ];
[下载照片任务恢复];
}
-(UNNotificationAttachment*)getAttachments:(NSURL*)attachmentURL with identifier:(NSString*)identifier
{
n错误*错误;
UNNotificationAttachment*图标=[UNNotificationAttachment attachmentWithIdentifier:identifier URL:attachmentURL选项:无错误:&错误];
NSLog(@“图标为:%@,[error userInfo]);
未通知附件*附件=图标;
报税表(附件);
}

我可以让它工作。我使用的是[NSURL URLWithString:fullPath]NSURL,但我应该使用[NSURL fileURLWithPath:fullPath],但在方法downloadImageFromURL中,继续使用字符串URL,而不使用fileURLWithPath:method格式(这会导致图像无法下载)