10

我只想添加明显的按钮“在 Safari 中打开”我怎样才能以简单的方式做到这一点。

#pragma mark - Share the link
- (IBAction)activityButtonPressed:(id)sender {


    NSString *textToShare = @"I just shared this from my App";
   // UIImage *imageToShare = [UIImage imageNamed:@"Image.png"];
    NSURL *urlToShare = [NSURL URLWithString:@"http://www.google.com"];
    NSArray *activityItems = [NSArray arrayWithObjects:textToShare, urlToShare,nil];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:Nil];
    //This is an array of excluded activities to appear on the UIActivityViewController
    //activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:TRUE completion:nil];


}
4

4 回答 4

14

正如 nevo shalev 所说,您需要继承UIActivity 类

这是一个例子:

网址活动.h

#import <UIKit/UIKit.h>

@interface UrlActivity : UIActivity

@end

网址活动.m:

#import "UrlActivity.h"

@implementation UrlActivity

- (NSString *)activityType
{
    return @"your Custom Type";
}

- (NSString *)activityTitle
{
    return @"Title to display under your icon";
}

- (UIImage *)activityImage
{
    return [UIImage imageNamed:@"your icon.png"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    // basically in your case: return YES if activity items are urls  
}

- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    //open safari with urls (activityItems)
}

+(UIActivityCategory)activityCategory
{
    return UIActivityCategoryShare; // says that your icon will belong in application group, not in the lower part;
}

@end

在你的主文件中(在导入 UrlActivity.h 之后):

#pragma mark - Share the link
- (IBAction)activityButtonPressed:(id)sender {


    NSString *textToShare = @"I just shared this from my App";
   // UIImage *imageToShare = [UIImage imageNamed:@"Image.png"];
    NSURL *urlToShare = [NSURL URLWithString:@"http://www.google.com"];
    NSArray *activityItems = [NSArray arrayWithObjects:textToShare, urlToShare,nil];

    // create an array with your custom activity and add it to the activityVC
    NSArray *appActivities = [NSArray arrayWithObjects:[[UrlActivity alloc] init]]];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:appActivities];
    //This is an array of excluded activities to appear on the UIActivityViewController
    //activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:TRUE completion:nil];


}
于 2014-02-17T16:47:35.457 回答
3

这个项目为你做:https ://github.com/davbeck/TUSafariActivity它支持 CocoaPods

于 2014-07-22T02:29:16.457 回答
0

您需要继承 UIActivity 并实现方法,然后将类添加到 applicationActivities

于 2013-12-01T11:54:40.573 回答
-6
#pragma mark - Share the link
- (IBAction)activityButtonPressed:(id)sender {
    NSURL *urlToShare = [NSURL URLWithString:@"http://www.google.com"];
    [[UIApplication sharedApplication] openURL: urlToShare];
}
于 2013-02-13T10:18:13.870 回答