问题
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