简体   繁体   English

保存API Facebook数据以在不同的ViewController Swift iOS中使用

[英]Saving api Facebook data for use in different ViewController swift iOS

I'm hitting Facebook's graph to pull basic user info when the user logs in. My question is how do I use swift to save/pull that information in the best way so that it persists across the child viewcontrollers thereafter (basically everything after login). 我正在按Facebook的图表在用户登录时提取基本用户信息。我的问题是如何使用swift以最佳方式保存/拉取该信息,以便此后在子视图控制器中持久存在(基本上是登录后的所有信息) 。 For instance, I want to use the profile pic as a settings button throughout the app after the login screen (not in it) in my login view controller I have this relevant code: 例如,我想在我的登录视图控制器中的登录屏幕(不在其中)之后将个人资料图片用作整个应用程序中的设置按钮,我具有以下相关代码:

let userImageView: UIImageView = {
    let imageView = UIImageView()

    return imageView
}()

let nameLabel: UILabel = {
    let label = UILabel()

    return label
}()

and then later: 然后再:

func fetchProfile() {
    let parameters = ["fields": "email, first_name, last_name, picture.type(large)"]
    FBSDKGraphRequest(graphPath: "me", parameters: parameters).startWithCompletionHandler({ (connection, user, requestError) -> Void in

        if requestError != nil {
            print(requestError)
            return
        }

        var _ = user["email"] as? String
        let firstName = user["first_name"] as? String
        let lastName = user["last_name"] as? String

        self.nameLabel.text = "\(firstName!) \(lastName!)"

        var pictureUrl = ""

        if let picture = user["picture"] as? NSDictionary, data = picture["data"] as? NSDictionary, url = data["url"] as? String {
            pictureUrl = url

        }

        let url = NSURL(string: pictureUrl)
        NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
            if error != nil {
                print(error)
                return
            }

            let image = UIImage(data: data!)
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.userImageView.image = image
            })

        }).resume()

    })
}

What do I need to do to access this in my second ViewController? 要在第二个ViewController中访问它,我需要做什么? From what I can understand, segues only help if I have a physical attribute in the first viewController to push them from. 据我了解,只有当我在第一个viewController中有一个物理属性可以将其推送时,segue才有用。

Thanks 谢谢

The best way to save images will be with Documents Directory as Core Data is not optimized for files as large as images. 保存图像的最佳方法是使用Documents Directory,因为Core Data并未针对像图像一样大的文件进行优化。 You would want to save the photo in Documents Directory as so...... 您可能希望将照片保存为“文档目录”……

func saveImageDocumentDirectory(){
        let fileManager = NSFileManager.defaultManager()
        let paths = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString).stringByAppendingPathComponent(*** Name of DocDir Image***)
        let image = // *** Your Facebook Image ***
        print(paths)
        let imageData = UIImageJPEGRepresentation(image!, 0.5)
        fileManager.createFileAtPath(paths as String, contents: imageData, attributes: nil)
    }

Then in your viewcontroller(s) create an empty public image var fbImage:UIImage() then create a getImage function and code as follows..... 然后在您的viewcontroller中创建一个空的公共图像var fbImage:UIImage()然后创建一个getImage函数和代码,如下所示。

    func getImage()
{
            let fileManager = NSFileManager.defaultManager()
            let imagePAth = (self.getDirectoryPath() as NSString).stringByAppendingPathComponent(*** Name of Your DocDir Image ***)
            if fileManager.fileExistsAtPath(imagePath){
                self.fbImage.image = UIImage(contentsOfFile: imagePath)
            }else{
                print("No Image Saved")
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Swift IOS 的不同 ViewController 中的相同功能 - Same Function in different ViewController using Swift IOS 如何在iOS swift中将数据传递给以前的viewController? - how to pass data to previous viewController in iOS swift? iOS Swift:将数据从ViewController传递到UITableViewCell - iOS Swift: Pass data from ViewController to UITableViewCell 在Swift中从其他ViewController获取数据 - Getting data from a different ViewController in Swift Swift iOS-哪个viewController生命周期事件用于在视图更改后用于将数据发送到Firebase - Swift iOS -Which viewController lifecycle event to use to send data to Firebase after a view changes 如何在一个viewController上使用多个搜索栏以在Swift中访问不同的JSON数据? - How to use multiple search bars on one viewController to access different JSON data in Swift? 在iOS 8的Swift中重用ViewController - Reuse a ViewController in Swift on iOS 8 为什么在 iOS 的 ViewController 中声明视图时在 Swift 中使用“weak”关键字 - Why use "weak" keyword in Swift when declaring Views in a ViewController for iOS 获取Facebook API个人资料图片ios Swift - Get Facebook API Profile Picture ios Swift Facebook好友列出了快速ios的api - Facebook friends list api for swift ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM