iOS如何实现分享按钮

时间:2017-05-23 07:54:36

标签: ios objective-c

我正在重新提出这个问题我在网页视图中有一个带按钮的uiwebview看起来像这样:

分享申请

现在我需要应用程序获取url并检查前4个字符=“sha:”如果为true,则运行share命令,如果为false,则在webview中打开。

现在通过分享我想要的应用程序

NSString *textToShare = @"Look at this awesome website for aspiring iOS Developers!";
    NSURL *myWebsite = [NSURL URLWithString:@"http://www.codingexplorer.com/"];

    NSArray *objectsToShare = @[textToShare, myWebsite];

    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];
}

提前谢谢

2 个答案:

答案 0 :(得分:0)

您需要在共享按钮

的操作方法中实现以下代码
UIActivityViewController *activityViewController =
  [[UIActivityViewController alloc] initWithActivityItems:@[string, URL]
                                    applicationActivities:nil];
[self presentViewController:activityViewController
                                      animated:YES
                                    completion:^{
  // ...
}];

此处在字符串变量中放置您的文本,并在URL变量中指定您的共享网址。

答案 1 :(得分:0)

我举一个实际的例子说明我是如何在我的应用中构建的:

share = [[UIButton alloc] init];
                [share setImage:[UIImage imageNamed:@"icon_share_ios"] forState:UIControlStateNormal];
                share.frame = CGRectMake(25, 90, 18, 18);
                [share        addTarget:self
                                 action:@selector(btnShare:)
                       forControlEvents:UIControlEventTouchUpInside];
                [mainView addSubview:share];





 -(void)btnShare:(id)sender {

    NSArray *objectsToShare;

    NSString* shareText = @"hey check out this new app on App Store";
    NSURL *website = [NSURL URLWithString:[NSString stringWithFormat:@"%@", urlExternalPages]];

    objectsToShare = @[shareText, website];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    NSArray *excludeActivities = @[UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo,
                                   UIActivityTypePostToTwitter,
                                   UIActivityTypePostToWeibo,
                                   UIActivityTypePostToTencentWeibo,
                                   UIActivityTypePrint,
                                   UIActivityTypeAirDrop,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeCopyToPasteboard];

    activityVC.excludedActivityTypes = excludeActivities;

    if ([activityVC respondsToSelector:@selector(popoverPresentationController)]) {
        // iOS8+
        activityVC.popoverPresentationController.sourceView = self.view;
        activityVC.popoverPresentationController.sourceRect = [(UIButton *)sender frame];
    }

    [self.navigationController presentViewController:activityVC animated:YES completion:nil];

}

希望有所帮助