How do I customize a UIActivityViewController to show a URL link when posting to facebook and twitter?

后端 未结 4 1266
礼貌的吻别
礼貌的吻别 2020-12-14 07:08

So I\'m trying out the new UIActivityViewController in iOS 6, and it\'s really easy to get up and running, but I can\'t figure out how to control it like I want

4条回答
  •  悲&欢浪女
    2020-12-14 07:57

    If you don't specifically need to use UIActivityViewController using SLComposeViewController is easy to use for this purpose.

    +(void)presentShareWidgetWithText:(NSString*)text url:(NSURL*)url images:(NSArray*)images toService:(NSString*)service presentIn:(UIViewController*)parentViewController {
        SLComposeViewController* controller = [SLComposeViewController composeViewControllerForServiceType:service];
        if (controller == nil) {
            DLog(@"Could not initialize SLComposeViewController for %@!", service);
            return;
        }
        if (url != nil) {
            if (![controller addURL:url])
                DLog(@"Failed adding url %@!", url);
        }
        if (images != nil) {
            for (UIImage* image in images) {
                if (![controller addImage:image]) {
                    DLog(@"Failed adding image: %@", image);
                }
            }
        }
        if (![controller setInitialText:text]) {
            DLog(@"Failed setting initial text: %@", text);
        }
    
        SLComposeViewControllerCompletionHandler handler = ^(SLComposeViewControllerResult result) {
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    DLog(@"SLComposeViewControllerResultCancelled");
                    break;
    
                case SLComposeViewControllerResultDone:
                    DLog(@"SLComposeViewControllerResultDone, Shared on service: %@", service);
                    break;
    
                default:
                    DLog(@"Unhandled result: %d", result);
                    break;
            }
    
            dispatch_async(dispatch_get_main_queue(), ^{
                [parentViewController dismissViewControllerAnimated:YES completion:^{
                    DLog(@"Dismissed a controller here!");
                }];
            });
        };
    
        [controller setCompletionHandler:handler];
    
        [parentViewController presentViewController:controller animated:YES completion:^{
            DLog(@"Presented %@!", controller);
        }];
    }
    

提交回复
热议问题