Flutter 调取App Store评分的三种方式

2021-03-09  本文已影响0人  溜萝卜

参照上一篇文章:创建一个Flutter与iOS原生交互插件创建FlutterLauncher Flugin

 static const MethodChannel _channel = const MethodChannel('flutter_launcher');

目前iOS支持三种App Store评分方式

1.通过App打开网页,跳转到AppStore编辑评论,可评分,可评论。
  static void launch({String androidAppId, String iOSAppId, bool writeReview = true}) async {
    await _channel.invokeMethod(
        'launch', {'android_id': androidAppId, 'ios_id': iOSAppId, 'write_review': writeReview});
        FlutterLauncher.launch(
                  androidAppId: "com.xxxx.com",
                  iOSAppId: "585027354",
                  writeReview: true);
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
if ([@"launch" isEqualToString:call.method]) {
        NSString *appId = call.arguments[@"ios_id"];

        if (appId == (NSString *)[NSNull null]) {
            result([FlutterError errorWithCode:@"ERROR"
                                     message:@"App id cannot be null"
                                     details:nil]);
        } else if ([appId length] == 0) {
            result([FlutterError errorWithCode:@"ERROR"
                                     message:@"Empty app id"
                                     details:nil]);
        } else {
            NSString *iTunesLink;
            if ([call.arguments[@"write_review"] boolValue]) {
              iTunesLink = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@?action=write-review", appId];
            } else {
              iTunesLink = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@", appId];
            }

            NSURL* itunesURL = [NSURL URLWithString:iTunesLink];
            if ([[UIApplication sharedApplication] canOpenURL:itunesURL]) {
              [[UIApplication sharedApplication] openURL:itunesURL];
            }

            result(nil);
        }
    }
2. iOS 6.0以后 在App内部加载AppStore 展示App信息

实现:

static void inAppPresentStore({String androidAppId, String iOSAppId}) async {
    _channel.invokeMethod(
        'inAppPresentStore', {'android_id': androidAppId, 'ios_id': iOSAppId});
  }

FlutterLauncher.inAppPresentStore(androidAppId:"com.xxxx.com",iOSAppId: "585027354" );

if ([@"inAppPresentStore" isEqualToString:call.method]){
          
        NSString *appId = call.arguments[@"ios_id"];

       __weak UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController ;
          SKStoreProductViewController *storeProductViewController = [[SKStoreProductViewController alloc] init];
          [storeProductViewController setDelegate:self];
          [storeProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier : appId}
                                                completionBlock:^(BOOL result, NSError *error) {
                                                    if (error) {
                                                        NSLog(@"Error %@ with User Info %@.", error, [error userInfo]);
                                                    } else {
                                                        [rootVC presentViewController:storeProductViewController animated:YES completion:nil];
                                                    }
                                                }];
        
    }
@interface FlutterLauncherPlugin ()<SKStoreProductViewControllerDelegate>

@end
#pragma mark - SKStoreProductViewControllerDelegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0)
{
    __weak UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController ;
    
    [rootVC dismissViewControllerAnimated:YES completion:nil];
}

@end
3. iOS 10.3以后可以在APP应用内评分,调用系统接口,不需要跳转

实现:

 static void inAppLaunch() async {
    await _channel.invokeMethod(
        'inAppLaunch', {});
  }

  FlutterLauncher.inAppLaunch();
if ([@"inAppLaunch" isEqualToString:call.method]){
        if (@available(iOS 10.3, *)) {
            [SKStoreReviewController requestReview];
        } else {
            // Fallback on earlier versions
        }
    }

that's all~

上一篇 下一篇

猜你喜欢

热点阅读