I would like to replicate this behaviour (see image below) and share contents from my app using this kind of action sheet.
The question is:
Is this really an Action Sheet? I can't find anywhere tutorial for iOS 7 or 8. Not sure how to proceed.
Do the sharing options depends on user's configurations?
Hints would be appreciated.
It is not in UIActionSheet it is in UIActivityController, which is the default function in iOS.
objective-C
- (void)presentActivityController:(UIActivityViewController *)controller {
// for iPad: make the presentation a Popover
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:controller animated:YES completion:nil];
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionAny;
popController.barButtonItem = self.navigationItem.leftBarButtonItem;
// access the completion handler
controller.completionWithItemsHandler = ^(NSString *activityType,
BOOL completed,
NSArray *returnedItems,
NSError *error){
// react to the completion
if (completed) {
// user shared an item
NSLog(@"We used activity type%@", activityType);
} else {
// user cancelled
NSLog(@"We didn't want to share anything after all.");
}
if (error) {
NSLog(@"An Error occured: %@, %@", error.localizedDescription, error.localizedFailureReason);
}
};
}
-(void)sendMessage {
//create a message
NSString *theMessage = @"Some text we're sharing with an activity controller";
NSArray *items = @[theMessage];
// build an activity view controller
UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
// and present it
[self presentActivityController:controller];
}
Swift
let shareText = "Hello, world!"
if let image = UIImage(named: "myImage") {
let vc = UIActivityViewController(activityItems: [shareText, image], applicationActivities: [])
present(vc, animated: true, completion: nil)
}
Try these links for tutorials
http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/
http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/
Swift
You can achieve this result by using UIActivityController class.
Please take a look at this link :- http://nshipster.com/uiactivityviewcontroller/
Hope this helps!
来源:https://stackoverflow.com/questions/31941188/xcode-sharing-content-via-action-sheet
