Reputation: 8947
I have an application which uses UIActivityViewController
to share a link with NSExtensionActivationSupportsWebURLWithMaxCount
like so:
NSURL *shareUrl = [NSURL URLWithString:[[sarray valueForKey:@"url"] description]];
NSString* someText = @"";
NSArray* dataToShare = @[shareUrl,someText]; // ...or whatever pieces of data you want to share.
NSArray *appActivities = [NSArray arrayWithObjects:[[UIActivity alloc] init], nil];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:appActivities];
activityViewController.excludedActivityTypes=@[UIActivityTypeAddToReadingList,UIActivityTypeAirDrop,UIActivityTypePrint,UIActivityTypeAssignToContact];
[self.navigationController presentViewController:activityViewController animated:YES completion:nil];
[activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (!completed) return;
}];
Except for Instagram, all options are coming up in the UIActivityViewController
.
Can anyone help me discover where I am going wrong or is this by default?
Upvotes: 4
Views: 3239
Reputation: 31
Instagram option will not be available with only text. For displaying instagram option in activity controller, you need to pass url object with text, or only url object else you can pass only image to share items which will enable instagram option for you.
Upvotes: 0
Reputation: 321
Swift 4.0+
To get the instagram app to show in the icon list, you must share an UIImage
object in your dataToShare
array.
I ran into a problem where my shared items contained a URL
and an UIImage
, and when sharing to iMessages I only wanted the URL
to be shared and not the UIImage
.
To solve this I made the presenting UIViewController conform to UIActivityItemSource
protocol like so:
extension PresentingViewController: UIActivityItemSource {
public func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
UIImage() // This allows instagram app to show as a choice
}
public func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
guard let items = self.items else { return nil } // items to present
switch activityType! {
case .message, .postToTwitter, .copyToPasteboard, .mail:
return items.first(where: { $0 is URL })
default: // this catches the case where IG is tapped
return items.first(where: { $0 is UIImage }) as? UIImage
}
}
}
and instead of passing in an array of Activity Items that contain the object you want to share, you pass in the PresentingViewController like so:
// set up activity view controller
activityVC = UIActivityViewController(
activityItems: [PresentingViewController.self],
applicationActivities: [instaGramActivity]
)
self.present(activityVC, animated: true, completion: nil)
Upvotes: 1
Reputation: 1468
By default iOS doesn't have support for instagram. U might need to use 3rd party libraries.
For iOS 6 and above, you can use this UIActivity to upload images to Instagram which has the same workflow using iOS hooks but simplifies development.
Try this library
Upvotes: 1