2013-02-13 23 views
10

Tôi chỉ muốn thêm nút hiển thị "Mở trong Safari" Tôi có thể làm điều đó một cách đơn giản như thế nào.Thêm nút (tùy chỉnh) rõ ràng vào UIActivityViewController

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


} 

Trả lời

14

Như Nevo Shalev nói rằng bạn cần phải phân lớp UIActivity class

Dưới đây là một ví dụ:

UrlActivity.h

#import <UIKit/UIKit.h> 

@interface UrlActivity : UIActivity 

@end 

UrlActivity.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 

Và trong tập tin chính của bạn (sau khi nhập 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]; 


} 
+0

Sửa đổi nhỏ: 'NSArray * appActivities = [NSArray arrayWithObjects: [[ Phân bổ UrlActivity] init]]]; ' cần phải là ' NSArray * appActivities = [NSArray arrayWithObjects: [[MobrActivityView alloc] init], nil]; ' – SpittingLlama

-6
#pragma mark - Share the link 
- (IBAction)activityButtonPressed:(id)sender { 
    NSURL *urlToShare = [NSURL URLWithString:@"http://www.google.com"]; 
    [[UIApplication sharedApplication] openURL: urlToShare]; 
} 
+0

Đó sẽ không thêm nút chia sẻ, tôi muốn có tất cả các tùy chọn chia sẻ hoạt độngXem + mở trong safari – chewy

+0

Bạn đã nói mở trong safari –

+2

Nó cũng không làm tổn thương [google around] (https://github.com/davbeck/TUSafariActivity) và [xung quanh] (http://uiactivities.com/) –

0

bạn cần phải phân lớp UIActivity và và thực hiện các phương pháp và sau khi thêm lớp để applicationActivities

Các vấn đề liên quan