16.5 C
London
Wednesday, September 4, 2024

swift – iOS Motion Extension: Get textual content worth from the motion extension and present it on the Container appwithout the usage of App Group


`Presently, I’m able to navigate to the Container app(Viewcontroller) from motion extension utilizing url scheme. However couldn’t capable of go the textual content worth from the motion extension.

Within the ActionViewController, on the actionAppNavigation button motion i’m making an attempt to go the textual content worth to the Container app(ViewController) by means of Scenedelegate.`

import UIKit
import MobileCoreServices
import UniformTypeIdentifiers

class ActionViewController: UIViewController {

    @IBOutlet weak var myTextView: UITextView!
        var convertedString: String?
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()

        let textItem = self.extensionContext!.inputItems[0]
            as! NSExtensionItem

        let textItemProvider = textItem.attachments![0]

        if textItemProvider.hasItemConformingToTypeIdentifier(UTType.textual content.identifier) {
            textItemProvider.loadItem(
             forTypeIdentifier: UTType.textual content.identifier,
                      choices: nil,
             completionHandler: { (consequence, error) in
                 self.convertedString = consequence as? String

                             if self.convertedString != nil {
                                 self.convertedString = self.convertedString!.uppercased()

                                 DispatchQueue.principal.async {
                                     self.myTextView.textual content = self.convertedString!
                                     
                                 }
                             }
                        })
        }
      
    }

@IBAction func actionAppNavigation(_ sender: UIButton) {
if let textual content = self.myTextView.textual content,
           let escapedText = textual content.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {

            let urlString = "newshare://?textSent=(escapedText)"
            if let url = URL(string: urlString) {
                let nsurl = NSURL(string: urlString)
                let selector = sel_registerName("openURL:")
                    var responder = self as UIResponder?
                    whereas let r = responder, !r.responds(to: selector) {
                        responder = r.subsequent
                    }
                    _ = responder?.carry out(selector, with: nsurl)
            } else {
                print("Error: Unable to create URL from string")
            }

        } else {
            print("Error: Unable to get legitimate textual content from myTextView")
        }
       
    }
 @IBAction func performed() {
        let returnProvider =
                NSItemProvider(merchandise: convertedString as NSSecureCoding?,
                              typeIdentifier: UTType.textual content.identifier)

            let returnItem = NSExtensionItem()

            returnItem.attachments = [returnProvider]
                self.extensionContext!.completeRequest(
                    returningItems: [returnItem], completionHandler: nil)
    }

In Scene delegate,

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, choices connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
                let storyboard = UIStoryboard(identify: "Primary", bundle: nil)
                guard let rootVC = storyboard.instantiateViewController(identifier: "ViewController") as? ViewController else {
                    print("ViewController not discovered")
                    return
                }
                let rootNC = UINavigationController(rootViewController: rootVC)
                self.window?.rootViewController = rootNC
                self.window?.makeKeyAndVisible()
    }

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
           if let url = URLContexts.first?.url {
               print(url)
               let urlStr = url.absoluteString //1
               // Parse the customized URL as per your requirement.
               let part = urlStr.elements(separatedBy: "=") // 2
               if part.rely > 1, let appId = part.final { // 3
                   print(appId)
                   let topViewController = self.window?.rootViewController as? UINavigationController
                   let currentVC = topViewController?.topViewController as? ViewController
                   currentVC?.receivedLabel.textual content = "Utility Id : " + appId
               }
           }
       }
}

In Container app(ViewController),

import UIKit
import UniformTypeIdentifiers
class ViewController: UIViewController {

    @IBOutlet var receivedLabel: UILabel!

 override func viewDidLoad() {
        tremendous.viewDidLoad()
}
}


Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here