如何导航共享扩展以在Swift中托管应用程序?

时间:2019-07-15 16:17:51

标签: ios swift slcomposeviewcontroller

Swift获取URL后,如何导航Share Extension以在ShareExtension中托管应用?

import UIKit
import Social
import MobileCoreServices

class ShareViewController: UIViewController {

    let sharedKey = "shareappKey"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "Picked URL"        
        getURL()
    }

    @IBAction func nextAction(_ sender: Any) {

        self.redirectToHostApp()
    }

    @IBAction func cancelAction(_ sender: Any) {

        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    func redirectToHostApp() {

        let url = URL(string: "SelectedURL:\(sharedKey)")
        var responder = self as UIResponder?
        let selectorOpenURL = sel_registerName("openURL:")
        while (responder != nil) {
            if (responder?.responds(to: selectorOpenURL))! {
                let _ = responder?.perform(selectorOpenURL, with: url)
            }
            responder = responder!.next
        }
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    func getURL() {

        if let item = extensionContext?.inputItems.first as? NSExtensionItem {
            if let itemProvider = item.attachments?.first {
                if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
                    itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in

                        if let error = error {
                            print("error :-", error)
                        }
                        if (url as? NSURL) != nil {
                            // send url to server to share the link
                            do {
                                if (url as? URL) != nil {
                                    // do what you want to do with shareURL
                                    print("Selected URL :- ", url as Any)
                                    print(url as Any)

                                    let dict: [String : Any] = ["imgData" :  url as Any, "name" : "Added" as Any]
                                    print(dict)
                                    let userDefault = UserDefaults.init(suiteName: "group.com.abcd.shareapp")
                                    userDefault?.set(dict, forKey: self.sharedKey)
                                    userDefault?.synchronize()

                                   // Here I got Struct 
                                }
                            }catch let err{
                                print(err)
                            }
                        }
                        self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
                    })
                }
            }
        }
    }    
}


// Host App
import UIKit

class ViewController: UIViewController {

    var urlString = String()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let userDefault = UserDefaults.standard
        userDefault.addSuite(named: "group.com.abcd.shareapp")

        if let dict = userDefault.value(forKey: "img") as? NSDictionary{

            let data = dict.value(forKey: "imgData") as! Data
            let str = dict.value(forKey: "name") as! String
            print("Data is :- ", data)
            print("Str is :- ", str)
            userDefault.removeObject(forKey: "img")
            userDefault.synchronize()

            // Here i need to get that URL from Share Extention
        }
    }
}
  

错误

     

2019-07-15 22:01:15.045361 + 0530 LearingAppShare [3183:73775] [用户   默认值]尝试设置非属性列表对象{       imgData =“ https://m.jagran.com/lite/cricket/headlines-sachin-tendulkar-son-arjun-tendulkar-picked-for-rs-5-lakh-for-t20-mumbai-league-19192257.html”;       名称=已添加; }作为键URLKey的NSUserDefaults / CFPreferences值2019-07-15 22:01:15.047424 + 0530 LearingAppShare [3183:73775]

     
     

由于未捕获的异常而终止应用程序   “ NSInvalidArgumentException”,原因:“试图插入非属性   列出对象{       imgData =“ https://m.jagran.com/lite/cricket/headlines-sachin-tendulkar-son-arjun-tendulkar-picked-for-rs-5-lakh-for-t20-mumbai-league-19192257.html”;       名称=已添加; }作为关键字URLKey'

1 个答案:

答案 0 :(得分:1)

我已经回答了自己的问题。

您可以将其存储在字典中。 但是 UserDefaults无法保存带有自定义数据类型(如图像或URL)的字典。 因此,您需要先将字典转换为数据,然后再保存为默认值

import UIKit
import Social
import MobileCoreServices

class ShareViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "Picked URL"        
        getURL()
    }

    @IBAction func nextAction(_ sender: Any) {

        self.redirectToHostApp()
    }

    @IBAction func cancelAction(_ sender: Any) {

        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    func redirectToHostApp() {

        let url = URL(string: "YourOwnURLscheme:\(sharedKey)")
        var responder = self as UIResponder?
        let selectorOpenURL = sel_registerName("openURL:")
        while (responder != nil) {
            if (responder?.responds(to: selectorOpenURL))! {
                let _ = responder?.perform(selectorOpenURL, with: url)
            }
            responder = responder!.next
        }
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    func getURL() {

        if let item = extensionContext?.inputItems.first as? NSExtensionItem {
            if let itemProvider = item.attachments?.first {
                if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
                    itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in
                        if let error = error {
                            print("error :-", error)
                        }
                        if (url as? NSURL) != nil {
                            // send url to server to share the link
                                                    do {
                            var urlData: Data!
                            if let url = url as? URL{
                                urlData = try Data(contentsOf: url)
                            }
                            let dict: [String : Any] = ["urlData" :  urlData as Any, "name" : self.contentText as Any]
                            print(dict)
                            let userDefault = UserDefaults(suiteName: "group.com.abcd.sharecontent1")
                            userDefault?.set(dict, forKey: "storedURLData")
                            userDefault?.synchronize()
                        }catch let err{
                            print(err)
                        }
                       }
                    })
                }
            }
        }
    }    
}


// Host App
import UIKit

class ViewController: UIViewController {

    @IBOutlet var imgView: UIImageView!
    @IBOutlet var lblText: UILabel!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let userDefault = UserDefaults(suiteName: "group.com.abcd.sharecontent1")

        if let dict = userDefault?.value(forKey: "storedURLData") as? NSDictionary{

            let data = dict.value(forKey: "urlData") as! Data
            let str = dict.value(forKey: "name") as! String
            print("Data is :- ", data)
            print("str is :- ", str)

            self.imgView.image = UIImage(data: data)
            self.lblText.text = str

            userDefault?.removeObject(forKey: "storedURLData")
            userDefault?.synchronize()
        }
    }
}