iOS Question [Solved] How to implement a given SDK protocol callback in Obj-C?

JordiCP

Expert
Licensed User
Longtime User
I need to implement a callback function defined in a @protocol declaration of an SDK that I am using.

I managed to integrate the SDK and initialize it using inline Objective-C (I receive logs stating that it is 'working'), but need a clue (better two) of how to implement this callback in Objective C

According to their DOCUMENTATION, it says
"You may also customize actions using MOCAProximityActionsDelegate:"

B4X:
@protocol
@optional

// ... some other protocol functions that won't be used


// This is the one I want to use.
/*
 * Called when the app should execute a custom action.
 * @param customAttribute - user provided custom attribute
 * @return YES if the alert was shown to the user, NO otherwise.
 */
-(BOOL)action:(MOCAAction*)sender performCustomAction:(NSString*)customAttribute withSituation:(MOCAFireSituation)situation;

@end

How could I declare this function in inline Objective C so that I can process this call?

Thanks in advance!
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,

when you initialize the object you just need to set the delegate property:
B4X:
Object.eventsDelegate = self;
Now add the the method of the protocol to your Objc code:
B4X:
-(BOOL)action:(MOCAAction*)sender performCustomAction:(NSString*)customAttribute withSituation:(MOCAFireSituation)situation{

}

At some point, you probably want to call a B4i sub back, then use: https://www.b4x.com/android/forum/threads/call-b4i-sub-from-inline-objc.52528/#post-329006


Jan
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Thanks for the answer Jan, and sorry for the late reply!

This seems the way to go, but there are some details that I can't sort, possibly due to my inexperience

I'll try to summarize the best I can:

  • The main Class is MOCA. From it we can get MOCAProximityService, which is the one that has the delegates. There is an eventDelegate and an actionDelegate.
  • The same in Android is really simple (Just declaring that the app class implements de callback and declaring it with @override) but not in IOS
  • My current code is
B4X:
- (void)MocaInit {
 
    // Initialize MOCA SDK.
    BOOL mocaReady = [MOCA initializeSDK];
    if (!mocaReady) {
        NSLog(@"MOCA SDK initialization failed.");
    }else{
        NSLog(@"MOCA SDK initialization OK!!!");
        MOCAProximityService * theService = [MOCA proximityService];
        [theService actionsDelegate] = self;     // Compiler throws: "error: expression is not assignable" 
        //[theService eventsDelegate] = self;   //<-- we don't use this one, since the desired callback is defined in the MOCAProximityActionsDelegate and not the MOCAProximityEventsDelegate
    }       
}

What should I change to make in work? and also how to declare the callback function: Inside an @implementation block? Should I declare also @interface and/or that my app adheres to the defined protocol (from what I have read)... too many doubts

Just in case I attach the 3 header files (MOCAlib.h, MOCAProximityService.h and MOCAProximityDelegate.h) where . I'll give my eternal gratitude, a virtual beer or a donation (whatever you prefer) if I can get it solved.
 

Attachments

  • theHeaders.zip
    7.3 KB · Views: 305
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Just for others who will face a similar case while learning with trial and error, now it is solved.

Just had to change the line
B4X:
// [theService actionsDelegate] = self;  // Previous error line
[theService actionsDelegate : self];      // Correct one

Since according to the definition in the header file, in this case, it is defined as a function and not a property
B4X:
/**
 * Protocol to handle proximity experience actions.
 */
-(void)actionsDelegate: (id<MOCAProximityActionsDelegate>) delegate;
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Yes, the original problem regarding the thread title is now solved
Now the event is fired and I can call my B4I sub passing 'customAttribute' as a param.
B4I
B4X:
Sub MOCABeaconDetected(identifier As String)
    Log("Identifier: "&identifier)
End Sub

And this is in the Inline Obj-C part
B4X:
-(BOOL)action:(MOCAAction*)sender performCustomAction:(NSString*)customAttribute withSituation:(MOCAFireSituation)situation{
    [self.bi raiseEvent:nil event:@"mocabeacondetected:" params:@[customAttribute]];
    return TRUE;
}

However, I have a doubt: I only need the 'customAttirbute' param for my project, but I'm curious how to interpret/read/parse the rest ('withSituationMOCAFireSituation)situation')
 
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,
but I'm curious how to interpret/read/parse the rest ('withSituationMOCAFireSituation)situation')
although situation is declared as a MOCAFireSituation object you can handle it as an integer. This type is defined in MOCAProximityService:

B4X:
typedef NS_ENUM(NSInteger, MOCAFireSituation) {
    // Indicates that an action is fired automatically by a proximity trigger
    // when the app is foreground or background.
    MOCAFiredByProximity,
    // Indicates that an action is fired by a user clicking a push notification
    // generated previously by proximity action fired in background.
    MOCAFiredByPushClicked
};

Probably MOCAFiredByProximity has the value 0 and MOCAFiredByPushClicked 1, but you can try this out.

Jan
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…