بکاپ گیری از دیتابیس با استفاده از لایبری SQLITE آقای کاشیزاده
با سلام و احترام نظر به اینکه مشکلی در عملکرد پیش نمیومد من این پست رو به موضوع جدید آبدیت کردم
با توجه به اینکه دیتابیس در فولدری داخل آیفون ایجاد میشه از چه طریقی میشه از دیتابیس مربوطه بکاپ گرفت و به همون طریق هم بازیابی کرد؟
حالا یا ایمیل کرد یا در icloud ذخیره و بازیابی کرد و یا به هر طریق دیگه که ممکنه
با تشکر
برای اینکه کاربر از طریق آیتونز مستقیما بتونه به فایل دیتابیس دسترسی پیدا کنه باید توی فایل Plist این عبارت رو اضافه کنید: UIFileSharingEnabled
بعد مقدارش هم بذارید روی YES
برای بکآپ توسط ایمیل:
class PoshtibanVC: UIViewController , MFMailComposeViewControllerDelegate {
internal var path:NSString!
@IBOutlet weak var emailButton: UIButton!
@IBAction func btnBackUpAction(sender: AnyObject) {
shareEmailWithPathsCollection(["\(path)/db.sqlite"])
}
override func viewDidLoad() {
super.viewDidLoad()
emailButton.layer.cornerRadius = 7
self.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IRYekan", size: 14)!], forState: .Normal)
path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
} // end of viewDidLoad
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func shareEmailWithPathsCollection(filePaths: [String]) {
if (MFMailComposeViewController.canSendMail() == true) {
let mailComposeViewController = MFMailComposeViewController();
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setSubject("موضوع ایمیل")
mailComposeViewController.setMessageBody("لطفا این فایل را حذف نکنید و در آرشیو ایمیل خود نگه دارید. این فایل بجز قابل استفاده بودن در برنامه شما. در سایت های مدیریت آنلاین دیتابیس نیز قابل مشاهده و دسترسی است.", isHTML: false)
for filePathToShare in filePaths {
let fileData = NSData(contentsOfFile: filePathToShare)
var fileMIMEType = "db/sqlite"
let filePathToShareURL = NSURL(string: filePathToShare)
if (filePathToShareURL?.URLByDeletingPathExtension?.lastPathComponent == "sqlite") {
fileMIMEType = "db/sqlite";
}
mailComposeViewController.addAttachmentData(fileData!, mimeType: fileMIMEType, fileName: (filePathToShareURL?.lastPathComponent)!)
}
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
print("Email Doesnt Setuped")
// User can't share the files since email account is not yet setup on the device.
// Show suitable error message
}
}
} // end of CLASS
برای اینکه کاربر از طریق آیتونز مستقیما بتونه به فایل دیتابیس دسترسی پیدا کنه باید توی فایل Plist این عبارت رو اضافه کنید: UIFileSharingEnabled
بعد مقدارش هم بذارید روی YES
برای بکآپ توسط ایمیل:
class PoshtibanVC: UIViewController , MFMailComposeViewControllerDelegate {
internal var path:NSString!
@IBOutlet weak var emailButton: UIButton!
@IBAction func btnBackUpAction(sender: AnyObject) {
shareEmailWithPathsCollection(["\(path)/db.sqlite"])
}
override func viewDidLoad() {
super.viewDidLoad()
emailButton.layer.cornerRadius = 7
self.navigationItem.leftBarButtonItem!.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IRYekan", size: 14)!], forState: .Normal)
path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
} // end of viewDidLoad
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func shareEmailWithPathsCollection(filePaths: [String]) {
if (MFMailComposeViewController.canSendMail() == true) {
let mailComposeViewController = MFMailComposeViewController();
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setSubject("موضوع ایمیل")
mailComposeViewController.setMessageBody("لطفا این فایل را حذف نکنید و در آرشیو ایمیل خود نگه دارید. این فایل بجز قابل استفاده بودن در برنامه شما. در سایت های مدیریت آنلاین دیتابیس نیز قابل مشاهده و دسترسی است.", isHTML: false)
for filePathToShare in filePaths {
let fileData = NSData(contentsOfFile: filePathToShare)
var fileMIMEType = "db/sqlite"
let filePathToShareURL = NSURL(string: filePathToShare)
if (filePathToShareURL?.URLByDeletingPathExtension?.lastPathComponent == "sqlite") {
fileMIMEType = "db/sqlite";
}
mailComposeViewController.addAttachmentData(fileData!, mimeType: fileMIMEType, fileName: (filePathToShareURL?.lastPathComponent)!)
}
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
print("Email Doesnt Setuped")
// User can't share the files since email account is not yet setup on the device.
// Show suitable error message
}
}
} // end of CLASS
با سلام و احترام
ميشه بفرماييد اين ارور چه دليلي ميتونه داشته باشه؟
mail sheet accounts for bundle ID: com.enaderlou.Acc, source account management: 1
2017-04-19 00:52:21.535413+0430 Acc[1718:574884] [MC] Result: YES
fatal error: unexpectedly found nil while unwrapping an Optional value
2017-04-19 00:52:21.560963+0430 Acc[1718:574884] fatal error: unexpectedly found nil while unwrapping an Optional value
ضمنا اين عبارت رو در
plist
پيدا نميكنم
UIFileSharingEnabled
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .