I am working on an iOS app. I want to selext some videos from iPhone library. I have done code as below but it also loads the photos from the photo library. My need is to load only videos on the device (not photos ONLY VIDEOS). So can anyone please help me what changes I need to for this goal? My code is as below:
UIAlertAction *Gallery = [UIAlertAction actionWithTitle:#"Add Existing Video" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[alertcontroller dismissViewControllerAnimated:YES completion:nil];
//VideoViewController *videoVC = [[VideoViewController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//videoVC.sourceType = picker.sourceType;
self.picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
NSArray *sourceTypes = [CustomCamera availableMediaTypesForSourceType:self.picker.sourceType];
//videoVC.pickerController = picker;
if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ])
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:#"No Video available" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
[self presentViewController:self.picker animated:YES completion:nil];
}
}];
Related
I have a button to share my app's link. I want a dialog box for it. I have searched a lot for it but haven't found a satisfactory solution. How can I share my application link to different social platforms, like Facebook, Twitter or Gmail?
I'm using this code:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *textToShare = #"Look at this awesome website for aspiring iOS Developers!";
NSURL *myWebsite = [NSURL URLWithString:#"My URL"];
NSArray *objectsToShare = #[textToShare, myWebsite];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
NSArray *excludeActivities = #[UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo];
activityVC.excludedActivityTypes = excludeActivities;
[self presentViewController:activityVC animated:YES completion:nil];
// Do any additional setup after loading the view.
}
You can create your Share Action Button directly from Interface Builder and ctrl drag it into your code.
Then you can do something like this :
- (IBAction)shareByFacebook:(id)sender {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self generateMessage:controller];
}else{
UIAlertView* facebookAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(#"Social.Account.FB.title", #"") message:NSLocalizedString(#"Social.Account.FB.message", #"") delegate:nil cancelButtonTitle:NSLocalizedString(#"Error.ok", #"") otherButtonTitles: nil];
[facebookAlert show];
}
}
This method share an image and a corresponding text message to Facebook.
- (IBAction)shareByTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[self generateMessage:tweetSheet];
}else{
UIAlertView* twitterAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(#"Social.Account.Twitter.title", #"") message:NSLocalizedString(#"Social.Account.Twitter.message", #"") delegate:nil cancelButtonTitle:NSLocalizedString(#"Error.ok", #"") otherButtonTitles: nil];
[twitterAlert show];
}
}
Same for Twitter.
Don't forget to import #import <Social/Social.h>
I have created a generic generateMessage method in order to avoid code repetition.
-(void)generateMessage:(SLComposeViewController *)controller
{
if ([controller.serviceType isEqualToString:SLServiceTypeTwitter]) {
NSString* message = #"The message you want."
[controller setInitialText:message];
}
[controller setCompletionHandler:^(SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultDone) {
DDLogInfo(#"Posted");
} else if (result == SLComposeViewControllerResultCancelled) {
DDLogInfo(#"Post Cancelled");
} else {
DDLogInfo(#"Post Failed");
}
}];
[self.parentVC presentViewController:controller animated:YES completion:nil];
}
Those methods enable you to share content (images, photos, message..) to your Facebook/Twitter and Google account directly from your app.
N.B: For Google it's a little bit different because their share method is now deprecated
Share Google+ iOS
But you can use the old way, like this example in order to share an URL for example :
- (void)showGooglePlusShare:(NSURL*)shareURL {
// Construct the Google+ share URL
NSURLComponents* urlComponents = [[NSURLComponents alloc]
initWithString:#"https://plus.google.com/share"];
urlComponents.queryItems = #[[[NSURLQueryItem alloc]
initWithName:#"url"
value:[shareURL absoluteString]]];
NSURL* url = [urlComponents URL];
if ([SFSafariViewController class]) {
// Open the URL in SFSafariViewController (iOS 9+)
SFSafariViewController* controller = [[SFSafariViewController alloc]
initWithURL:url];
controller.delegate = self;
[self.parentVC presentViewController:controller animated:YES completion:nil];
} else {
// Open the URL in the device's browser
[[UIApplication sharedApplication] openURL:url];
}
}
EDIT :
You can create only 1 IBAction button in order to share to social network.
And then the user has to choose which one.
The result will be something like this :
And the code example :
- (IBAction)shareContentSocialNetwork:(id)sender
{
if ([UIAlertController class]){
// ios 8 or higher
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"" message:#"Share on Social Network" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* fb = [UIAlertAction actionWithTitle:#"Facebook" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// Create a method in order to add image, text etc..
[self generateMessage:controller];
}else{
UIAlertView* facebookAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(#"Social.Account.FB.title", #"") message:NSLocalizedString(#"Social.Account.FB.message", #"") delegate:nil cancelButtonTitle:NSLocalizedString(#"Error.ok", #"") otherButtonTitles: nil];
[facebookAlert show];
}
}];
[alertController addAction:fb];
UIAlertAction* twit = [UIAlertAction actionWithTitle:#"Twitter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
// Create a method in order to add image, text etc..
[self generateMessage:controller];
}else{
UIAlertView* twitterAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(#"Social.Account.Twitter.title", #"") message:NSLocalizedString(#"Social.Account.Twitter.message", #"") delegate:nil cancelButtonTitle:NSLocalizedString(#"Error.ok", #"") otherButtonTitles: nil];
[twitterAlert show];
}
}];
[alertController addAction:twit];
UIAlertAction* ggl = [UIAlertAction actionWithTitle:#"Google+" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
NSURL *url = [[NSURL alloc] initWithString:#"yourContentURL"];
[self showGooglePlusShare:url];
}];
[alertController addAction:ggl];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:nil];
}
}
Basically I am creating the 3 specific actions of the AlertController.
For Twitter and Facebook it is pretty straightforward, even so you have to use the generateMessage method I showed you earlier.
Hope it helps.
I am use UIImagePickerView in my code in which three buttons are there. One to take photo, second to choose photo and third to cancel. The code as below:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select the operation to proceed?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Take Photo", #"Select Photo", nil];
[actionSheet showInView:self.view];
When I click on the Cancel button it does not work.
I am using UIImagePickerViewDelegate method as below.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
#try {
//set selected image in imageview by imagepickerview
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
imgProfilePic.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:nil];
}
#catch (NSException *exception) {
NSLog(#"%#",exception.description);
}
}
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
// NSLog(#"####");
// [picker dismissViewControllerAnimated:YES completion:NULL];
//}
Please check my code for UIImagePickerView and provide me guidance for correct code.
#pragma mark - Actionssheet delegate method for Image
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
#try {
if(buttonIndex != 3)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
if(buttonIndex == 0)
{
NSLog(#"Choose Photo from Camera");
//simulator has no camera so app will crash, below call just provide the alert that device has no camera.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
else
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
}
if(buttonIndex == 1)
{
NSLog(#"Choose Photo from Gallary");
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
// if (buttonIndex == 3)
// {
// NSLog(#"###");
// }
//
// else
// {
// NSLog(#"Cancel the tab");
// [picker dismissViewControllerAnimated:YES completion:NULL];
//
// }
[self presentViewController:picker animated:YES completion:nil];
}
}
#catch (NSException *exception) {
NSLog(#"%#",exception.description);
}
}
I think you are new for the stackOverflow and iOS application Development. That UIActionsheet and UIImagePickerController both are different things. So if you are create a actiohsheet like:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select the operation to proceed?"delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Select Photo", nil];
[actionSheet showInView:self.view];
Now you can called its button method like following:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
// take photo selected
}
else if (buttonIndex==1)
{
// select photo selected
}
else
{
// cancel button selected
}
}
You can also use UIAlertController that is the replacement of UIAlert and UIActionSheet from iOS8 so you can use like following:
UIAlertController* AlertSheet = [UIAlertController alertControllerWithTitle:#"New ActionSheet" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"Action" style:UIAlertActionStyleDefault //
handler:^(UIAlertAction * action) {
NSLog(#"Action");
}];
[AlertSheet addAction:defaultAction];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[AlertSheet addAction:cancleAction];
[self presentViewController:AlertSheet animated:YES completion:nil];
You can try this code its work for me
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:#"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Take a photo"
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL]; }];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:#"Choose Existing"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Choose existing"
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];
UIActionSheet is deprecated since iSO 8. You should use UIAlertController
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:#"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Take a photo"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:#"Select Photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Select Photo"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];
I am using the below code for clicking 5 consecutive images in just 1 click but i am unable to do this. what should I do?
- (IBAction)cameraaBtton:(id)sender
{
for ( int i=0;1<5; i++)
{
[self CapturePhoto];
}
}
and the capture photo method is:
- (void)CapturePhoto
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:NULL];
}
else
{
UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:#"Device has no camera" message:#"Dear user please buy a apple cell" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil];
[alertview show];
}
I have created a new app under xcode 6 and an old cold no more work.
Since iOS 8 we use UIalertcontroller to show action sheet
I use it to lauch photolibrary and select a picture but when I want to dismiss the picker, my app crash and I don't know why.
Below my code:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
UIAlertController * uiViewActionSheet= [UIAlertController
alertControllerWithTitle:#"Action"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* chooseExisting = [UIAlertAction
actionWithTitle:NSLocalizedString(#"CHOOSE_EXISTING",#"Choose Existing")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
[uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];
}];
[uiViewActionSheet addAction:chooseExisting];
[uiViewActionSheet addAction:cancel];
[self presentViewController:uiViewActionSheet animated:YES completion:nil];
Below the imagepicker function :
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.uImageProfile.image = chosenImage;
self.lstatutImage.text = #"save";
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
}
__block NSString* resultSaveImage = #"";
//Save image onthe server
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
resultSaveImage = [controllerObject saveProfileImage:self.uImageProfile.image];
dispatch_async(dispatch_get_main_queue(), ^{
if(![resultSaveImage isEqualToString:#"\"ok\""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"ERROR",#"Error") message:resultSaveImage delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
});
});
}
My code crash here :
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Have you any idea ?
You're dismissing the picker, and then calling it:
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera) {...
You also don't need the [uiViewActionSheet dismissViewControllerAnimated:YES completion:nil]; lines!
Try moving the dismiss line to the end. If it still crashes, it might be that the presentingViewController is not alive anymore.
Struggling with the UIImagePicker right now. I'm attempting to catch an empty library to prevent the user from getting sent there.
Here is where I check to see if the camera or library is available and has content:
imagePicker = [[UIImagePickerController alloc]init];
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
[[imagePicker navigationBar] setTintColor:nil];
//set the delegate
imagePicker.delegate = self;
//Determine if a camera is available and react accordingly
BOOL cameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
BOOL libraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
if (cameraAvailable) {
if(cameraAvailable && libraryAvailable) {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Take Photo With Camera", #"Select Photo From Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[actionSheet showInView:self.view];
} else {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Take Photo With Camera", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[actionSheet showInView:self.view];
}
}
else if(libraryAvailable) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePicker animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Media Types Available" message:#"The photo library does not contain any images and the camera is unavailable. Add pictures to your library if you wish to post an image." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
According to the docs
Because a media source may not be present or may be unavailable, devices may not always support all source types. For example, if you attempt to pick an image from the user’s library and the library is empty, this method returns NO
I have cleared all the photos from the library to confirm this will work but it still gets returned as YES. I have also checked UIImagePickerControllerSourceTypeSavedPhotosAlbum which is also getting returned as YES.
I performed a Reset content and settings in the simulator and verified in the settings menu that there is no photos stored on the device. Does this method not work on the simulator properly or am I doing something wrong?