Unity3D-iOS calls the native application details interface (which pops up directly in the game)

Keywords: Unity iOS

It could have been through Unity Application.OpenUrl However, there is another way above iOS6 to pop up the evaluation box in the application

Then I added a function to the previous NativeShare, and it can be used.

Look at the previous code here: Unity3D - Implementation of calling IOS native sharing 

The first is the. h file:

#import <UIKit/UIKit.h>
#Import < storekit / storekit. H > / / introduce the pop-up Library

@interface GJCSocialShare : NSObject<SKStoreProductViewControllerDelegate>//To implement the callback of the pop-up box, there will be a callback when canceling, which must be added

+ (id) sharedInstance;

- (void) nativeShare:(NSString*)text media: (NSString*) media;
- (void) showStoreProductInApp:(NSString *)appID;//Open the corresponding application details by passing in an appID
- (void) productViewControllerDidFinish:(SKStoreProductViewController *)viewController;//Callback when the pop-up box is closed

@end

Those added with comments at the back are all new ones

Secondly. mm file:

...

- (void)showStoreProductInApp:(NSString *)appID{//Method implementation
    Class isAllow = NSClassFromString(@"SKStoreProductViewController");
    if (isAllow != nil) {
        SKStoreProductViewController *storeVC = [[SKStoreProductViewController alloc] init];
        [storeVC.view setFrame:CGRectMake(0, 200, 320, 200)];
        [storeVC setDelegate:self];
        [storeVC loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: appID}
                           completionBlock:^(BOOL result, NSError *error) {
                               if (result) {
                                   NSLog(@"show component: true");
                                   UnitySendMessage("GJCNativeShare", "OnOpenAppStoreSuccess", [GJC_DataConvertor NSStringToChar:@"Success"]);
                                   UIViewController *vc =  UnityGetGLViewController();
                                   [vc presentViewController:storeVC animated:YES completion:nil];
                                }else{
                                    NSLog(@"error:%@",error);
                                    UnitySendMessage("GJCNativeShare", "OnOpenAppStoreFailed", [GJC_DataConvertor NSStringToChar:error.localizedDescription]);
                                }
                            }];
    }else{
        NSLog(@"Not support this controller, use openUrl to AppStore!");
        UnitySendMessage("GJCNativeShare", "OnOpenAppStoreSuccessURL", [GJC_DataConvertor NSStringToChar:@"Success on url"]);
        //System versions lower than iOS6 do not have this class and do not support this function
        NSString *string = [NSString stringWithFormat:@"https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8",appID];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    }
}
//Callback
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    NSLog(@"show component finished");
    UnitySendMessage("GJCNativeShare", "OnOpenAppStoreFinished", [GJC_DataConvertor NSStringToChar:@"Finished"]);
    //In the proxy method, miss is the VC
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

...

Thirdly, add an interface to the end of the. mm file that can be called by the Unity side

extern "C" {
    void _GJC_OpenAppStore(char* appId) {
        NSString *appID = [GJC_DataConvertor charToNSString:appId];
        [[GJCSocialShare sharedInstance] showStoreProductInApp:appID];
    }
}


OK, the original side is finished.

Next, take a look at the. cs file of Unity. We also need to add the corresponding method:

public class GJCNativeShare : MonoBehaviour {
	#if UNITY_IPHONE && !UNITY_EDITOR

	[DllImport ("__Internal")]
	private static extern void _GJC_OpenAppStore(string appID);//---add---
	#endif

	public delegate void OnNativeEvent(string msg);

	public OnNativeEvent onOpenAppStore = null;

	//---add---
	private void OnNativeShareCancel(string result){
		// Debug.Log("cancel: " + result);
		if (onShareCancel != null){
			onShareCancel(result);
		}
	}
	//After loading, display interface / / - -- add---
	private void OnOpenAppStoreSuccess(string result){
		Debug.Log("Success: " + result);
		if (onOpenAppStore != null){
			onOpenAppStore("Success");
		}
	}
	//The interface is closed, whether it is cancel or other operations / / - -- add---
	private void OnOpenAppStoreFinished(string result){
		Debug.Log("Finished: " + result);
		if (onOpenAppStore != null){
			onOpenAppStore("Finished");
		}
	}
	//If the iOS version does not support it, it will automatically jump to the AppStore and return / / - -- add---
	private void OnOpenAppStoreSuccessURL(string result){
		Debug.Log("Success: " + result);
		if (onOpenAppStore != null){
			onOpenAppStore("Success on url");
		}
	}
	//Loading failed, problem / / - -- add---
	private void OnOpenAppStoreFailed(string result){
		Debug.Log("Failed: " + result);
		if (onOpenAppStore != null){
			onOpenAppStore("Failed");
		}
	}
}


Then, how to add calling methods to any script:

public class Main : MonoBehaviour {
	void Start()
	{
		GJCNativeShare.Instance.onOpenAppStore = OnNativeEvent;
	}
	public void OnOpenAppStore(){
		GJCNativeShare.Instance.OpenAppStore("1251737937");
	}
	void OnNativeEvent(string msg){
		Debug.Log("OnNativeEvent: " + msg);
		//--- your code
	}
}



Open the result as like as two peas in the AppStore, which are exactly the same as the box that pops up.

I opened it with the previous online application, which can be displayed.

In addition, it should be noted that on the Unity side, you can select the native. h or. mm file and select it from the right border StoreKit.frameword Tick, like this:


The Social check means that only the native sharing component is used. The function I added on the original basis should also be checked.




Posted by emmbec on Tue, 14 Jul 2020 08:40:25 -0700