我很难让 MFMessageCompose Unity3D iOS Plugin一起使用。

我有一个警报窗口弹出并带有按钮,但是访问MFMessageComposer时出现错误。似乎无法获得正确弹出窗口的方法。

这是我的 iOSBridge.h (如果需要,可以链接文件):

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>

@interface Delegate : NSObject <UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate>

@end

还有我的iOSBridge.mm文件:
#import "iOSBridge.h"

@implementation Delegate


//Trying to still understand the meaning behind this line.  Why???
-(id)init
{
    return self;
}

//RATE US Button Numbers
//Not entirely sure What I did here but it still bring it up.  This section has nothing to do with SMS
-(void) alertView: (UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //Give back the number of the button

    NSString *inStr = [NSString stringWithFormat:@"%d", (int)buttonIndex];
    const char *cString = [inStr cStringUsingEncoding:NSASCIIStringEncoding];
    UnitySendMessage("Popup", "UserFeedBack", cString);
    NSLog(@"%li", (long)buttonIndex);
} // RATE US button number end

-(void)SMS:(id)sender{

    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    [controller setMessageComposeDelegate:self];

    if([MFMessageComposeViewController canSendText]){

        [controller setRecipients:[NSArray arrayWithObjects:nil]];
        [controller setBody:[NSString stringWithFormat:@"Text"]];

        //WHYYY do you not work
        //[self presentViewController:controller animated:YES];

        Delegate *appDelegate = UIApplication.sharedApplication.delegate;
        [appDelegate.window.rootViewController presentViewController:controller animated:YES completion:nil]
    }

}

@end

static Delegate* delegateObject;

extern "C"
{

//A method for unity can run
void _AddNotification(const char* title,
                  const char* body,
                  const char* cancelLabel,
                  const char* firstLabel,
                  const char* secondLabel)

{

//Don't have a full grasp of this delegateObject thing yet.
if(delegateObject ==nil){
    delegateObject = [[Delegate alloc]init];
}

//iOS Alert Pop up view RATE OUR GAME

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: [NSString stringWithUTF8String:title]
                      message:[NSString stringWithUTF8String:body] delegate:delegateObject
                      cancelButtonTitle:[NSString stringWithUTF8String:cancelLabel]
                      otherButtonTitles:[NSString stringWithUTF8String:firstLabel],[NSString stringWithUTF8String:secondLabel], nil];


    [alert show];
} //End of _AddNotification


//SMS Method for Unity to use
void _SMSGO(const char* Mbody){

        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        if([MFMessageComposeViewController canSendText]){

            NSString *s = [NSString stringWithFormat:@"%s", Mbody];
            controller.body = s;

            //Suppose to Brings up the SMS view not sure why it isnt working or how to make this work
            //If this can work its major progress

            [delegateObject presentViewController:controller animated:YES];
        }
    }
}

最佳答案

所以这些:

-(void) alertView: (UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *inStr = [NSString stringWithFormat:@"%d", (int)buttonIndex];
    const char *cString = [inStr cStringUsingEncoding:NSASCIIStringEncoding];
    UnitySendMessage("Popup", "UserFeedBack", cString);
    NSLog(@"%li", (long)buttonIndex);
}

-(void)SMS:(id)sender{

    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    [controller setMessageComposeDelegate:self];

    if([MFMessageComposeViewController canSendText]){

        [controller setRecipients:[NSArray arrayWithObjects:nil]];
        [controller setBody:[NSString stringWithFormat:@"Text"]];

        //WHYYY do you not work
        //[self presentViewController:controller animated:YES];

        Delegate *appDelegate = UIApplication.sharedApplication.delegate;
        [appDelegate.window.rootViewController presentViewController:controller animated:YES completion:nil]
    }
}

实际上,您的网桥根本无法访问代码块。因此,在此一无所获。
- (id) init是该类的初始化程序。因此,为了从其他任何文件访问该文件中的任何文件,您必须先对其进行init编码。考虑这一点的更简单方法是:
extern.c是一个单独的文件。
static Delegate *delObj;

