Xcode:通过操作表共享内容

时间:2015-08-11 11:58:39

标签: ios objective-c iphone swift uiactionsheet

我想复制此行为(请参见下图),并使用此类操作表从我的应用分享内容。

enter image description here

问题是:

这真的是行动表吗?我找不到iOS 7或8的任何教程。不确定如何继续。

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

提示将不胜感激。

2 个答案:

答案 0 :(得分:60)

它不在UIActionSheet UIActivityController中,这是iOS中的默认功能。

<强>目标-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];
}

<强>夫特

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/

  4. <强>夫特

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

答案 1 :(得分:3)

您可以使用UIActivityController类来实现此结果。

请查看以下链接: - http://nshipster.com/uiactivityviewcontroller/

希望这有帮助!