欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

iOS16.1 实时活动 (Live Activity)&灵动岛适配-记录一个很严重的问题

最编程 2024-08-13 14:48:25
...

产品要求:将用户头像和一些数据显示在实时活动和灵动岛上,随即使用AppGroup保存头像,在创建实时活动时取出头像使用,但是发生了一个很严重的问题。

测试员使用四台手机 iPhone 12、iPhone X、iPhone XR、iPhone 11iOS版本全部为 16.1.1)进行了测试,iPhone 12、iPhone X成功的显示了实时活动,但是iPhone XR、iPhone 11却没有显示,在Log中发现实时活动虽然被创建了,但是立马被系统删除了。

经过一下午的实验,终于发现

// 主程序保存头像
NSURL *imageURL = [NSURL URLWithString:@"https://xxxxxxxxxxxx.png"];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
    if (finished && image) {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *pathURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.app.???.???"];
        pathURL = [pathURL URLByAppendingPathComponent:@"ActivityLoverAvatar.png"];
        NSData *imageData = UIImagePNGRepresentation(image);
        BOOL success = [imageData writeToURL:pathURL atomically:YES];
        ...
        // 创建实时活动
    } else {
        ...
        // 处理错误
    }
}];

// 实时活动获取对象头像
func getLoveryAvatatImage()->Image {
    let manager = FileManager.default
    let floderURL:URL = manager.containerURL(forSecurityApplicationGroupIdentifier: "group.app.???.???")!
    let fileURL:URL = floderURL.appendingPathComponent("ActivityLoverAvatar.png")
    do {
        let data: Data = try Data.init(contentsOf: fileURL)
        let image = UIImage.init(data: data)
        return Image(uiImage: image!)
    } catch let error{
        print(error.localizedDescription)
        return Image("placeholderImage")
    }
}

// 实时活动Widget配置
struct ActivityWidget: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: ActivityWidgetAttributes.self) { context in
            ActivityWidgetMainView(context: context) // 内部使用了 getLoveryAvatatImage()
        } dynamicIsland: { context in
            DynamicIsland {
                // Expanded UI goes here.  Compose the expanded UI through
                // various regions, like leading/trailing/center/bottom
                DynamicIslandExpandedRegion(.leading, priority: 1) {
                    ActivityWidgetMainDynamicIslandView(context: context) //内部使用了 getLoveryAvatatImage()
                        .dynamicIsland(verticalPlacement: .belowIfTooWide)
                        .padding(0)
                }
                
            } compactLeading: {
               getLoveryAvatatImage()
                    .resizable()
                    .frame(width: 22, height: 22)
                    .clipShape(Circle())
            } compactTrailing: {
                Image(getIconImageName(context.state.iconName))
                    .resizable()
                    .frame(width: 17, height: 17)
            } minimal: {
                getLoveryAvatatImage()
                    .resizable()
                    .frame(width: 22, height: 22)
                    .clipShape(Circle())
            }
        }
    }
}

代码中一共有4处使用了 getLoveryAvatatImage() 方法,如果删除minimalcompactLeading中的getLoveryAvatatImage(),不能正常显示的两个手机就可以正常显示了。所以,建议设计师将这两处换成了显示AppLogo图片了。

// 实时活动Widget配置
struct ActivityWidget: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: ActivityWidgetAttributes.self) { context in
            ActivityWidgetMainView(context: context) // 内部使用了 getLoveryAvatatImage()
        } dynamicIsland: { context in
            DynamicIsland {
                // Expanded UI goes here.  Compose the expanded UI through
                // various regions, like leading/trailing/center/bottom
                DynamicIslandExpandedRegion(.leading, priority: 1) {
                    ActivityWidgetMainDynamicIslandView(context: context) //内部使用了 getLoveryAvatatImage()
                        .dynamicIsland(verticalPlacement: .belowIfTooWide)
                        .padding(0)
                }
                
            } compactLeading: {
               Image("AppLogo")
                    .resizable()
                    .frame(width: 22, height: 22)
                    .clipShape(Circle())
            } compactTrailing: {
                Image(getIconImageName(context.state.iconName))
                    .resizable()
                    .frame(width: 17, height: 17)
            } minimal: {
                Image("AppLogo")
                    .resizable()
                    .frame(width: 22, height: 22)
                    .clipShape(Circle())
            }
        }
    }
}

但是这个问题的根本原因却不知道,因为有的手机是可以正常使用的,只有那两台测试机无法正常使用。如果有人知道为什么请私信我,大家一起讨论,谢谢!!!