extern "C"
{
    void _callMain (const char *hello) {
        /**
         * delObj is a static so it "Will be once it is" so it will be
         * in memory (mallocated) the moment you tell it to be something.
         */
        if (!delObj) {

在这种情况下,我们希望该静态对象成为Delegate的实例(这不是将类设置为一个特别好的名称,这会引起很多混乱)。我们通过调用init来“启动”或初始化该类,并告诉它这是我们需要的对象,将其放在 read this上。
        delObj = [[Delegate alloc] init];
    }

    // Now we can call the function in the next file
    [delObj helloWorld];
}
Delegate.m-将此视为另一个文件。您可以从extern.c访问此文件,因为(实际上)它只是包含在同一文件中。但这完全是一个单独的实体。您必须从extern.c访问此“文件”
@implementation Delegate

// This is the function we call with [[Delegate alloc] init];
-(id) init {
    // It simply returns itself - saying hey this is the object memory chunk in the heap you want to talk to.
    return self;
}

// This is the function we call with [delObj helloWorld];
- (void) helloWorld {
    NSLog("Hello World"); // This will show up in your console.
}

因此,上面的代码有两个函数,一个说“这是您要寻找的内存对象”,另一个说“我是一个函数,让我们执行一些事情”。

现在,所有布置都没有,您不会打电话:
-(void) alertView: (UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex


-(void)SMS:(id)sender{

通过您的extern "C"_AddNotification起作用的原因是因为您在那里有代码调用UIAlertView ,之后来确保您的delegateObject在内存中。在_SMSGO函数中,您没有将对象放入内存。因此,当您在delegateObject中调用[delegateObject presentViewController:controller animated:YES];时,它会说:“哦,我知道delegateObject是什么,但是它什么都没有。实际上,它没有执行任何内容的内存!”这就是为什么您可能没有任何错误或异常的原因。该代码知道有一个静态对象,但尚未将其设置为任何对象。

解决方案:

首先,除非您确实想要发送某些内容,否则void方法不需要:(id)sender就是更多的IBAction东西-因此,请将-(void)SMS:(id)sender{更改为- (void) SMS {(也要留出漂亮的间距。这对眼睛来说更好)。

其次,将SMS中的所有内容交换为:
- (void) SMS {

    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    [controller setMessageComposeDelegate:self];

    if([MFMessageComposeViewController canSendText]){

        [controller setRecipients:[NSArray arrayWithObjects:nil]];
        [controller setBody:[NSString stringWithFormat:@"Text"]];

        // We now know why we work!
        [self presentViewController:controller animated:YES];
    }
}

可是等等!并且不要复制粘贴。确保了解您编写的每一行代码。

这行对象分配到内存中,在本例中为MFMessageComposeViewController对象。然后初始化它。
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];

这行代码设置了我们刚创建的对象的代表(controller)。但是为什么可以做到这一点?因为我们在MFMessageComposeViewControllerDelegate文件中设置了iOSBridge.h。这告诉.mm文件中的所有内容“嘿,如果我们碰巧在某个地方有一个MFMessage对象,则可以根据需要访问委托属性。”[controller setMessageComposeDelegate:self];if ( [MFMessageComposeViewController canSendText] )是人类可读的。

[controller setRecipients:[NSArray arrayWithObjects:nil]];[controller setBody:[NSString stringWithFormat:@"Text"]];相同

但是现在[self presentViewController:controller animated:YES];是我们需要研究的地方。

因此,在Unity中,他们说:“嘿,我们不需要处理每个可能调用的本机函数”,因此他们为您创建了对extern功能的访问。这使您可以从UnityScript方面与Objective-C进行对话。 Objective-C只是C的超集,因此是共同点。此处的所有内容仅应调用Delegate中的函数。上面的示例在伪造的extern.c代码块中。唯一要做的就是创建delegateObject并调用一个函数。现在进行下一步。用以下命令交换_SMSGO中的所有内容:
void _SMSGO (const char *mBody) { // I prefer char *name vs char* name but it doesn't matter

    [delegateObject SMS]; // add mBody as a parameter when you're ready. Handle the stringWithUTF8String on the Objective-C side of things. Yes, this is not the Obj-C side, this is the C side.

}

“但是等等……您只是delegateObject才是必需品”-是的。在您的extern "C"部分中创建另一个方法称为_init,并在 class 的AwakeStart函数中从Unity运行一次 。这将设置您的静态对象一次,再也不会对其造成混乱。只需使其:
void _init() {
    if ( !delegateObject ) {
        delegateObject = [[Delegate alloc] init];
    }
}
!delegateObjectdelegateObject == nil相同,但代码更简洁。双方都说“我们不知道dat是什么”-好的,现在我们有了delegateObject,我们可以随意使用delegateObject进行下一次调用。

现在,当您调用[delegateObject SMS]时,它将转到- (void) SMS。而且,当您调用self presentViewController时,它会知道self是指 iOS本机对象。如果您在presentViewController中调用extern "C",它说:“...嗯,嗯。我还没有得到最模糊的含义。”-因此,我们将extern "C"部分中的全部用作Delegate对象的交叉点。那就是extern东西的全部目的,只是为了调用Obj-C东西中的东西。

希望这可以帮助。

关于ios - 使用Unity3D在iPhone上弹出SMS ViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36024759/

10-12 15:02