gofunbox commented on issue #934:
URL: 
https://github.com/apache/cordova-plugin-camera/issues/934#issuecomment-4140318537

   > How can this problem be solved?
   
   I used AI to solve issues in the JavaScript file replacement script he wrote
   
   `/**
    * [iOS 26 兼容修复] Cordova Hook 脚本
    * 
    * 修复 AssetsLibrary 在 iOS 26 中被废弃的问题
    * 将 ALAssetsLibrary 替换为 PHPhotoLibrary
    * 
    * Hook 类型: after_platform_add, after_prepare
    */
   
   const fs = require('fs');
   const path = require('path');
   
   module.exports = function(context) {
       if (!context.opts.platforms.includes('ios')) {
           console.log('[iOS26 Fix] 跳过非 iOS 平台');
           return;
       }
   
       console.log('[iOS26 Fix] 开始修复 AssetsLibrary 兼容性问题...');
   
       const projectRoot = context.opts.projectRoot;
       const cordovaLibPath = path.join(projectRoot, 
'platforms/ios/CordovaLib/Classes/Public/CDVURLProtocol.m');
   
       if (!fs.existsSync(cordovaLibPath)) {
           console.log('[iOS26 Fix] CDVURLProtocol.m 文件不存在,跳过');
           return;
       }
   
       let content = fs.readFileSync(cordovaLibPath, 'utf8');
   
       // 检查是否已经修复过
       if (content.includes('#import <Photos/Photos.h>')) {
           console.log('[iOS26 Fix] CDVURLProtocol.m 已修复,跳过');
           return;
       }
   
       // 替换 imports
       const oldImports = `#import <AssetsLibrary/ALAsset.h>
   #import <AssetsLibrary/ALAssetRepresentation.h>
   #import <AssetsLibrary/ALAssetsLibrary.h>
   #import <MobileCoreServices/MobileCoreServices.h>
   #import "CDVURLProtocol.h"`;
   
       const newImports = `// [iOS 26 兼容修复] 替换废弃的 AssetsLibrary 为 Photos 框架
   // #import <AssetsLibrary/ALAsset.h>
   // #import <AssetsLibrary/ALAssetRepresentation.h>
   // #import <AssetsLibrary/ALAssetsLibrary.h>
   #import <Photos/Photos.h>
   #import <MobileCoreServices/MobileCoreServices.h>
   #import "CDVURLProtocol.h"`;
   
       content = content.replace(oldImports, newImports);
   
       // 替换 startLoading 方法
       const oldStartLoading = `- (void)startLoading
   {
       // NSLog(@"%@ received %@ - start", self, NSStringFromSelector(_cmd));
       NSURL* url = [[self request] URL];
   
       if ([[url absoluteString] hasPrefix:kCDVAssetsLibraryPrefixes]) {
           ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset* 
asset) {
               if (asset) {
                   // We have the asset!  Get the data and send it along.
                   ALAssetRepresentation* assetRepresentation = [asset 
defaultRepresentation];
                   NSString* MIMEType = (__bridge_transfer 
NSString*)UTTypeCopyPreferredTagWithClass((__bridge 
CFStringRef)[assetRepresentation UTI], kUTTagClassMIMEType);
                   Byte* buffer = (Byte*)malloc((unsigned 
long)[assetRepresentation size]);
                   NSUInteger bufferSize = [assetRepresentation getBytes:buffer 
fromOffset:0.0 length:(NSUInteger)[assetRepresentation size] error:nil];
                   NSData* data = [NSData dataWithBytesNoCopy:buffer 
length:bufferSize freeWhenDone:YES];
                   [self sendResponseWithResponseCode:200 data:data 
mimeType:MIMEType];
               } else {
                   // Retrieving the asset failed for some reason.  Send an 
error.
                   [self sendResponseWithResponseCode:404 data:nil 
mimeType:nil];
               }
           };
           ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError* error) {
               // Retrieving the asset failed for some reason.  Send an error.
               [self sendResponseWithResponseCode:401 data:nil mimeType:nil];
           };
   
           ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
           [assetsLibrary assetForURL:url resultBlock:resultBlock 
failureBlock:failureBlock];
           return;
       }`;
   
       const newStartLoading = `// [iOS 26 兼容修复] 使用 PHPhotoLibrary 替代废弃的 
ALAssetsLibrary
   - (void)startLoading
   {
       // NSLog(@"%@ received %@ - start", self, NSStringFromSelector(_cmd));
       NSURL* url = [[self request] URL];
   
       if ([[url absoluteString] hasPrefix:kCDVAssetsLibraryPrefixes]) {
           // 使用 PHAsset 获取资源
           PHFetchResult<PHAsset *> *fetchResult = [PHAsset 
fetchAssetsWithALAssetURLs:@[url] options:nil];
           
           if (fetchResult.count > 0) {
               PHAsset *asset = fetchResult.firstObject;
               PHImageManager *imageManager = [PHImageManager defaultManager];
               PHImageRequestOptions *options = [[PHImageRequestOptions alloc] 
init];
               options.synchronous = NO;
               options.networkAccessAllowed = YES;
               options.deliveryMode = 
PHImageRequestOptionsDeliveryModeHighQualityFormat;
               
               [imageManager requestImageDataForAsset:asset options:options 
resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, 
UIImageOrientation orientation, NSDictionary * _Nullable info) {
                   if (imageData) {
                       NSString* MIMEType = (__bridge_transfer 
NSString*)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)dataUTI, 
kUTTagClassMIMEType);
                       [self sendResponseWithResponseCode:200 data:imageData 
mimeType:MIMEType];
                   } else {
                       [self sendResponseWithResponseCode:404 data:nil 
mimeType:nil];
                   }
               }];
           } else {
               [self sendResponseWithResponseCode:404 data:nil mimeType:nil];
           }
           return;
       }`;
   
       content = content.replace(oldStartLoading, newStartLoading);
   
       // 写入文件
       fs.writeFileSync(cordovaLibPath, content, 'utf8');
       console.log('[iOS26 Fix] CDVURLProtocol.m 修复完成');
   };
   `


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to