UIImagePickerController - Let user choose between camera and album? - swift

In my app I have the ability for the user to take a photo and post it to a web service. I use the following code:
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil)
However, I would prefer to give the user the option to either take a photo with their camera or pick a photo from the album. I know I can choose sourceType = .PhotoLibrary but that again rules out the camera as a possible source. Is there no combination here that can give me both?
Thanks!

You can use a UIAlertController to present an alert with style UIAlertControllerStyle.ActionSheet, giving those both as choices.
This is like how Apple does it when you choose to add a photo to a text message in the Messages app, and is used in other apps as well to provide this choice, so this is obviously the most official method available for doing this. But you won't find a way to just present a UIImagePickerController that lets you choose.

Related

Get date created of video from UIPickerViewController - Swift

If you present a UIImagePickerController with a source type of .camera or .photoLibrary (Swift), how can you get the date taken in UIImagePickerControllerDelegate.
You can use this (YPImagePicker) pod for get videos detail.

Error "[ShareSheet] connection invalidated" error iOS13+ but not on iOS 11.4

I want to share a file using the Share Sheet and have written code that seems to work just fine. However I keep seeing these error messages in the log (using Xcode 11.3)
[ShareSheet] connection invalidated
I have two physical devices I'm testing on; an iPad running iOS 13.1.2 and an iPhone 6 running 11.4. I don't see these messages on the iPhone with the older iOS on it. Both cases the sharing seems to work just fine. Here's the code I'm using using text instead of a file:
let activityViewController = UIActivityViewController(activityItems: ["simple text for test"], applicationActivities: nil)
activityViewController.excludedActivityTypes = [.message, .airDrop]
activityViewController.popoverPresentationController?.barButtonItem = myBarButtonItem
self.present(activityViewController, animated: true, completion: nil)
The message shows up when the share sheet goes away (either because user completes an action, or they tap outside of it to cancel).
Is it safe to ignore these messages? It's just odd that they didn't show up in the older OS but do in the new one.
Edited on 20 Mar 2020: I validated that I was providing a valid source or barButtonItem. I've changed the code to match that where I'm using a UIBarButtonItem and I still see the ShareSheet connection invalidated error.
I got
[ShareSheet] connection invalidated
in the Xcode output log on iOS 13.x, and the share sheet was squished and did not have any buttons in it.
To fix it, assign your sourceView to something more specific than self.view
In my case, I had some UILabels near the top of my view, so I set my sourceView to one of those. For example, in my parent view controller, I had a UILabel named labelCustomerName so I used that:
activityViewController.popoverPresentationController?.sourceView = self.labelCustomerName
For me, it was solved when i added this code, based on Apple developer documentation.
activityViewController.isModalInPresentation = true
By the way, it seems like even if press the close button for the activity it still shows that message.
try this ...
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentsPath.appendingPathComponent(filename)
let activityController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
present(activityController, animated: true, completion: nil)

Show custom alert for camera and gallery permission swift 3

May be this is duplicate question but I actually didn't find exact solution. I have camera and photo gallery collection view on the same screen.see this UI
When I land to this screen, I get two alerts: one for camera access and other for photo access. Can anyone tell me how to get rid of these native alerts and implement the custom one alert for both the permissions.
You can get the image when picking is done with imagePickerController
First, implement the UIImagePickerControllerDelegate
Second, create an instance and set the delegate
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
Present the imagePicker with
present(imagePicker, animated: true, completion: nil)
And finally
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imagePicker.dismiss(animated: true, completion: nil)
}
Now you have the image.
Whenever you will be add permission details inside your applications <Your App Name>.plist. App will automatically show default alert message from your .plist privacy permissions String displaying there because permission message always display in default alert view so you can do one thing before access camera show yours custom alert then redirect to camera/gallery view.
Before accessing camera you can show like this(Example contain only)

Check Photo gallery is accessible or not iphone

I am accessing the photo gallery using below code :
UIImagePickerController* picker = [UIImagePickerController new];
picker.sourceType = type;
picker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:type];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
So I user open Gallery for first time then application is display popup like this :
If user select the OK then its work fine. But if user select the Don't Allow then photo gallery is not able to access from application. My question is if user open Photo Gallery then how to check whether photo gallery is accessible or not.
If user is not allow then application is display the screen like this :
But is there any way to check Accessibility before opening the window.
Thanks,
You can check your access ability using ALAuthorizationStatusAuthorized.
Check authorization status of the app.
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//THen code here ,,,,
}
you might looking for this solution in failure block you can show alert to user that then can give access by go to settings....

What are the methods/classes I need to create an iPhone upload photo form?

Currently I have a view with an ImageViewer and 2 buttons (Browse & Upload). I'm currently searching for methods to display the iPhone photo album once the button is pressed.
I've looked at this example here but I assume this one is the method you call when you want to upload the photo into the server.
Objective C: How to upload image and text using HTTP POST?
I'd like to duplicate the functionality of a web upload form.
1. Click button to browse folders.
2. Choose photo and display in a container.
3. Click Upload to upload photo into the server.
**Update:
Found some nice tutorials that helped me start with my iPhone app photo upload form.
Using UIImagePickerController
http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html
http://trailsinthesand.com/picking-images-with-the-iphone-sdk-uiimagepickercontroller/
Adding images to iPhone Simulator
You will have to use UIImagePickerController class
Here is the class reference for UIImagePickerController
Initialize the picker controller in your button event handler method.
Then set the UIImagePickerControllerDelegate to your current viewController
Here is the class reference for UIImagePickerControllerDelegate
picker.delegate = self;
Set the UIImagePickerControllerSourceType of picker to UIImagePickerControllerSourceTypePhotoLibrary
picker.UIImagePickerControllerSourceType = UIImagePickerControllerSourceTypePhotoLibrary
Then present the pickerViewController (if your target device is an iPad, present the imagePickerView in a popOver control)
Once user selects an image picker controller will automatically call its delegate method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
You can get the selected image by accessing the value for UIImagePickerControllerOriginalImage from the NSDictionary *info
Then you can use the image for uploading to server as mentioned in the post link
These are some tutorials for UIImagePickerController
Tutorial one
Tutorial two
Tutorial three
for getting images from iPhone library you have to use UIImagePickerController.
m_imagePickerController = [[UIImagePickerController alloc] init];
m_imagePickerController.delegate = self;
m_imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
hope this will help you