2013-02-13 20 views
10

Ich möchte einfach die offensichtliche Schaltfläche hinzufügen "Öffnen in Safari" Wie kann ich es auf einfache Weise tun.Hinzufügen einer offensichtlichen (benutzerdefinierten) Schaltfläche zu 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]; 


} 

Antwort

14

hinzufügen Als nevo Shalev sagte Sie UIActivity class

Hier ist ein Beispiel, Unterklasse müssen:

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 

Und in der Hauptdatei (nach UrlActivity.h Import):

#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

Kleine Korrektur: 'NSArray * appActivities = [NSArray ArrayWithObjects: [[ UrlActivity alloc] init]]]; ' muss sein ' 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

That Ich möchte keine Freigabe-Schaltfläche hinzufügen, möchte ich alle Freigabeoptionen haben activityView gibt + öffnen in Safari – chewy

+0

Sie sagten, offen in Safari –

+2

Es tut auch nicht weh zu [google around] (https://github.com/davbeck/TUSafariActivity) und [around] (http://uiactivities.com/) –

0

Sie benötigen UIActivity Unterklasse und und die Methoden zu implementieren und nach die Klasse zu applicationActivities

Verwandte Themen