用swift playground写个ios应用和大模型或者网站交互

Swift 复制代码
import SwiftUI


struct ContentView: View {

    @State private var textFieldText: String = ""

    @State private var outputText: String = "输出将会显示在这里"

    private let tip:String = "消息已发送,请等待"

    @State private var history:[String] = []

    var body: some View {

        ZStack {

            // 背景图片

            Image("图像资源") // 替换为你的背景图片名称

                .resizable()

                .scaledToFill()

                .edgesIgnoringSafeArea(.all) // 使背景图片覆盖整个屏幕

            

            VStack { 

                

                ScrollView { // 使用ScrollView使内容可滚动

                    Text(outputText)

                        .padding()

                        .background(Color.white.opacity(0.7))

                        .cornerRadius(8)

                        .font(.system(size: 30))

                        .border(Color.gray, width: 1)

                        .frame(width: 700) // 设置固定大小

                }

                .frame(width: 700, height: 200)

                

                  

                        

                

                

                HStack {

                    TextField("在这里输入...", text: $textFieldText)

                        .padding()

                        .textFieldStyle(RoundedBorderTextFieldStyle())

                        .autocapitalization(.none)

                        .disableAutocorrection(true)

                        .frame(width: 700)

                    

                    

                    Button(action: {

                        let sendmessage=textFieldText

                        textFieldText=""

                        outputText=tip

                        Task {

                            

                            if let result = await send_to_llm(message: sendmessage) {

                                history.append(result)

                                

                                outputText = history.joined(separator: "\n-------------------------------------------\n")

                                

                            } else {

                                outputText = "请求失败"

                            }

                        }

                    }) {

                        Text("提交")

                            .padding([.leading, .trailing], 10) // 调整左右内边距

                            .padding([.top, .bottom], 5) // 调整上下内边距

                            .background(Color.blue)

                            .foregroundColor(.white)

                            .cornerRadius(8)

                            

                    }

                    

                    .buttonStyle(BorderlessButtonStyle()) // 移除默认按钮样式

                }

                

                

            }  .padding(.top,450)

        }

      

        

        .foregroundColor(.black) // 设置文本颜色为黑色以确保在背景图片上清晰可见

    }

    

    

    

}

Swift 复制代码
import Foundation


// 定义请求的目标主机和路径

let host = 

let path = 


public func send_to_llm(message: String) async -> String? {

    // 定义请求头

    let headers: [String: String] = [

        

    ]

    

    // 定义请求体

    let body: [String: Any] = [

   

        ],

        "message": message

    ]

    

    // 将请求体转换为 JSON 数据

    guard let jsonData = try? JSONSerialization.data(withJSONObject: body, options: []) else {

        return ("Failed to serialize request body")

        

    }

    

    // 创建 URL

    guard let url = URL(string: "https://\(host)\(path)") else {

        return  ("Invalid URL")

         

    }

    

    // 创建 URLRequest

    var request = URLRequest(url: url)

    request.httpMethod = "POST"

    request.allHTTPHeaderFields = headers

    request.httpBody = jsonData

    

    do {

        // 使用 await 发送请求并获取数据

        let (data, _) = try await URLSession.shared.data(for: request)

        

        if let responseBody = String(data: data, encoding: .utf8) {

            print("Response Body: \(responseBody)")

            

            // 将String转换回Data,以便可以使用JSONSerialization

            if let jsonData = responseBody.data(using: .utf8),

               let jsonDict = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any],

               let messageDict = jsonDict["message"] as? [String: Any],

               let context = messageDict["content"] as? String {

                return context

            } else {

                return("Failed to parse JSON or key not found")

                

            }

        } else {

            return ("No response data")

             

        }

    } catch {

        return   ("Error: \(error)")

         

    }

}

Swift 复制代码
import SwiftUI


@main

struct MyApp: App {

    var body: some Scene {

        WindowGroup {

            ContentView()

        }

    }

}

这里可以设置打开控制台

测试接口可以在电脑端用foxapi,写个swift文件测

用swift playground 写一个和大模型或者任何网页端对话的应用

相关推荐
懋学的前端攻城狮4 小时前
超越Toast:构建优雅的UI反馈与异步协调机制
ios·性能优化
00后程序员张4 小时前
完整教程:如何将iOS应用程序提交到App Store审核和上架
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张4 小时前
iOS应用性能优化全解析:卡顿、耗电、启动与瘦身
android·ios·性能优化·小程序·uni-app·iphone·webview
Evavava啊8 小时前
iOS微信小程序WebView中按钮背景渐变显示问题解决方案
ios·微信小程序·h5·渲染
Evavava啊10 小时前
微信小程序H5页面iOS视频播放问题解决方案
ios·微信小程序·音视频·h5·http 响应头
90后的晨仔21 小时前
第5章:基础状态管理
ios
90后的晨仔21 小时前
第4章:基础布局系统
ios
90后的晨仔1 天前
第3章:基础视图组件
ios
Digitally1 天前
iTunes 无法连接到此 iPhone - 9 种解决方法
ios·iphone
2501_916007471 天前
iOS逆向工程:详细解析ptrace反调试机制的破解方法与实战步骤
android·macos·ios·小程序·uni-app·cocoa·iphone