I am working on an iOS application that uses the photo sharing extension, where the image is processed and our main function is launched.
In Photo simulator, this works great. I decided to launch the device from the Photos application, and it also works great, but when I take a screenshot and try to share a new “quick screenshot” with iOS 11, the extension fails, any ideas?
The extension receives the image, sends it to the server, receives a response and displays this response (all in the extension). This annoys me, as the extended extensions of Messenger and Snapchat still work with a quick screenshot!
Xcode 9 also does not give me any logs from the shared extension. It’s also worth noting that I use a developer account that I need to “trust” every time I reinstall the application on the device.
code:
let suiteName = "group.suite.id"
override func viewDidLoad() {
print("Styling views..")
styleViews()
print("Styled views")
print("Adding notifications..")
addNotifications()
print("Added notifications")
print("Fetching image..")
fetchSharedImage()
}
func styleViews(){
mainView.layer.cornerRadius = 8
mainShadowView.addShadow()
}
func addNotifications(){
NotificationCenter.default.addObserver(forName: NotificationDisplayFetchedLink, object: nil, queue: nil){ notification in
guard let userInfo = notification.userInfo,
let link = userInfo["link"] as? String
else {
print("No userInfo found in notification")
return
}
self.displayResult(with: link)
}
}
func fetchSharedImage(){
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
let contentType = kUTTypeImage as String
if let contents = content.attachments as? [NSItemProvider] {
for attachment in contents {
if attachment.hasItemConformingToTypeIdentifier(contentType) {
attachment.loadItem(forTypeIdentifier: contentType, options: nil) { data, error in
let url = data as! URL
if let imageData = try? Data(contentsOf: url) {
self.selectedImage = UIImage(data: imageData)
DispatchQueue.main.async {
self.selectedImageView.layer.cornerRadius = 8
self.selectedImageView.image = self.selectedImage
}
self.makeWebRequest()
}
}
}
}
}
}
}
func makeWebRequest(){
let url = URL(string: "url.json")
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
guard let dict = json as? [String:Any] else { return }
let item = dict["item"]
guard let itemData = item as? [[String:Any]] else { return }
let link = itemData[0]["url"]
NotificationCenter.default.post(name: NotificationDisplayFetchedLink, object: nil, userInfo: [link: link!])
}
task.resume()
}
Edit:
So the solution (as Owen Zhao said) is that the iOS 11 screenshot editor returns UIImage, while applications like Photos will return the URL.
My solution for this elegant solution was to save the UIImage OR URL passed to UIImage to an iOS temporary directory (deleted after 3 days) and then returning the image URL in that directory to expand sharing.
source
share