Обмен через UIActivityViewController для Twitter/Facebook и т.д., Вызывающий сбой
На iOS8 я использую UIActivityViewController для совместного использования UIImage в Facebook/Twitter и т.д. Он работал нормально, но сегодня он внезапно начал сбой при запуске кода на моем iPad. Тем не менее, он по-прежнему работает, как ожидалось, в симуляторе.
Мой код:
UIActivityViewController *controller =
[[UIActivityViewController alloc]
initWithActivityItems:@[text, url, myImage]
applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
После сбоя Xcode выплевывает:
Обнаруженные расширения: {( {id = com.apple.share.Facebook.post}, {id = com.apple.share.Twitter.post}, {id = com.apple.share.TencentWeibo.post}, {id = com.apple.share.SinaWeibo.post})} для атрибутов: { NSExtensionActivationRule = { extensionItems = ( { вложения = ( { registeredTypeIdentifiers = ( "Public.image" ); }, { registeredTypeIdentifiers = ( "Public.plain-текст" ); }, { registeredTypeIdentifiers = ( "Public.url" ); } ); } ); }; NSExtensionPointName = ( "com.apple.share-услуги", "com.apple.ui-услуги", "com.apple.services" ); } 2014-08-07 21: 38: 59.208 collageTest [279: 11021] LaunchServices: invalidationHandler называется 2014-08-07 21: 38: 59.212 collageTest [279: 11016] Обнаруженные расширения: {( {id = com.apple.share.Flickr.post}, {id = com.apple.mobileslideshow.StreamShareService}, {id = com.apple.share.Twitter.post}, {id = com.apple.share.Facebook.post}, {id = com.apple.share.Vimeo.post}, {id = com.apple.share.SinaWeibo.post}, {id = com.apple.share.TencentWeibo.post})} для атрибутов: { NSExtensionPointName = "com.apple.share-services"; } 2014-08-07 21: 38: 59.216 collageTest [279: 11021] LaunchServices: invalidationHandler называется
Ответы
Ответ 1
Посмотрев документы, мне нужно было определить представление источника для контроллера popover
UIActivityViewController *controller =
[[UIActivityViewController alloc]
initWithActivityItems:@[text,url,myImage]
applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
UIPopoverPresentationController *presentationController =
[controller popoverPresentationController];
presentationController.sourceView = self.view;
Ответ 2
popoverPresentationController
был новичком в iOS 8 и рухнет на iOS 7. Он также будет ноль на iPhone, потому что он только в UIPopover
на iPad. Здесь христианский ответ в Свифте, с учетом этих фактов:
Swift 2.0 (Xcode 7)
let controller = UIActivityViewController(activityItems: [text, url, myImage], applicationActivities: nil)
presentViewController(controller, animated: true, completion: nil)
if #available(iOS 8.0, *) {
let presentationController = controller.popoverPresentationController
presentationController?.sourceView = view
}
Swift 1.2 (Xcode 6)
let controller = UIActivityViewController(activityItems: [text, url, myImage], applicationActivities: nil)
presentViewController(controller, animated: true, completion: nil)
if controller.respondsToSelector("popoverPresentationController") {
// iOS 8+
let presentationController = controller.popoverPresentationController
presentationController?.sourceView = view
}
Ответ 3
Как сказал @mmccomb здесь, на iPad контроллер активности будет отображаться как popover с помощью нового UIPopoverPresentationController. Вам нужно указать хотя бы исходный вид:
activityViewController.popoverPresentationController.sourceView = YOURDESIREDVIEW;
Если вы хотите показать, что popover привязано к любой точке этого представления, укажите его с свойством sourceRect объекта popoverPresentationController.
Ответ 4
Вот как я решил с быстрым:
let someText:String = "shareText"
let google:NSURL = NSURL(string:"http://google.com")!
let activityViewController = UIActivityViewController(activityItems: [someText, google], applicationActivities: nil)
if respondsToSelector("popoverPresentationController") {
self.senderView.presentViewController(activityViewController, animated: true, completion: nil)
activityViewController.popoverPresentationController?.sourceView = sender
}else{
senderView.presentViewController(activityViewController, animated: true, completion: nil)
}