Explorar o código

implement Swift iOS callback

Jonas Reinsch %!s(int64=7) %!d(string=hai) anos
pai
achega
97b7126675

+ 6 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		7A451D941FB1B1DB00177250 /* wrapper.c in Sources */ = {isa = PBXBuildFile; fileRef = 7A451D921FB1B1DB00177250 /* wrapper.c */; };
 		7A7923401FB0A14300BC2DE5 /* libiconv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 7A79233F1FB0A14300BC2DE5 /* libiconv.tbd */; };
 		7A79236A1FB0A2C800BC2DE5 /* compress.c in Sources */ = {isa = PBXBuildFile; fileRef = 7A79235D1FB0A2C800BC2DE5 /* compress.c */; };
 		7A79236B1FB0A2C800BC2DE5 /* create.c in Sources */ = {isa = PBXBuildFile; fileRef = 7A79235E1FB0A2C800BC2DE5 /* create.c */; };
@@ -87,6 +88,8 @@
 
 /* Begin PBXFileReference section */
 		6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_deltachat_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; };
+		7A451D921FB1B1DB00177250 /* wrapper.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = wrapper.c; sourceTree = "<group>"; };
+		7A451D931FB1B1DB00177250 /* wrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = wrapper.h; sourceTree = "<group>"; };
 		7A79233F1FB0A14300BC2DE5 /* libiconv.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libiconv.tbd; path = usr/lib/libiconv.tbd; sourceTree = SDKROOT; };
 		7A7923451FB0A2C800BC2DE5 /* config-netpgp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "config-netpgp.h"; sourceTree = "<group>"; };
 		7A7923461FB0A2C800BC2DE5 /* config-original-as-configured-for-ubuntu-16.04-64bit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "config-original-as-configured-for-ubuntu-16.04-64bit.h"; sourceTree = "<group>"; };
@@ -314,6 +317,8 @@
 				7A9FB14C1FB061E2001FEA36 /* LaunchScreen.storyboard */,
 				7A9FB14F1FB061E2001FEA36 /* Info.plist */,
 				7A9FB1561FB06540001FEA36 /* deltachat-ios-Bridging-Header.h */,
+				7A451D921FB1B1DB00177250 /* wrapper.c */,
+				7A451D931FB1B1DB00177250 /* wrapper.h */,
 			);
 			path = "deltachat-ios";
 			sourceTree = "<group>";
@@ -621,6 +626,7 @@
 				7A79236C1FB0A2C800BC2DE5 /* crypto.c in Sources */,
 				7A9FB5461FB08557001FEA36 /* mrmailbox_e2ee.c in Sources */,
 				7A9FB54A1FB08557001FEA36 /* mrmimefactory.c in Sources */,
+				7A451D941FB1B1DB00177250 /* wrapper.c in Sources */,
 				7A9FB54D1FB08557001FEA36 /* mrosnative.c in Sources */,
 				7A9FB5471FB08557001FEA36 /* mrmailbox_imex.c in Sources */,
 				7A9FB53C1FB08557001FEA36 /* mrcmdline.c in Sources */,

+ 26 - 1
deltachat-ios/AppDelegate.swift

@@ -8,6 +8,27 @@
 
 import UIKit
 
+
+@_silgen_name("callbackSwift")
+
+public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLong, data1String: UnsafePointer<Int8>, data2String: UnsafePointer<Int8>) -> CUnsignedLong {
+    
+    switch event {
+    case MR_EVENT_INFO:
+        let s = String(cString: data2String)
+        print("Info: \(s)")
+    case MR_EVENT_WARNING:
+        let s = String(cString: data2String)
+        print("Warning: \(s)")
+    case MR_EVENT_ERROR:
+        let s = String(cString: data2String)
+        print("Error: \(s)")
+    default:
+        break
+    }
+    return 0
+}
+
 extension String {
     var nullTerminated: Data? {
         if var data = self.data(using: String.Encoding.utf8) {
@@ -43,7 +64,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
         
         // TODO: - add callback as first parameter
         //       - second param remains nil (user data for more than one mailbox)
-        guard let m = mrmailbox_new(nil, nil) else {
+        
+
+        guard let m = mrmailbox_new(callback_ios, nil) else {
             fatalError("Error: mrmailbox_new returned nil")
         }
         let mailbox = m.pointee
@@ -57,6 +80,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
 
         let r = mrmailbox_open(m, dbfile, nil)
         print(r)
+        
+//        mrmailbox_c
 
 //        dbfile.withCString {
 //            (cString:UnsafePointer<Int8>) in

+ 1 - 1
deltachat-ios/deltachat-ios-Bridging-Header.h

@@ -3,4 +3,4 @@
 //
 
 #include "mrmailbox.h"
-
+#include "wrapper.h"

+ 18 - 0
deltachat-ios/wrapper.c

@@ -0,0 +1,18 @@
+//
+//  wrapper.c
+//  deltachat-ios
+//
+//  Created by Jonas Reinsch on 07.11.17.
+//  Copyright © 2017 Jonas Reinsch. All rights reserved.
+//
+
+#include "wrapper.h"
+
+long callbackSwift(int, long, long, const char*, const char*);
+
+
+ uintptr_t callback_ios(mrmailbox_t* mailbox, int event, uintptr_t data1, uintptr_t data2)
+{
+    callbackSwift(event, data1, data2, (const char*)data1, (const char*)data2);
+    return 0;
+}

+ 20 - 0
deltachat-ios/wrapper.h

@@ -0,0 +1,20 @@
+//
+//  wrapper.h
+//  deltachat-ios
+//
+//  Created by Jonas Reinsch on 07.11.17.
+//  Copyright © 2017 Jonas Reinsch. All rights reserved.
+//
+
+#ifndef wrapper_h
+#define wrapper_h
+
+#include <stdio.h>
+#include "mrmailbox.h"
+
+// typedef uintptr_t (*mrmailboxcb_t) (mrmailbox_t*, int event, uintptr_t data1, uintptr_t data2);
+
+
+uintptr_t callback_ios(mrmailbox_t* mailbox, int event, uintptr_t data1, uintptr_t data2);
+
+#endif /* wrapper_h */