// 检测 iOS 第三方浏览器
const isIOSThirdPartyBrowser = (): boolean => {
const ua = navigator.userAgent;
const isIOS = /iPad|iPhone|iPod/.test(ua);
const isThirdParty = /CriOS|FxiOS|OPiOS|Quark|QQBrowser|UCBrowser|MicroMessenger|Weibo|DingTalk|Alipay/i.test(ua);
return isIOS && isThirdParty;
};
// 下载函数
export const downloadImageFileFromUrl = async (url: string, filename?: string) => {
// iOS 第三方浏览器:打开新窗口,让用户长按保存
if (isIOSThirdPartyBrowser()) {
window.open(url, '_blank', 'noopener,noreferrer');
return;
}
// iOS Safari 和其他平台:使用标准 blob 下载
const response = await fetch(url);
const blob = await response.blob();
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = blobUrl;
link.download = filename || 'image.png';
link.click();
setTimeout(() => URL.revokeObjectURL(blobUrl), 100);
};