iOS利用剪切板在app中传递信息

时间:2024-04-18 01:58:09

利用iOS剪切板在app中传递信息

App1 中添加URLSchemes   app1

App2 中国添加URLSchemes   app2

App1中进入app2:

UIApplication.shared.open(URL(string: "App2://getData")!, options: [:], completionHandler: nil)

app2中在appadelegate中

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

if url.absoluteString.hasSuffix("getData"){

let pasteboard = UIPasteboard(name: .init("KmyPasteboard"), create: true)

pasteboard!.string = “需要传的数据信息”

}

if #available(iOS 10.0, *) {

UIApplication.shared.open(URL(string: "App1://getData")!, options: [:], completionHandler: nil)//返回app1

} else {

// Fallback on earlier versions

}

}

return true

}

返回进入app1后

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

if url.absoluteString.hasSuffix("getData") {

let pasteboard = UIPasteboard(name: .init("KmyPasteboard"), create: true)

let jsonStr = pasteboard!.string

pasteboard!.string = “”//清空剪切板

NotificationCenter.default.post(name: NSNotification.Name("Active"), object: jsonStr)

}

return true

}