[Interest] iOS Objective-C callback block example connecting back to iOS Qt app?

Ben Lau xbenlau at gmail.com
Sat May 16 20:33:10 CEST 2015


Hi Edward,

On 17 May 2015 at 01:33, Edward Sutton <edward.sutton at subsite.com> wrote:

>  Hi Ben,
>
>  I am having Objective-C problems trying to implement a message handler I
> added to your qisystemutils.mm when it calls NDHTMLtoPDF
> createPDFWithHTML.
>
>      https://github.com/benlau/quickios/blob/master/qisystemutils.mm  //
> <- This is great by the way!
>
>
>  I think I have callback block iissues when createPDFWithHTML is called.
>
>  *2015-05-16 11:51:10.692 widgets-to-html[86428:769940]
> -[QIOSApplicationDelegate window]: unrecognized selector sent to instance
> 0x7fa660547ef0*
> *2015-05-16 11:51:10.693 widgets-to-html[86428:769940] *** Terminating app
> due to uncaught exception 'NSInvalidArgumentException', reason:
> '-[QIOSApplicationDelegate window]: unrecognized selector sent to instance
> 0x7fa660547ef0'*
> **** First throw call stack:*
>
>  I suspect blocks and delegates are different ways to do same thing in
> Objective-C.  Can callback blocks be implemented in a static htmlToPdf
> method?
>
>
Sure. You could use block syntax in a static C++ style method call. The
qisystemutils.mm uses it heavily.


>  Do I need to implement a qiviewdelegate equivalent for NDHTMLtoPDF?
>
>
I am lazy to write delegate per class. So I make a single QIViewDelegate to
have protocol/interface for all classes and pass the result to Qt's code
via block syntax. QIViewDelegate is not a must for user , you may write
your own delegate class if needed.

So I think the problem is not using delegate or block , just your code did
something wrong.

Could you trace out which line of code throw the exception?



