Ich habe dieses Format (Abbildung unten) der Freigabeoption in den meisten iOS-Anwendungen gesehen, die iOS 7 unterstützen. Gibt es einen Standardcode/ein Standardframework zum Implementieren dieser Freigabeoption, wie im folgenden Bild gezeigt?
Was Sie suchen, ist das UIActivityViewController
.
Da Sie eine allgemeine Frage gestellt haben, kann ich Ihnen nur einen Link zur Dokumentation geben
Neben der akzeptierten Antwort ein kleines Stück Beispielcode
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(NSURL *)url
{
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:text];
}
if (image) {
[sharingItems addObject:image];
}
if (url) {
[sharingItems addObject:url];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
Rufen Sie shareText
an und belassen Sie die Dinge, die Sie nicht teilen möchten, bei nil
.
[self shareText:@"Hello world" andImage:nil andUrl:nil];
Der Controller in dem von Ihnen geposteten Bild ist der UIActivitiyViewController dies ist ein Link zur Klassendokumentation
ein gutes Beispiel: Wie zeige ich das Standardaktionsblatt für iOS 6-Freigaben mit verfügbaren Freigabeoptionen an?
Ich weiß, dass diese Frage speziell für iOS 7 gilt und das Codebeispiel iOS 6 angibt, aber AFAICT, sie sind sich sehr ähnlich. Man könnte den Beispielcode genauso hilfreich finden wie ich.
UIActivityViewController
ist das, wonach Sie suchen.
Sie können entweder die Elemente oder die Anwendungen angeben
UIActivityViewController *actCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
Verwenden Sie einfach den folgenden Code für die Standardfreigabe. Sie können dem Array shareItems
je nach Ihren Anforderungen weitere Elemente hinzufügen.
NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects:
@"Hello",
[UIImage imageNamed:@"your_image.png"],
@"http://google.com/", nil];
[self shareItemToOtherApp:shareItems];
Die folgende Methode ist für die Standardfreigabe von Text oder Bild in andere Apps vorgesehen: -
-(void)shareItemToOtherApp:(NSMutableArray *)shareItems{
UIActivityViewController *shareController = [[UIActivityViewController alloc]
initWithActivityItems: shareItems applicationActivities :nil];
[shareController setValue:@"Sharing" forKey:@"subject"];
shareController.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
shareController.completionHandler = ^(NSString *activityType, BOOL completed)
{
//NSLog(@" activityType: %@", activityType);
//NSLog(@" completed: %i", completed);
};
[self presentViewController: shareController animated: YES completion: nil];
}
Wenn Sie ein benutzerdefiniertes Freigabeblatt erstellen möchten, verwenden Sie den folgenden Code. Dazu müssen Sie das Framework <Social/Social.h>
Importieren.
-(void)shareOnFacebook:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *faceSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// NSLog(@"%@", messageField.text);//This returns the appropriate string
[faceSheet setInitialText:@"Hellooooooo"];
//The facebook VC appears, but initial text is not set to messageField.text
[self presentViewController:faceSheet animated:YES completion:nil];
}
else
{
NSLog(@"Please first install Application and login in Facebook");
}
}
-(void)shareOnTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:@"Hello"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else{
NSLog(@"Please first install Application and login in Twitter");
}
}
Hoffe, das ist was du suchst. Alle Bedenken melden sich bei mir. :)