iOS share button crashing when run on ipad - ios

I am working on a xcode project in objective-c and i want a share button on my ios application to share things i watched a tutorial on youtube here's the link : https://www.youtube.com/watch?v=zkrOKN_OA_A
but when i run this code on iphone it works properly and when i run this code on ipad the xcode give's me this error :
int main(int argc, char * argv[]) {
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}}
I used this code :
- (IBAction)Share1:(id)sender {
UIImage *shareImage = [UIImage imageNamed:#"invoice_logo.png"];
NSArray *itemsToShare = #[shareImage];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[];
[self presentViewController:activityVC animated:YES completion:nil];}

You are missing :
activityVC.excludedActivityTypes=#[UIActivityTypePostToFacebook,UIActivityTypePostToTwitter]// add your share types
look at the video you provided from 7:30 min

Try this:
UIImage *shareImage = [UIImage imageNamed:#"invoice_logo.png"];
NSArray *itemsToShare = #[shareImage];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.popoverPresentationController.sourceView = self.view;
activityVC.popoverPresentationController.sourceRect =
CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height-50, 1.0, 1.0);
[self presentViewController:activityVC animated:YES completion:nil];

Related

iOS - Is it possible to share UIImage with Programatically text to WhatsApp in iOS?

NSString *texttoshare = #"Hello world....";
UIImage *image = [UIImage imageNamed:#"image.png"]
NSArray *activityItems = #[image,texttoshare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAirDrop];
[self presentViewController:activityVC animated:TRUE completion:nil];
You cant share image and text together, but you can convert image and text together has pdf and you can share pdf PDF documents (UTI: com.adobe.pdf)
https://www.whatsapp.com/faq/en/iphone/23559013

UIActivityViewController not working on iPad? iOS 8

I've followed suggestions from similar SO questions and can't seem to make this work. I'm working on a game, which reports the last score and then a share button for that last score.
This works on iPhone and my code for the iPad as shown below doesn't. The iPad version just crashes as soon as I hit the Share button. This code was from this tutorial on Coding Explorer.
iPad code (doesn't work)
- (IBAction)shareButton:(id)sender;
{
NSString * text = [NSString stringWithFormat:#"I just scored %li in Flying Freddy. Think you can beat it? https://itunes.apple.com/app/id1033007818", (long)_score];
NSArray *objectsToShare = #[text];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
NSArray *excludeActivities = #[UIActivityTypeAirDrop,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo];
activityVC.excludedActivityTypes = excludeActivities;
[self presentViewController:activityVC animated:YES completion:nil];
}
iPhone code (does work)
- (IBAction)ShareIt {
NSString * text = [NSString stringWithFormat:#"I just scored %li in Flying Freddy. Think you can beat it? https://itunes.apple.com/app/id1033007818", (long)_score];
NSArray * activityItems = #[text];
UIActivityViewController * avc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
avc.excludedActivityTypes = #[ UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
[self presentViewController:avc animated:YES completion:nil];
}
You must add a source view.
Try to add this line :
activityVC.popoverPresentationController.sourceView = self.view;
You can also add the arrow direction :
activityVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionRight;
activityVC.popoverPresentationController.sourceView = sender;
I also had to set the modalPresentationStyle, preferredContentSize and sourceRect for the Pipiks answer to work.

sharedApplication takes long to display

So I added some code to this to work on both ios 8 and 9(5s and ipad). Before the change the 5s would load the share dialog fairly quick while the ipad would crash the device. After adding the fix they both don't crash, but now the ipad displays the share dialog fairly quick while the 5s takes roughly ~1-2 mins to display it. Not sure why that might be, maybe I did something wrong with my change?
#import <Foundation/Foundation.h>
extern "C" {
void _shareText(const char *text, const char *imgUrl)
{
NSString * message = [NSString stringWithUTF8String:text];
NSString * urlAddress = [NSString stringWithUTF8String:imgUrl];
NSURL *url = [NSURL URLWithString:urlAddress];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:message];
}
if (img) {
[sharingItems addObject:img];
}
UIViewController *qtController = [[UIApplication sharedApplication].keyWindow rootViewController];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
if ([activityController respondsToSelector:#selector(popoverPresentationController)] ){
[qtController presentViewController:activityController animated:YES completion:nil];
activityController.popoverPresentationController.sourceView = qtController.view;
}else{
[qtController presentViewController:activityController animated:YES completion:nil];
}
}
}
Update:
Unity Called function:
[DllImport("__Internal")]
private static extern void _shareText(string message, string imgUrl);
public static void InviteRequest(string message, string imgUrl)
{
#if UNITY_IOS
if (Application.platform != RuntimePlatform.OSXEditor)
{
_shareText(message, imgUrl);
}
#endif
}

Email issue iOS8 in Simulator

I have an issue in iOS8 issue in Simulator in won't work ? can anybody help me for this ?
here message is String Message but it not appear in Mail sheet ?
Here's what i have tried !!
NSMutableString* sub = [NSMutableString string];
[message appendFormat:#"\n"];
[sub appendString:#"Item Id : "];
[sub appendString:item.item_id];
UIImage *img = self.thumbnailImageView.image;
NSMutableArray *actItems = [[NSMutableArray alloc] init];
[actItems addObject:message];
[actItems addObject:sub];
if(img != nil)
{
[actItems addObject:img];
}
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:actItems
applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter,
UIActivityTypeAirDrop];
[self.navigationController presentViewController:activityViewController
animated:YES
completion:^{}];
[activityViewController setValue:sub forKey:#"subject"];
activityViewController.popoverPresentationController.sourceView = parentView;

UIActivityViewController issue iOS 7 and iOS 8?

I’m building an article reading app for iPad. I have integrated a social sharing functionality which means user can share articles on Facebook, and google mail.
I’m using UIActivityViewController for sharing.
There is a bar button item,when user click on that UIActivityViewController opens.I updated Xcode 6
When I run on simulator it runs fine But I run on real device(iPad) with iOS 7,the app get crash on clicking on bar button item.
this is my code:
- (IBAction)ysshareAction:(id)sender
{
NSURL *linkURL = [NSURL URLWithString:_DetailModal1[4]];//article url
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:_DetailModal1[0]];//_DetailModal1[0] contain article title////
[stringText addAttribute:NSLinkAttributeName value:linkURL range:NSMakeRange(0, stringText.length)];
NSArray * itemsArray = #[[NSString stringWithFormat:#"%#",_DetailModal1[0]], [NSURL URLWithString:_DetailModal1[4]]];
NSArray * applicationActivities = nil;
UIActivityViewController * AVC = [[UIActivityViewController alloc] initWithActivityItems:itemsArray applicationActivities:applicationActivities];
AVC.popoverPresentationController.sourceView = _webView;
[self presentViewController:AVC animated:YES completion:nil];
[AVC setCompletionHandler:^(NSString *act, BOOL done)
{
if([act isEqualToString:UIActivityTypeMail]) {
ServiceMsg = #"Mail sent!";
} else if([act isEqualToString:UIActivityTypePostToTwitter]) {
ServiceMsg = #"Article Shared!";
} else if([act isEqualToString:UIActivityTypePostToFacebook]) {
ServiceMsg = #"Article Shared!";
} else if([act isEqualToString:UIActivityTypeMessage]) {
ServiceMsg = #"SMS sent!";
} else if([act isEqualToString:UIActivityTypeAddToReadingList]) {
ServiceMsg = #"Added to Reading List";
} else if([act isEqualToString:UIActivityTypeCopyToPasteboard]){
ServiceMsg = #"Copied Link";
}
if ( done )
{
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:ServiceMsg message:#"" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[Alert show];
}
}];
}
Help is appreciated!
Following line is the issue
AVC.popoverPresentationController.sourceView = _webView;
You will have to put iOS8 condition in order popoverPresentationController is introduced for iOS 8 and later so you can not use it with iOS 7
For checking for iOS8 you can define a macro like found from here
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
And use it in following way.
NSURL *linkURL = [NSURL URLWithString:_DetailModal1[4]];//article url
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:_DetailModal1[0]];//_DetailModal1[0] contain article title////
[stringText addAttribute:NSLinkAttributeName value:linkURL range:NSMakeRange(0, stringText.length)];
NSArray * itemsArray = #[[NSString stringWithFormat:#"%#",_DetailModal1[0]], [NSURL URLWithString:_DetailModal1[4]]];
NSArray * applicationActivities = nil;
UIActivityViewController * AVC = [[UIActivityViewController alloc] initWithActivityItems:itemsArray applicationActivities:applicationActivities];
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")){
AVC.popoverPresentationController.sourceView = _webView;
}
[self presentViewController:AVC animated:YES completion:nil];
Refer this for more info about what has changed for UIActivityViewController in iOS8
A lot might argue that checking for existence of the class explicitly is better than checking a hard coded version number. UIPopoverPresentationController may be deprecated at some future point, or there might be a (future ?) device which does not support the class, like the iPhone never used to support UIPopoverController or UISplitViewController..
if ( NSClassFromString(#"UIPopoverPresentationController") ) {
AVC.popoverPresentationController.sourceView = _webView;
}
In Swift, You can use '?' instead checking OS version.
AVC.popoverPresentationController?.sourceView = _webView

Resources