利用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
}