>
>  *File: *qisystemutils.mm
> <https://github.com/benlau/quickios/blob/master/qisystemutils.mm>
>
> ———————————
>
>
> NDHTMLtoPDF *m_htmlToPdfConverter = 0;
>
>
>   static bool htmlToPdf(QVariantMap& data) {
>
>         QString pathToHtmlFile = data["pathToHtmlFile"].toString();
>
>     QString pathToPdfFile = data["pathToPdfFile"].toString();
>
>         NSString* htmlAsBuiltPathDest = pathToHtmlFile.toNSString();
>
>     NSURL *baseUrl=[NSURL fileURLWithPath:htmlAsBuiltPathDest];
>
>         NSString* pdfOutputFilePath = pathToPdfFile.toNSString();
>
>         NSString *html = [NSString stringWithContentsOfFile:htmlAsBuiltPathDest encoding:NSUTF8StringEncoding error:nil];;
>
>     if (0 == html) {
>
>         return false;
>
>     }
>
>     // createPDFWithHTML is static method that returns NDHTMLtoPDF
>
>     m_htmlToPdfConverter = [NDHTMLtoPDF createPDFWithHTML:(NSString*)html
>
>                                                   baseURL:(NSURL*)baseUrl
>
>                                                pathForPDF:pdfOutputFilePath
>
>                                                  pageSize:kPaperSizeA4
>
>                                                   margins:UIEdgeInsetsMake(10, 5, 10, 5)
>
>                             successBlock:^(NDHTMLtoPDF *htmlToPDF)
>
>                             {
>
>                                 NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did succeed (%@ / %@)", htmlToPDF, htmlToPDF.PDFpath];
>
>                                 NSLog(@"%@",result);
>
>                             }
>
>                             errorBlock:^(NDHTMLtoPDF *htmlToPDF)
>
>                             {
>
>                                 NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did fail (%@)", htmlToPDF];
>
>                                 NSLog(@"%@",result);
>
>                             }];
>
>         NSLog(@"***call completed ***");
>
>         return true;
>
> }
>
>
>  *File: *NDHTMLtoPDF.mm
> ----------------------------------
>
>  /// A static method
> + (id)createPDFWithHTML:(NSString*)HTML baseURL:(NSURL*)baseURL
> pathForPDF:(NSString*)PDFpath pageSize:(CGSize)pageSize margins:(
> UIEdgeInsets)pageMargins successBlock:(NDHTMLtoPDFCompletionBlock)successBlock
> errorBlock:(NDHTMLtoPDFCompletionBlock)errorBlock
> {
>     NDHTMLtoPDF *creator = [[NDHTMLtoPDF alloc] initWithHTML:HTML baseURL:baseURL
> delegate:nil pathForPDF:PDFpath pageSize:pageSize margins:pageMargins];
>     creator.successBlock = successBlock;
>     creator.errorBlock = errorBlock;
>
>
>      return creator;
> }
>
>  - (id)initWithHTML:(NSString*)HTML baseURL:(NSURL*)baseURL delegate:(id <
> NDHTMLtoPDFDelegate>)delegate
>         pathForPDF:(NSString*)PDFpath pageSize:(CGSize)pageSize margins:(
> UIEdgeInsets)pageMargins
> {
>     if (self = [super init])
>     {
>         self.HTML = HTML;
>         self.URL = baseURL;
>         self.delegate = delegate;
>         self.PDFpath = PDFpath;
>
>
>          self.pageMargins = pageMargins;
>         self.pageSize = pageSize;
>
>          [self forceLoadView];
>     }
>     return self;
> }
>
>
>   //
>  //  NDHTMLtoPDF.h
>  //  Nurves
>  //
>  //  Created by Clement Wehrung on 31/10/12.
>  //  Copyright (c) 2012-2014 Clement Wehrung. All rights reserved.
>  //
>  //  Released under the MIT license
>
>  #import <UIKit/UIKit.h>
>
>  #define kPaperSizeA4 CGSizeMake(595.2,841.8)
>  #define kPaperSizeLetter CGSizeMake(612,792)
>
>  @class NDHTMLtoPDF;
>
>  typedef void (^NDHTMLtoPDFCompletionBlock)(NDHTMLtoPDF* htmlToPDF);
>
>  @protocol NDHTMLtoPDFDelegate <NSObject>
>
>  @optional
> - (void)HTMLtoPDFDidSucceed:(NDHTMLtoPDF*)htmlToPDF;
> - (void)HTMLtoPDFDidFail:(NDHTMLtoPDF*)htmlToPDF;
>  @end
>
>  @interface NDHTMLtoPDF : UIViewController <UIWebViewDelegate>
>
>  @property (nonatomic, copy) NDHTMLtoPDFCompletionBlock successBlock;
>  @property (nonatomic, copy) NDHTMLtoPDFCompletionBlock errorBlock;
>
>  @property (nonatomic, weak) id <NDHTMLtoPDFDelegate> delegate;
>
>  @property (nonatomic, strong, readonly) NSString *PDFpath;
>  @property (nonatomic, strong, readonly) NSData *PDFdata;
>
>  + (id)createPDFWithURL:(NSURL*)URL pathForPDF:(NSString*)PDFpath
> delegate:(id <NDHTMLtoPDFDelegate>)delegate pageSize:(CGSize)pageSize
> margins:(UIEdgeInsets)pageMargins;
> + (id)createPDFWithHTML:(NSString*)HTML pathForPDF:(NSString*)PDFpath
> delegate:(id <NDHTMLtoPDFDelegate>)delegate pageSize:(CGSize)pageSize
> margins:(UIEdgeInsets)pageMargins;
> + (id)createPDFWithHTML:(NSString*)HTML baseURL:(NSURL*)baseURL
> pathForPDF:(NSString*)PDFpath delegate:(id <NDHTMLtoPDFDelegate>)delegate
> pageSize:(CGSize)pageSize margins:(UIEdgeInsets)pageMargins;
>
>  + (id)createPDFWithURL:(NSURL*)URL pathForPDF:(NSString*)PDFpath
> pageSize:(CGSize)pageSize margins:(UIEdgeInsets)pageMargins successBlock:(
> NDHTMLtoPDFCompletionBlock)successBlock errorBlock:(
> NDHTMLtoPDFCompletionBlock)errorBlock;
> + (id)createPDFWithHTML:(NSString*)HTML pathForPDF:(NSString*)PDFpath
> pageSize:(CGSize)pageSize margins:(UIEdgeInsets)pageMargins successBlock:(
> NDHTMLtoPDFCompletionBlock)successBlock errorBlock:(
> NDHTMLtoPDFCompletionBlock)errorBlock;
> + (id)createPDFWithHTML:(NSString*)HTML baseURL:(NSURL*)baseURL
> pathForPDF:(NSString*)PDFpath pageSize:(CGSize)pageSize margins:(
> UIEdgeInsets)pageMargins successBlock:(NDHTMLtoPDFCompletionBlock)successBlock
> errorBlock:(NDHTMLtoPDFCompletionBlock)errorBlock;
>  @end
>
>  Sorry for my Objective-Confusion.  I appreciate the help I have received
> from Robert and Ben.
>
>  Thanks in advance for any suggestions,
>
>  -Ed
>
>
>
>
>
>  On May 15, 2015, at 7:29 AM, Robert Iakobashvili <coroberti at gmail.com>
> wrote:
>
>     Dear Ed,
>  There is an example attached to this
>  bug report:
>
> https://bugreports.qt.io/browse/QTBUG-42705
>
>  The attached to the bug example code is about
>  mixing Qt C++ and Obj-C code and sharing text.
>
>  May be it could be helpful.
>
>  Take care.
>
>  Regards,
> Robert
>
> On Fri, May 15, 2015 at 3:26 PM, Edward Sutton <edward.sutton at subsite.com>
> wrote:
>
>> Hi Ben,
>>
>>  Thank you very much for sharing your example.
>>
>>  https://github.com/benlau/quickios/blob/master/qisystemutils.mm
>>
>>  Understanding the Objective-C language has been much more difficult
>> than I expected coming from C, C++, and C#.
>>
>>  QMessageBox looks horrible on iOS.  I am very glad you have a solution
>> for using UIAlertView in your alertViewCreate method.
>>
>>  UiActionSheet and UIImagePickerController
>>
>> This has given me much to study and understand.
>>
>>  Thank you again for sharing your iOS code!
>>
>>  -Ed
>>
>>
>>
>>  On May 15, 2015, at 2:33 AM, Ben Lau <xbenlau at gmail.com> wrote:
>>
>>  Hi Edward,
>>
>>  You may find example code from my project.
>>
>>  quickios/qisystemutils.mm at master · benlau/quickios
>> <https://github.com/benlau/quickios/blob/master/qisystemutils.mm>
>>
>>  It has implemented an interface to create and communicate to
>> UIAlertView , UIActionSheet , UIImagePickerController etc. It uses a
>> singleton object called SystemMessenger as the bridge between C++ and
>> Objective-C++ code. So that the developed code can be run on non-iOS
>> platform too  (for development use)
>>
>>
>> On 15 May 2015 at 05:53, Edward Sutton <edward.sutton at subsite.com> wrote:
>>
>>> Can anyone share an example on how to implement the asynchronous return
>>> of a result from an Objective-C callback block to Qt?
>>>
>>> For example, see the “successBlock:” below which gets called back
>>> asynchronously.
>>>
>>>     self.PDFCreator = [NDHTMLtoPDF createPDFWithHTML:(NSString*)html
>>>                                              baseURL:(NSURL*)baseUrl
>>>
>>> pathForPDF:[@"~/Documents/hello-world.pdf" stringByExpandingTildeInPath]
>>>                                             pageSize:kPaperSizeA4
>>> margins:UIEdgeInsetsMake(10, 5, 10, 5)
>>>     successBlock:^(NDHTMLtoPDF *htmlToPDF) {
>>>
>>>         NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did
>>> succeed (%@ / %@)", htmlToPDF, htmlToPDF.PDFpath];
>>>         NSLog(@"%@",result);
>>>         self.resultLabel.text = result;
>>>
>>>     }
>>>     errorBlock:^(NDHTMLtoPDF *htmlToPDF) {
>>>         NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did
>>> fail (%@)", htmlToPDF];
>>>         NSLog(@"%@",result);
>>>         self.resultLabel.text = result;
>>>     }];
>>>
>>>
>>> Background:
>>> ————————
>>>
>>> I need to implement native iOS code to replace the missing QPrinter
>>> functionality I used to convert HTML to a PDF report on Android, OS X,
>>> Linux, and Windows.
>>>
>>> Unfortunately the Qt html-to-pdf functionality is implemented in
>>> QPrinter.  This functionality is gone from Qt iOS since iOS has no printer
>>> device support,  even though I am not accessing a printer device from iOS.
>>>
>>> I found this project which seems to work:
>>>
>>>         https://github.com/iclems/iOS-htmltopdf
>>>
>>> Objective-C interfacing to C++ is challenging.
>>>
>>> Thanks in advance for any tips,
>>>
>>> -Ed
>>> This email and any files transmitted with it from The Charles Machine
>>> Works, Inc. are confidential and intended solely for the use of the
>>> individual or entity to which they are addressed. If you have received this
>>> email in error please notify the sender. Our company accepts no liability
>>> for the contents of this email, or for the consequences of any actions
>>> taken on the basis of the information provided, unless that information is
>>> subsequently confirmed in writing. Please note that any views or opinions
>>> presented in this email are solely those of the author and do not
>>> necessarily represent those of the company. Finally, the recipient should
>>> check this email and any attachments for the presence of viruses. The
>>> company accepts no liability for any damage caused by any virus transmitted
>>> by this email.
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org
>>> http://lists.qt-project.org/mailman/listinfo/interest
>>>
>>
>>
>> This email and any files transmitted with it from The Charles Machine
>> Works, Inc. are confidential and intended solely for the use of the
>> individual or entity to which they are addressed. If you have received this
>> email in error please notify the sender. Our company accepts no liability
>> for the contents of this email, or for the consequences of any actions
>> taken on the basis of the information provided, unless that information is
>> subsequently confirmed in writing. Please note that any views or opinions
>> presented in this email are solely those of the author and do not
>> necessarily represent those of the company. Finally, the recipient should
>> check this email and any attachments for the presence of viruses. The
>> company accepts no liability for any damage caused by any virus transmitted
>> by this email.
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
>  This email and any files transmitted with it from The Charles Machine
> Works, Inc. are confidential and intended solely for the use of the
> individual or entity to which they are addressed. If you have received this
> email in error please notify the sender. Our company accepts no liability
> for the contents of this email, or for the consequences of any actions
> taken on the basis of the information provided, unless that information is
> subsequently confirmed in writing. Please note that any views or opinions
> presented in this email are solely those of the author and do not
> necessarily represent those of the company. Finally, the recipient should
> check this email and any attachments for the presence of viruses. The
> company accepts no liability for any damage caused by any virus transmitted
> by this email.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150517/ddca239e/attachment.html>


More information about the Interest mailing list