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.
Related
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];
Technology: Objective-C, xCode 7
#property (nonatomic, copy) NSString *notes;
#synthesize notes;
example.notes = #"Click <a href='http://www.google.com'>here</a>";
NSString *msg = [#"" stringByAppendingFormat:#"%#", myAnnotation.notes];
UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle: #"Title of Alert Box" message: msg preferredStyle: UIAlertControllerStyleAlert];
[alertViewController addAction: [UIAlertAction actionWithTitle: #"Close" style: UIAlertActionStyleCancel handler: nil]];
[self presentViewController: alertViewController animated: YES completion: nil];
Trying to get a link to appear in the Alert Box, but having no luck. Just outputs as plain text.
Is this even possible? If so, using my example above, how would you implement it?
Just like this.
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Alert Title"
message:msg
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"GO"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSString *urlString = #"someurl.com";
NSURL *url = [NSURL URLWithString:urlString];
if (NSClassFromString(#"SFSafariViewController"))
{
SFSafariViewController *safariViewController = [[SFSafariViewController alloc]initWithURL:url];
safariViewController.delegate = self;
[self presentViewController:safariViewController animated:YES completion:nil];
}
else
{
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
}
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
I've added code for opening link in SFSafariController only for iOS 9. In iOS 8 devices, it will open with safari browser as SFSafariController does not exist in iOS 8. This is done because apple now considers it bad user experience to exit the app to go to external links. So you can either open links in a web view or Safari Controller.
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];
}
}];
Is there a supported way of launching Google Authenticator on iOS?
I want to make it easier for customers to open the app and copy out the time-based code, before pasting it back into my app.
I've empirically discovered that this (Swift) code will launch the app:
UIApplication.sharedApplication().openURL(NSURL(string: "otpauth://")!)
...but I want to know if there is a better, supported way.
Specifically, is the otpauth:// protocol supported without arguments to simply launch the app?
Looking at the Git repo for the app it does seem like they have registered the Custom URL Schemes for bot otpauth and totp
https://github.com/google/google-authenticator/blob/bd50d15c348a978c314d2b30e586fbc562096223/mobile/ios/OTPAuth-Info.plist#L42
And here
https://github.com/google/google-authenticator/blob/bd50d15c348a978c314d2b30e586fbc562096223/mobile/ios/Classes/OTPAuthURL.h#L23
And here is the documentation on how exactly to build the url:
https://github.com/google/google-authenticator/wiki/Key-Uri-Format
After you form them correctly and get your app and the Google Authenticator app on the same device you would just need to test.
Objective C
if ([[[notification realRequestResults] valueForKey:#"action"] isEqualToString:#"2FA"]) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Two Factor Authentication"
message:#"Please, enter your Google Authenticator 2FA Token."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"Confirm Token"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
#try {
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
[userName text], #"loginName",
[passwordField text], #"password",
#"false", #"rememberMe",
[[alert textFields][0] text], #"tfa",
nil];
[self callWebserviceForIdentifier:AuthRequestInternalLogin
withParameters:parameters
onSuccessSelector:#selector(loginSuccessfulAgain:)
onFailureSelector:#selector(loginFailedAgain:)];
} #catch (NSException *exception) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"login"
message:[NSString stringWithFormat:#"%#", exception.description]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
} #finally {
}
}];
[alert addAction:defaultAction];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.delegate = self;
textField.placeholder = [NSMutableString stringWithString:#"Enter your 2FA token"];
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.font = [UIFont systemFontOfSize:16.0];
textField.textAlignment = NSTextAlignmentCenter;
textField.textColor = UIColor.blackColor;
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"authenticator.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(authenticatorBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeAlways;
textField.rightView = addButton;
}];
[self presentViewController:alert animated:YES completion:nil];
}
else {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Warning"
message:#"Invalid Credentials. Please try again."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
[self stopAnimation];
}
}
-(IBAction)authenticatorBtnClicked:(id)sender{
NSString *AppStoreURL = #"https://apps.apple.com/in/app/google-authenticator/id388497605";
NSString *customAppURL = #"otpauth://";
BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customAppURL]];
NSString *url = canOpenURL ? customAppURL : AppStoreURL;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
}
In Info.plist File
In my project i am having 5 levels. In First and second level i can able to move and shoot at a time. When completing 2nd level i want to share it with Facebook. After facebook share i cant able to move and shoot at a time. Only one process is working(Either shoot or move).
What i want to do for solve this problem.
My coding is here:
{
UIViewController*v=[[UIViewController alloc]init];
[[[CCDirector sharedDirector]openGLView]addSubview:v.view];
SLComposeViewController*mySLComposerSheet;
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
NSString *strg=[NSString stringWithFormat:#" "];
NSString *bodyStr=[NSString stringWithFormat:#"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"!\n;
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:bodyStr]; //the message you want to post
[mySLComposerSheet addImage:[UIImage imageNamed:#"my.jpg"]]; //an image you could post
NSURL *url = [NSURL URLWithString:#"https:example.com"];
[mySLComposerSheet addURL:url];
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output,*head;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"FREE levels NOT unlocked";
head=#"Facebook Share Unsuccessfull";
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:head message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
[self reload];
break;
case SLComposeViewControllerResultDone:
output = #"Extra FREE levels successfully unlocked";
head=#"Successfull";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:head message:output delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
alert.tag=22;
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}
You are presenting the ViewController mySLComposerSheet
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
but there is no dismiss... statement in your completionHandler
[self dismissViewControllerAnimated:YES completion:nil];
see the post https://stackoverflow.com/a/12651085/2787026