I am new to ios developpement.
I have built and apps that saves data in the core data. What I would like to do is sharing this data with my Ipad, or my kids Iphone.
The devices are not on the same network or closed to each other, so the shared process will be via Email or Imessage.
the app will be installed on all devices to be abel to send/receive data.
I would like to be sure that the only way to do that is to use UIActivityViewController.
I didn't start coding part yet ( Sharing part), I am still doing some research and some good advices.
Thank you for helping
// Full code
after doing a lot of search here is my code , I don't know if there is a better way to do it but here is how I solved :
1 - creating a Json String
let savedData = ["Something": 1]
let jsonObject: [String: Any] = [
"type_id": 1,
"model_id": true,
"Ok": [
"startDate": "10-04-2015 12:45",
"endDate": "10-04-2015 16:00"
],
"custom": savedData
]
2 - Saving in as file
let objectsToShare = [jsonObject as AnyObject]
let data: Data? = try? JSONSerialization.data(withJSONObject: objectsToShare, options: .prettyPrinted)
let filePath = NSTemporaryDirectory() + "/" + NSUUID().uuidString + ".kis"
do
{
try data?.write(to: URL(fileURLWithPath: filePath))
}
catch {
}
print (filePath)
// create activity view controller
let activityItem:NSURL = NSURL(fileURLWithPath:filePath)
let activityViewController = UIActivityViewController(activityItems: [activityItem], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
3 - update info.plist
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>LSItemContentTypes</key>
<array>
<string>XXXSharingData.kis</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleTypeName</key>
<string>kis file</string>
<key>LSHandlerRank</key>
<string>Owner</string>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeIdentifier</key>
<string>XXXSharingData.kis</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.mime-type</key>
<string>application/pry</string>
<key>public.filename-extension</key>
<string>kis</string>
</dict>
</dict>
</array>
finally when sending/receiving file as attachment via email and opened in my app : open method appdelegate, I was able to see the Json string
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
do
{
let dictionary = try NSString(contentsOf: url, encoding: String.Encoding.utf8.rawValue)
print (dictionary)
}
catch {
}
return true
}