ionic project code
This commit is contained in:
24
ios/App/shareextension/Base.lproj/MainInterface.storyboard
Normal file
24
ios/App/shareextension/Base.lproj/MainInterface.storyboard
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="j1y-V4-xli">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--Share View Controller-->
|
||||
<scene sceneID="ceB-am-kn3">
|
||||
<objects>
|
||||
<viewController id="j1y-V4-xli" customClass="ShareViewController" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<view key="view" opaque="NO" contentMode="scaleToFill" id="wbc-yd-nQP">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
|
||||
<viewLayoutGuide key="safeArea" id="1Xd-am-t49"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="CEy-Cv-SGf" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
</scene>
|
||||
</scenes>
|
||||
</document>
|
||||
33
ios/App/shareextension/Info.plist
Normal file
33
ios/App/shareextension/Info.plist
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSExtension</key>
|
||||
<dict>
|
||||
<key>NSExtensionAttributes</key>
|
||||
<dict>
|
||||
<key>NSExtensionActivationRule</key>
|
||||
<string>SUBQUERY (
|
||||
extensionItems,
|
||||
$extensionItem,
|
||||
SUBQUERY (
|
||||
$extensionItem.attachments,
|
||||
$attachment,
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" ||
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.plain-text" ||
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png" ||
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg-2000" ||
|
||||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.compuserve.gif"
|
||||
).@count == $extensionItem.attachments.@count
|
||||
).@count == 1
|
||||
</string>
|
||||
</dict>
|
||||
<key>NSExtensionMainStoryboard</key>
|
||||
<string>MainInterface</string>
|
||||
<key>NSExtensionPointIdentifier</key>
|
||||
<string>com.apple.share-services</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
183
ios/App/shareextension/ShareViewController.swift
Normal file
183
ios/App/shareextension/ShareViewController.swift
Normal file
@@ -0,0 +1,183 @@
|
||||
import UIKit
|
||||
import Social
|
||||
import MobileCoreServices
|
||||
import Foundation.NSURLSession
|
||||
|
||||
class ShareItem {
|
||||
public var title: String?
|
||||
public var type: String?
|
||||
public var url: String?
|
||||
public var webPath: String?
|
||||
}
|
||||
|
||||
class ShareViewController: UIViewController {
|
||||
private var shareItems: [ShareItem] = []
|
||||
override public func viewDidAppear(_ animated: Bool) {
|
||||
super.viewDidAppear(animated)
|
||||
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
|
||||
}
|
||||
|
||||
override public func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
shareItems.removeAll()
|
||||
let extensionItem = extensionContext?.inputItems[0] as! NSExtensionItem
|
||||
Task {
|
||||
try await withThrowingTaskGroup(
|
||||
of: ShareItem.self,
|
||||
body: { taskGroup in
|
||||
for attachment in extensionItem.attachments! {
|
||||
if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
|
||||
taskGroup.addTask {
|
||||
return try await self.handleTypeUrl(attachment)
|
||||
}
|
||||
} else if attachment.hasItemConformingToTypeIdentifier(kUTTypeText as String) {
|
||||
taskGroup.addTask {
|
||||
return try await self.handleTypeText(attachment)
|
||||
}
|
||||
} else if attachment.hasItemConformingToTypeIdentifier(kUTTypeMovie as String) {
|
||||
taskGroup.addTask {
|
||||
return try await self.handleTypeMovie(attachment)
|
||||
}
|
||||
} else if attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
|
||||
taskGroup.addTask {
|
||||
return try await self.handleTypeImage(attachment)
|
||||
}
|
||||
}
|
||||
}
|
||||
for try await item in taskGroup {
|
||||
self.shareItems.append(item)
|
||||
}
|
||||
})
|
||||
self.sendData()
|
||||
}
|
||||
}
|
||||
|
||||
private func sendData() {
|
||||
let queryItems = shareItems.map {
|
||||
[
|
||||
URLQueryItem(
|
||||
name: "title",
|
||||
value: $0.title?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""),
|
||||
URLQueryItem(name: "description", value: ""),
|
||||
URLQueryItem(
|
||||
name: "type",
|
||||
value: $0.type?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""),
|
||||
URLQueryItem(
|
||||
name: "url",
|
||||
value: $0.url?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""),
|
||||
URLQueryItem(
|
||||
name: "webPath",
|
||||
value: $0.webPath?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? "")
|
||||
]
|
||||
}.flatMap({ $0 })
|
||||
var urlComps = URLComponents(string: "globaljain://;")!
|
||||
urlComps.queryItems = queryItems
|
||||
openURL(urlComps.url!)
|
||||
}
|
||||
|
||||
fileprivate func createSharedFileUrl(_ url: URL?) -> String {
|
||||
let fileManager = FileManager.default
|
||||
print("share url: " + url!.absoluteString)
|
||||
let copyFileUrl =
|
||||
fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.globaljain.social.shareextension")!
|
||||
.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! + url!
|
||||
.lastPathComponent.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
|
||||
try? Data(contentsOf: url!).write(to: URL(string: copyFileUrl)!)
|
||||
|
||||
return copyFileUrl
|
||||
}
|
||||
|
||||
func saveScreenshot(_ image: UIImage) -> String {
|
||||
let fileManager = FileManager.default
|
||||
|
||||
let copyFileUrl =
|
||||
fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.globaljain.social.shareextension")!
|
||||
.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
|
||||
+ "/screenshot.png"
|
||||
do {
|
||||
try image.pngData()?.write(to: URL(string: copyFileUrl)!)
|
||||
return copyFileUrl
|
||||
} catch {
|
||||
print(error.localizedDescription)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func handleTypeUrl(_ attachment: NSItemProvider)
|
||||
async throws -> ShareItem
|
||||
{
|
||||
let results = try await attachment.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil)
|
||||
let url = results as! URL?
|
||||
let shareItem: ShareItem = ShareItem()
|
||||
|
||||
if url!.isFileURL {
|
||||
shareItem.title = url!.lastPathComponent
|
||||
shareItem.type = "application/" + url!.pathExtension.lowercased()
|
||||
shareItem.url = createSharedFileUrl(url)
|
||||
shareItem.webPath = "capacitor://localhost/_capacitor_file_" + URL(string: shareItem.url ?? "")!.path
|
||||
} else {
|
||||
shareItem.title = url!.absoluteString
|
||||
shareItem.url = url!.absoluteString
|
||||
shareItem.webPath = url!.absoluteString
|
||||
shareItem.type = "text/plain"
|
||||
}
|
||||
return shareItem
|
||||
}
|
||||
|
||||
fileprivate func handleTypeText(_ attachment: NSItemProvider)
|
||||
async throws -> ShareItem
|
||||
{
|
||||
let results = try await attachment.loadItem(forTypeIdentifier: kUTTypeText as String, options: nil)
|
||||
let shareItem: ShareItem = ShareItem()
|
||||
let text = results as! String
|
||||
shareItem.title = text
|
||||
shareItem.type = "text/plain"
|
||||
return shareItem
|
||||
}
|
||||
|
||||
fileprivate func handleTypeMovie(_ attachment: NSItemProvider)
|
||||
async throws -> ShareItem
|
||||
{
|
||||
let results = try await attachment.loadItem(forTypeIdentifier: kUTTypeMovie as String, options: nil)
|
||||
let shareItem: ShareItem = ShareItem()
|
||||
let url = results as! URL?
|
||||
shareItem.title = url!.lastPathComponent
|
||||
shareItem.type = "video/" + url!.pathExtension.lowercased()
|
||||
shareItem.url = createSharedFileUrl(url)
|
||||
shareItem.webPath = "capacitor://localhost/_capacitor_file_" + URL(string: shareItem.url ?? "")!.path
|
||||
return shareItem
|
||||
}
|
||||
|
||||
fileprivate func handleTypeImage(_ attachment: NSItemProvider)
|
||||
async throws -> ShareItem
|
||||
{
|
||||
let data = try await attachment.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil)
|
||||
let shareItem: ShareItem = ShareItem()
|
||||
switch data {
|
||||
case let image as UIImage:
|
||||
shareItem.title = "screenshot"
|
||||
shareItem.type = "image/png"
|
||||
shareItem.url = self.saveScreenshot(image)
|
||||
shareItem.webPath = "capacitor://localhost/_capacitor_file_" + URL(string: shareItem.url ?? "")!.path
|
||||
case let url as URL:
|
||||
shareItem.title = url.lastPathComponent
|
||||
shareItem.type = "image/" + url.pathExtension.lowercased()
|
||||
shareItem.url = self.createSharedFileUrl(url)
|
||||
shareItem.webPath = "capacitor://localhost/_capacitor_file_" + URL(string: shareItem.url ?? "")!.path
|
||||
default:
|
||||
print("Unexpected image data:", type(of: data))
|
||||
}
|
||||
return shareItem
|
||||
}
|
||||
|
||||
@objc func openURL(_ url: URL) -> Bool {
|
||||
var responder: UIResponder? = self
|
||||
while responder != nil {
|
||||
if let application = responder as? UIApplication {
|
||||
return application.perform(#selector(openURL(_:)), with: url) != nil
|
||||
}
|
||||
responder = responder?.next
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
10
ios/App/shareextension/shareextension.entitlements
Normal file
10
ios/App/shareextension/shareextension.entitlements
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.application-groups</key>
|
||||
<array>
|
||||
<string>group.com.globaljain.social.shareextension</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
Reference in New Issue
Block a user