# Fiction **Repository Path**: leeos/fiction ## Basic Information - **Project Name**: Fiction - **Description**: 小说《校花的贴身高手》更新 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-05 - **Last Updated**: 2026-05-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # iOS SwiftUI + WKWebView 打包指南 ## 项目结构 ``` fiction/ ├── fiction/ │ ├── ContentView.swift # WKWebView 封装代码 │ ├── fictionApp.swift # App 入口 │ ├── index.html # 本地 HTML 文件 │ └── Assets.xcassets/ # 资源文件 ├── fiction.xcodeproj/ # Xcode 项目 └── Info.plist # 配置文件 ``` --- ## 1. Info.plist 网络权限配置 在 `fiction/Info.plist` 中添加以下配置: ```xml NSAppTransportSecurity NSAllowsArbitraryLoads UIStatusBarHidden UIViewControllerBasedStatusBarAppearance UISupportedInterfaceOrientations UIInterfaceOrientationPortrait ``` --- ## 2. HTML 文件添加步骤 ### 方式一:拖拽添加(推荐) 1. 打开 Xcode 项目 2. 在 Finder 中找到 `index.html` 文件 3. 拖拽到 Xcode 左侧的 `fiction` 文件夹下 4. 在弹出的对话框中勾选: - ✅ **Copy items if needed** - ✅ **Create groups** - ✅ 选择 Target: `fiction` 5. 点击 **Finish** ### 方式二:右键添加 1. 在 Xcode 中右键点击 `fiction` 文件夹 2. 选择 **Add Files to "fiction"...** 3. 选择 `index.html` 文件 4. 勾选 **Copy items if needed** 和 Target 5. 点击 **Add** ### 验证添加成功 在 Xcode 中选中 `index.html`,检查右侧 **Target Membership** 区域: - ✅ `fiction` 必须被勾选 --- ## 3. Xcode 配置步骤 ### 3.1 检查 Deployment Target 1. 点击项目根目录 `fiction` 2. 选择 Target `fiction` 3. 进入 **General** → **Deployment Info** 4. 设置 **iOS** 为 `13.0` 或更高 ### 3.2 配置签名(真机运行必需) 1. 进入 **Signing & Capabilities** 2. 勾选 **Automatically manage signing** 3. 选择你的 **Team**(Apple ID) 4. 修改 **Bundle Identifier** 为唯一标识(如 `com.yourname.fiction`) ### 3.3 添加 WebKit 框架(iOS 13.7 必需) 1. 进入 **Build Phases** → **Link Binary With Libraries** 2. 点击 **+** 号 3. 搜索 `WebKit.framework` 4. 点击 **Add** --- ## 4. 真机运行步骤 ### 4.1 连接设备 1. 使用 USB 线连接 iPhone 11 到 Mac 2. 在 iPhone 上点击 **信任此电脑** 3. 输入 iPhone 解锁密码 ### 4.2 选择运行目标 1. 在 Xcode 顶部工具栏,点击模拟器下拉菜单 2. 选择你的 iPhone 设备(如 "李志龙的 iPhone") ### 4.3 运行项目 1. 点击 **▶** 运行按钮 或按 `Cmd + R` 2. 首次运行需要在 iPhone 上: - 打开 **设置** → **通用** → **VPN与设备管理** - 找到你的 Apple ID,点击 **信任** --- ## 5. 常见问题排查 ### 问题1:白屏/无法加载 HTML **原因**:HTML 文件未正确添加到 Bundle **解决**: ```swift // 在 ContentView.swift 的 loadLocalHTML 方法中添加调试代码 let bundlePath = Bundle.main.bundlePath print("Bundle 路径: \(bundlePath)") // 列出 Bundle 中的所有文件 let fileManager = FileManager.default do { let files = try fileManager.contentsOfDirectory(atPath: bundlePath) print("Bundle 文件列表: \(files)") } catch { print("读取 Bundle 失败: \(error)") } ``` ### 问题2:网络请求失败(API 无法访问) **原因**:App Transport Security 阻止 HTTP 请求 **解决**:确保 Info.plist 中已添加: ```xml NSAppTransportSecurity NSAllowsArbitraryLoads ``` ### 问题3:WebView 显示安全区域空白 **原因**:未忽略安全区域 **解决**:代码中已添加: ```swift .edgesIgnoringSafeArea(.all) ``` ### 问题4:签名错误(真机运行) **错误信息**:`Failed to code sign` **解决**: 1. 检查 Apple ID 是否已登录 Xcode → Preferences → Accounts 2. 修改 Bundle Identifier 为唯一值 3. 确保 iPhone 已信任开发者证书 ### 问题5:iOS 13.7 兼容性问题 **解决**: 1. 确保 Deployment Target 设置为 13.0 2. 不要使用 iOS 14+ 的新 API 3. SwiftUI 在 iOS 13 中部分功能受限,但 WKWebView 完全支持 --- ## 6. 代码说明 ### ContentView.swift 核心功能 | 功能 | 实现方式 | |------|----------| | 加载本地 HTML | `loadFileURL(_:allowingReadAccessTo:)` | | 禁用弹性回弹 | `webView.scrollView.bounces = false` | | 全屏显示 | `.edgesIgnoringSafeArea(.all)` | | 加载状态监听 | `WKNavigationDelegate` | | 网络权限 | `NSAllowsArbitraryLoads` | ### 支持的 iOS 版本 - 最低支持:iOS 13.0 - 推荐版本:iOS 13.7+ - 测试设备:iPhone 11 (828x1792) --- ## 7. 验收检查清单 - [ ] ContentView.swift 代码已复制到项目 - [ ] index.html 已添加到 Bundle 并勾选 Target - [ ] Info.plist 已配置网络权限 - [ ] WebKit.framework 已添加到 Link Binary With Libraries - [ ] Deployment Target 设置为 13.0 - [ ] Bundle Identifier 已修改 - [ ] 签名 Team 已选择 - [ ] iPhone 已信任开发者证书 - [ ] App 在 iPhone 11 上正常显示 HTML - [ ] HTML 中的 JS 和网络请求无报错