iOS Share Extension crashes when sharing from iOS 11 Screenshot iOS Share Extension crashes when sharing from iOS 11 Screenshot swift swift

iOS Share Extension crashes when sharing from iOS 11 Screenshot


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

The issue is because the data here is not a URL. It is a "public.image", try to convert to UIImage instead of Data.


In Objective C You can use following code for image that shared as screen shot.Screen shot having .png extension so use public.png instead of 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];});            }];