来自共享或操作扩展ios的网络请求 [英] Network request from share or action extension ios

查看:113
本文介绍了来自共享或操作扩展ios的网络请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个标题,但没有找到任何适当的信息。我已经找到了将网络请求发送到包含应用程序的网络请求的方法。但是,我想直接回复扩展并在那里显示信息。我已经面临一些使用javascript访问网页的方法,但没有任何关于向后端发出请求的信息。是否有可能提出一些请求并获得JSON响应或在iOS中共享扩展(iPhone,如果有关系)?任何信息或指南将不胜感激!

I've searched around this title, but didn't find any appropriate information. I've found ways to make network request that sends it's result to containing app. However, I wanna get response directly back to extension and show information there. I've faced some ways to use javascript to access webpage, but there was nothing about making request to backend. Is it possible to make some request and get JSON response in action or share extension in iOS (iPhone, if matters)? Any info or guides will be appreciated!

推荐答案

您可以直接从扩展程序使用URLSession。 https://www.raywenderlich.com/158106/有一个很好的教程urlsession-tutorial-getting-started

You can use URLSession directly from the extension. There is a good tutorial at https://www.raywenderlich.com/158106/urlsession-tutorial-getting-started

let url = URL(...)
let dataTask = defaultSession.dataTask(with: url) { data, response, error in
  if let error = error {

    // Handle error (error.localizedDescription)
    // ...

  } else if let data = data,
    let response = response as? HTTPURLResponse,
    response.statusCode == 200 {

    // Handle resonse data
    // ...

    DispatchQueue.main.async {
      // Close extension
      // ...
    }
  }
}

在这种情况下,JavaScript无济于事。只需向您的网址发出请求并处理回复。

JavaScript won't help in this case. Just make a request to your url and handle the response.

这篇关于来自共享或操作扩展ios的网络请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