jueves, 8 de junio de 2017

iOS: Select picture from camera or library

MyViewController

class MyViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    ...
 
    @IBOutlet weak var pictureImage: UIImageView!
 
    let picker = UIImagePickerController()
 
    ...
 
    override func viewDidLoad() {
        super.viewDidLoad()
     
        picker.delegate = self
     
        ...
    }

    ...

    // MARK: - Actions     @IBAction func changePictureAction(_ sender: Any) {
        ...
        changePhoto()
    }

    ...

    func changePhoto() {
     
        let optionMenu = UIAlertController(title: nil, message: "Lets get a picture", preferredStyle: .actionSheet)
     
        let selectFromLibraryAction = UIAlertAction(title: "Select photo from library", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            self.selectPictureFromLibrary()
        })
        optionMenu.addAction(selectFromLibraryAction)
     
        if (UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
            let takePictureAction = UIAlertAction(title: "Take a picture", style: .default, handler: {
                (alert: UIAlertAction!) -> Void in
                self.selectPictureFromCamera()
            })
            optionMenu.addAction(takePictureAction)
        }
     
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
            (alert: UIAlertAction!) -> Void in
            print("Cancelled")
        })
        optionMenu.addAction(cancelAction)
     
        self.present(optionMenu, animated: true, completion: nil)
    }
 
    func selectPictureFromLibrary() {
        picker.sourceType = .photoLibrary
        present(picker, animated: true, completion: nil)
    }
 
    func selectPictureFromCamera() {
        picker.sourceType = .camera
        present(picker, animated: true, completion: nil)
    }
 
...

// MARK: - UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        pictureImage.contentMode = .scaleAspectFit
        pictureImage.image = chosenImage
        dismiss(animated:true, completion: nil)
    }
 
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }    
}

Info.plist

Key: Privacy - Photo Library Usage Description
Type: String
Value: <you message>

Key: Privacy - Camera Usage Description
Type: String
Value: <you message>

iOS: WKWebKit not persisting cookies (login in not persisting when navigation out and into the web view)

I was having an issue that when loging in to a page through a WKWebKit the session didn't stick. Everytime I arrived at the page I was prompted to login.

We have to do the following.

We need to create a unique process pool to be shared among all WKWebKits and pass it along as a parameter to all of them (this wasn't necessary on UIWebView the way Apple handles the coookies changed):

var processPool: WKProcessPool?

somewhere where we create the WebViewController we pass said pool:

let webViewController = WebViewController()
webViewController.url = "some-url"
webViewController.processPool = processPool

then the web view controller that contains the WKWebKit we configure it like so:

class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
 
    var url: URL?
    var webView: WKWebView!
    var processPool: WKProcessPool?
 
    override func loadView() {
     
        let webConfiguration = WKWebViewConfiguration()
     
        if let pool = processPool {
            webConfiguration.processPool = pool
        }
     
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
             
        view = webView
    }
...

}