Xcode:通过操作表共享内容

33

我希望能复制这种行为(见下图),并使用这种操作表分享我的应用程序内容。

enter image description here

问题是:

这真的是一个操作表吗?我找不到任何适用于iOS 7或8的教程。不确定如何继续。

分享选项是否取决于用户的配置?

希望有提示。


4
т«ЃТў» UIActivityViewController УђїСИЇТў»СИђСИф actionsheetсђѓ - Bhavin Bhadani
我们如何在UIActivityViewController中添加Instagram? - Himali Shah
2个回答

67

这个功能不在UIActionSheet里面,而是在UIActivityController中,它是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)
}

请查看以下教程链接:
  1. http://nshipster.com/uiactivityviewcontroller/

  2. http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/

  3. http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/

Swift

https://www.hackingwithswift.com/example-code/uikit/how-to-share-content-with-uiactivityviewcontroller

这些链接提供了有关UIActivityViewController的教程。

1
这个链接对我特别有帮助:https://www.hackingwithswift.com/example-code/uikit/how-to-share-content-with-uiactivityviewcontroller - Rostyslav Druzhchenko

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接