###実現したいこと
Shared Extensionを使ってSafariで開いている画像をアプリ内に保存したいと考えています。
###試したこと
こちらの記事を参考にShared Extensionの設定を行いました。
teratialの過去の質問に画像取得に関する質問があったので回答のコードを使わせていただいたのですが、画像の保存ができていない状況です。
didSelectPostメソッドの中のコードは以下の様に定義しています。
Swift
1 override func didSelectPost() { 2 print("postされたよ") 3 let inputItem: NSExtensionItem = self.extensionContext?.inputItems[0] as! NSExtensionItem 4 let itemProvider = inputItem.attachments![0] as! NSItemProvider 5 6 // shareExtension ではそのページの 画像 を取得 7 if itemProvider.hasItemConformingToTypeIdentifier("public.url") { 8 itemProvider.loadPreviewImage(options: nil, completionHandler: { (url, error) in 9 if let image = url as? UIImage { 10 //キーチェーンで紐付いている名前を記入 11 let defaults = UserDefaults(suiteName: "group.com.dbank.BLE.sampleShare") 12 defaults?.setValue(image.pngData(), forKey: "ddd") 13 let objects = defaults!.object(forKey: "ddd") 14 print(objects!) 15 } 16 self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil) 17 }) 18 } 19 }
デバッグしながら進めるとif itemProvider.hasItemConformingToTypeIdentifier("public.url") {
の行で弾かれてしまっている様です。
画像取得を試みているURLに変更してみたのですが結果は同じでした。
###ログ
出力されているログはこの様に表示されています。
log
12019-06-19 16:06:23.901153+0900 ShareExtension[4262:267428] [core] SLComposeServiceViewController got NSURL (null) for public.jpeg error: Error Domain=NSItemProviderErrorDomain Code=-1100 "Cannot load an object of class NSURL" UserInfo={NSLocalizedDescription=Cannot load an object of class NSURL} 22019-06-19 16:06:23.916201+0900 ShareExtension[4262:267362] [core] SLComposeServiceViewController got attachment coarseType 1 32019-06-19 16:06:23.993828+0900 ShareExtension[4262:267362] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 42019-06-19 16:06:23.994053+0900 ShareExtension[4262:267362] [MC] Reading from public effective user settings. 5postされたよ
あなたの回答
tips
プレビュー