Network request from share or action extension ios

若如初见. 提交于 2020-01-22 12:54:42

问题


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!


回答1:


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 won't help in this case. Just make a request to your url and handle the response.



来源:https://stackoverflow.com/questions/48925323/network-request-from-share-or-action-extension-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!