IOS sharing when sharing with iOS 11 Screenshot

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:

// App Group keys

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(){
    // Set up main view
    mainView.layer.cornerRadius = 8
    mainShadowView.addShadow()

    // Set up views and buttons
    // Code hidden, applies shadows etc.

    // Code hidden, moves constraints of a view
}

func addNotifications(){
    // Helps views tell their parent (this view controller) to navigate to another form
    NotificationCenter.default.addObserver(forName: NotificationDisplayFetchedLink, object: nil, queue: nil){ notification in
        // Handles user info in lambda block
        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(){
    // Make sure we have a valid extension item
    if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
        let contentType = kUTTypeImage as String

        // Verify the provider is valid
        if let contents = content.attachments as? [NSItemProvider] {
            // look for images
            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 {
            // Data is empty
            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.

+6
source share
2 answers

let url = data as! URL if let imageData = try? Data(contentsOf: url) {

, URL-. "public.image", UIImage Data.

+5

Objective C , . , .png, public.png public.jpeg

if ([itemProvider hasItemConformingToTypeIdentifier:@"public.png"]){
            [itemProvider loadItemForTypeIdentifier:@"public.png" options:nil completionHandler: ^(id<NSSecureCoding> item, NSError *error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSData *imgData;
                    if([(NSObject*)item isKindOfClass:[NSURL class]]) {
                        imgData = [NSData dataWithContentsOfURL:(NSURL*)item];
                    }
                    if([(NSObject*)item isKindOfClass:[UIImage class]]) {
                        imgData = UIImagePNGRepresentation((UIImage*)item);
                    }
 UIImage *image=[UIImage imageWithData:imgData];
});

            }];
0

All Articles