summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/ios/qiosfiledialog.mm
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@qt.io>2016-09-28 13:58:56 +0200
committerRichard Moe Gustavsen <richard.gustavsen@qt.io>2016-10-26 08:55:10 +0000
commit99ce5d5e85007cb656cd48ab161be14af8f16238 (patch)
tree45a1f975156cd16fd5505793de0ca3df583cad45 /src/plugins/platforms/ios/qiosfiledialog.mm
parent60dbd80e5a26182b3ed9e2c60bb7212dfd32632b (diff)
iOS: refactor usage of photos into optional plugin
Starting from iOS 10, apps that tries to access photos on the device need to specify the reason for this up front by adding the key 'NSPhotoLibraryUsageDescription' into Info.plist. If the key is missing, the app will be rejected from AppStore. This causes problems for the iOS plugin as it stands since parts of it already tries to access photos, e.g to show an image picker dialog if a file dialog is set to open QStandardPaths::PicturesLocation. This means that currently, all apps written with Qt will be rejected from AppStore unless the developer adds this key, whether he tries to access photos or not. To solve this, we choose to split the plugin into two parts, one that contains the core functionality, and one that contains optional support. The latter will need to be enabled explicit by the developer in the pro file, or in this case, indirectly by adding the right key to the Info.plist. This patch refactors the code in the plugin that gives access to photos into a separate optional plugin called 'nsphotolibrarysupport'. Change-Id: Ic4351eb0bbfffdf840fd88cd00bb29a25907798f Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Jake Petroules <jake.petroules@qt.io>
Diffstat (limited to 'src/plugins/platforms/ios/qiosfiledialog.mm')
-rw-r--r--src/plugins/platforms/ios/qiosfiledialog.mm79
1 files changed, 33 insertions, 46 deletions
diff --git a/src/plugins/platforms/ios/qiosfiledialog.mm b/src/plugins/platforms/ios/qiosfiledialog.mm
index 7c32784e9d..a70eb11ef8 100644
--- a/src/plugins/platforms/ios/qiosfiledialog.mm
+++ b/src/plugins/platforms/ios/qiosfiledialog.mm
@@ -31,52 +31,18 @@
**
****************************************************************************/
-#include "qiosfiledialog.h"
-
#import <UIKit/UIKit.h>
#include <QtCore/qstandardpaths.h>
#include <QtGui/qwindow.h>
+#include <QDebug>
-@interface QIOSImagePickerController : UIImagePickerController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
- QIOSFileDialog *m_fileDialog;
-}
-@end
-
-@implementation QIOSImagePickerController
-
-- (id)initWithQIOSFileDialog:(QIOSFileDialog *)fileDialog
-{
- self = [super init];
- if (self) {
- m_fileDialog = fileDialog;
- [self setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
- [self setDelegate:self];
- }
- return self;
-}
-
-- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
-{
- Q_UNUSED(picker);
- NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
- QUrl fileUrl = QUrl::fromLocalFile(QString::fromNSString([url description]));
- m_fileDialog->selectedFilesChanged(QList<QUrl>() << fileUrl);
- emit m_fileDialog->accept();
-}
-
-- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
-{
- Q_UNUSED(picker)
- emit m_fileDialog->reject();
-}
-
-@end
-
-// --------------------------------------------------------------------------
+#include "qiosfiledialog.h"
+#include "qiosintegration.h"
+#include "qiosoptionalplugininterface.h"
QIOSFileDialog::QIOSFileDialog()
- : m_viewController(0)
+ : m_viewController(Q_NULLPTR)
{
}
@@ -98,17 +64,36 @@ bool QIOSFileDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality window
bool acceptOpen = options()->acceptMode() == QFileDialogOptions::AcceptOpen;
QString directory = options()->initialDirectory().toLocalFile();
- if (acceptOpen && directory.startsWith(QLatin1String("assets-library:"))) {
- m_viewController = [[QIOSImagePickerController alloc] initWithQIOSFileDialog:this];
- UIWindow *window = parent ? reinterpret_cast<UIView *>(parent->winId()).window
- : [UIApplication sharedApplication].keyWindow;
- [window.rootViewController presentViewController:m_viewController animated:YES completion:nil];
- return true;
- }
+ if (acceptOpen && directory.startsWith(QLatin1String("assets-library:")))
+ return showImagePickerDialog(parent);
return false;
}
+bool QIOSFileDialog::showImagePickerDialog(QWindow *parent)
+{
+ if (!m_viewController) {
+ QFactoryLoader *plugins = QIOSIntegration::instance()->optionalPlugins();
+ for (int i = 0; i < plugins->metaData().size(); ++i) {
+ QIosOptionalPluginInterface *plugin = qobject_cast<QIosOptionalPluginInterface *>(plugins->instance(i));
+ m_viewController = [plugin->createImagePickerController(this) retain];
+ if (m_viewController)
+ break;
+ }
+ }
+
+ if (!m_viewController) {
+ qWarning() << "QIOSFileDialog: Could not resolve Qt plugin that gives access to photos on iOS";
+ return false;
+ }
+
+ UIWindow *window = parent ? reinterpret_cast<UIView *>(parent->winId()).window
+ : [UIApplication sharedApplication].keyWindow;
+ [window.rootViewController presentViewController:m_viewController animated:YES completion:nil];
+
+ return true;
+}
+
void QIOSFileDialog::hide()
{
// QFileDialog will remember the last directory set, and open subsequent dialogs in the same
@@ -120,6 +105,8 @@ void QIOSFileDialog::hide()
emit directoryEntered(QUrl::fromLocalFile(QDir::currentPath()));
[m_viewController dismissViewControllerAnimated:YES completion:nil];
+ [m_viewController release];
+ m_viewController = Q_NULLPTR;
m_eventLoop.exit();
}