Skip to content

chore: Update ShareViewController example to redirect to host #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions docs/docs/ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Please Change Share Extension Target deployment version to Same as Main Project



```javascript
```swift

import UIKit
import Social
Expand All @@ -174,13 +174,15 @@ class ShareViewController: SLComposeServiceViewController {
return true
}

override func viewDidLoad() {
super.viewDidLoad();
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
super.viewDidAppear(animated)
self.dismiss(animated: false, completion: {
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
})
}

override func viewDidLoad() {
super.viewDidLoad();
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
if let contents = content.attachments {
for (index, attachment) in (contents).enumerated() {
Expand All @@ -201,19 +203,18 @@ class ShareViewController: SLComposeServiceViewController {
}

override func didSelectPost() {
print("didSelectPost");
}

super.didSelectPost()
}
override func configurationItems() -> [Any]! {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return []
}

private func handleText (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {
attachment.loadItem(forTypeIdentifier: textContentType, options: nil) { [weak self] data, error in

if error == nil, let item = data as? String, let this = self {

this.sharedText.append(item)

// If this is the last item, save imagesData in userDefaults and redirect to host app
Expand All @@ -223,7 +224,7 @@ class ShareViewController: SLComposeServiceViewController {
userDefaults?.synchronize()
this.redirectToHostApp(type: .text)
}

} else {
self?.dismissWithError()
}
Expand All @@ -232,9 +233,9 @@ class ShareViewController: SLComposeServiceViewController {

private func handleUrl (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {
attachment.loadItem(forTypeIdentifier: urlContentType, options: nil) { [weak self] data, error in

if error == nil, let item = data as? URL, let this = self {

this.sharedText.append(item.absoluteString)

// If this is the last item, save imagesData in userDefaults and redirect to host app
Expand All @@ -244,7 +245,7 @@ class ShareViewController: SLComposeServiceViewController {
userDefaults?.synchronize()
this.redirectToHostApp(type: .text)
}

} else {
self?.dismissWithError()
}
Expand All @@ -253,9 +254,8 @@ class ShareViewController: SLComposeServiceViewController {

private func handleImages (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {
attachment.loadItem(forTypeIdentifier: imageContentType, options: nil) { [weak self] data, error in

if error == nil, let url = data as? URL, let this = self {
// this.redirectToHostApp(type: .media)
// Always copy
let fileExtension = this.getExtension(from: url, type: .video)
let newName = UUID().uuidString
Expand All @@ -274,7 +274,7 @@ class ShareViewController: SLComposeServiceViewController {
userDefaults?.synchronize()
this.redirectToHostApp(type: .media)
}

} else {
self?.dismissWithError()
}
Expand All @@ -283,17 +283,17 @@ class ShareViewController: SLComposeServiceViewController {

private func handleVideos (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {
attachment.loadItem(forTypeIdentifier: videoContentType, options:nil) { [weak self] data, error in

if error == nil, let url = data as? URL, let this = self {

// Always copy
let fileExtension = this.getExtension(from: url, type: .video)
let newName = UUID().uuidString
let newPath = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")!
.appendingPathComponent("\(newName).\(fileExtension)")
let copied = this.copyFile(at: url, to: newPath)
if(copied) {
if (copied) {
guard let sharedFile = this.getSharedMediaFile(forVideo: newPath) else {
return
}
Expand All @@ -307,7 +307,7 @@ class ShareViewController: SLComposeServiceViewController {
userDefaults?.synchronize()
this.redirectToHostApp(type: .media)
}

} else {
self?.dismissWithError()
}
Expand All @@ -316,9 +316,9 @@ class ShareViewController: SLComposeServiceViewController {

private func handleFiles (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {
attachment.loadItem(forTypeIdentifier: fileURLType, options: nil) { [weak self] data, error in

if error == nil, let url = data as? URL, let this = self {

// Always copy
let newName = this.getFileName(from :url)
let newPath = FileManager.default
Expand All @@ -335,7 +335,7 @@ class ShareViewController: SLComposeServiceViewController {
userDefaults?.synchronize()
this.redirectToHostApp(type: .file)
}

} else {
self?.dismissWithError()
}
Expand Down Expand Up @@ -366,7 +366,6 @@ class ShareViewController: SLComposeServiceViewController {
}
responder = responder!.next
}
extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}

enum RedirectType {
Expand Down Expand Up @@ -441,7 +440,7 @@ class ShareViewController: SLComposeServiceViewController {
}

return saved ? SharedMediaFile(path: forVideo.absoluteString, thumbnail: thumbnailPath.absoluteString, duration: duration, type: .video) : nil

}

private func getThumbnailPath(for url: URL) -> URL {
Expand Down