如何从API响应中获取特定响应并将其与深链接Swift 5一起使用

发布于 2025-02-08 08:41:02 字数 1273 浏览 1 评论 0原文

我正在使用诸如此类的post方法

'成功{
收到API的回复 短链接= pahjt7;
状态= 200;
}'

我只想在我的URL上使用快捷链接来使用深层链接概念共享。
这是我的邮政方法守则,在其中我从API
中得到响应

    func postRequest(latitude:Double,longitude:Double) {
        guard let url = URL(string: "http://i.Mallangtech.com/api/Url") else{
            
            return
        }
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/Json", forHTTPHeaderField: "Content-Type")
        let body:[String: AnyHashable] = [
            "status": 200,
            "shortlink":"okko"]
        request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
        
        //hiting api
        
        let task = URLSession.shared.dataTask(with: request) { data, _, Error in
            guard let data = data, Error == nil else {
                return
            }

                do{
            
                let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            print("Success\(response)")
                
            }
            catch{
                print (error)
            }
            }
        task.resume()
  
    }

  

     ```

I'm Receiving Response from api using post method like this

'Success{
shortlink = pAHJt7;
status = 200;
} '

I want to use only shortlink with my url to share using deep linking concept.
Here is my code of post method in which I'm getting response from api

    func postRequest(latitude:Double,longitude:Double) {
        guard let url = URL(string: "http://i.Mallangtech.com/api/Url") else{
            
            return
        }
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/Json", forHTTPHeaderField: "Content-Type")
        let body:[String: AnyHashable] = [
            "status": 200,
            "shortlink":"okko"]
        request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .fragmentsAllowed)
        
        //hiting api
        
        let task = URLSession.shared.dataTask(with: request) { data, _, Error in
            guard let data = data, Error == nil else {
                return
            }

                do{
            
                let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            print("Success\(response)")
                
            }
            catch{
                print (error)
            }
            }
        task.resume()
  
    }

  

     ```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2025-02-15 08:41:02

让我们将此功能分为4个步骤。

  1. 从API中获得响应,
  2. 将深层链接与用户共享。
  3. 如果用户单击,请
  4. 用给定的URL打开屏幕加载屏幕。

您已经完成了步骤1。

对于步骤2,您可以使用uiactivityViewController。您可以参考执行。

对于步骤3和4,您需要为深链接配置并在SceneDelegate中处理它。请参阅 this ,您可以从启动视图控制器带有所需参数的scenelegate

Lets divide this feature into 4 steps.

  1. Getting Response from API
  2. Sharing the deep link to user.
  3. If user clicks, open the screen
  4. Load the screen with given url.

You have already done the step 1.

For step 2, You can use UIActivityViewController. You can refer this for its implementation.

For step 3 and 4, You need to configure for deep link and handle it inside SceneDelegate. Refer this and you can launch a view controller from SceneDelegate with required params.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文