The UIActivityViewController dinyatakan dalam jawaban lain membuat ini sepele. Yang harus Anda lakukan adalah menentukan teks / gambar / URL yang ingin Anda bagikan dan menampilkan pengontrol tampilan aktivitas secara sederhana dan iOS akan secara otomatis menampilkan semua layanan berbagi yang berlaku. Contoh:
Objective-C
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(URL *)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];
}
Cepat
func share(sharingText: String?, sharingImage: UIImage?, sharingURL: URL?) {
let sharingItems:[AnyObject?] = [
sharingText as AnyObject,
sharingImage as AnyObject,
sharingURL as AnyObject
]
let activityViewController = UIActivityViewController(activityItems: sharingItems.compactMap({$0}), applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityViewController.popoverPresentationController?.sourceView = view
}
present(activityViewController, animated: true, completion: nil)
}