//
//  NativePlugin.m
//  NativePlugin
//
//  Created by 初程程 on 2019/4/4.
//  Copyright © 2019年 初程程. All rights reserved.
//

#import "NativePlugin.h"

@interface NativePlugin()<RCTBridgeModule>
@end
@implementation NativePlugin
RCT_EXPORT_METHOD(gotoTmall:(NSString *)shopId){
    NSString *urlStr = [NSString stringWithFormat:@"taobao://shop.m.taobao.com/shop/shop_index.htm?shop_id=%@",shopId];
    
    [self openApplicationWithUrl:urlStr response:^(BOOL isSuccess) {
        if (!isSuccess) {
            [self openAppstoreWithAppId:@"387682726"];
        }
    }];
}
//跳转appstore
- (void)openAppstoreWithAppId:(NSString *)appid{
    NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/us/app/id%@",appid];
    
    [self openApplicationWithUrl:url response:^(BOOL isSuccess) {
        
    }];
}
//跳转系统设置，其他APP，打开safari的方法
- (void)openApplicationWithUrl:(NSString *)urlStr response:(void (^)(BOOL isSuccess))response{
    UIApplication *application = [UIApplication sharedApplication];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    BOOL isCanOpen = [application canOpenURL:url];
    
    if (isCanOpen) {
        [application openURL:url];
        if (response) {
            response(YES);
        }
    }else{
        if (response) {
            response(NO);
        }
    }
}
@end
