Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rhymelph committed Jul 27, 2021
1 parent d954804 commit 670acaf
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 0.3.5
* fix ios if not exist appId will crash error,add isChina params to [getVersionFrom][upgradeFromAppStore] method.
## 0.3.4
* fix 301/302 download error, larger then Android N network disconnect turn to connect download recovery.
## 0.3.3
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ enum NotificationStyle {
void upgradeFromAppStore() async {
bool isSuccess =await RUpgrade.upgradeFromAppStore(
'your AppId',//such as:WeChat AppId:414478124
false
);
print(isSuccess);
}
Expand All @@ -275,6 +276,7 @@ enum NotificationStyle {
void getVersionFromAppStore() async {
String versionName = await RUpgrade.getVersionFromAppStore(
'your AppId',//such as:WeChat AppId:414478124
false
);
print(versionName);
}
Expand Down
95 changes: 65 additions & 30 deletions ios/Classes/SwiftRUpgradePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ public class SwiftRUpgradePlugin: NSObject, FlutterPlugin {
result(FlutterError(code: "参数appId不能为空", message: nil, details: nil))
return
}
upgradeFromAppStore(appId: appId,result: result)
guard let isChina = (call.arguments as? Dictionary<String, Any>)?["isChina"] as? Bool else {
result(FlutterError(code: "参数isChina不能为空", message: nil, details: nil))
return
}
upgradeFromAppStore(appId: appId,isChina: isChina,result: result)
break;
case "getVersionFromAppStore":
print(call.arguments ?? "null")
guard let appId = (call.arguments as? Dictionary<String, Any>)?["appId"] as? String else{
result(FlutterError(code: "参数appId不能为空", message: nil, details: nil))
return
}
getVersionFromAppStore(appId: appId,result: result)
guard let isChina = (call.arguments as? Dictionary<String, Any>)?["isChina"] as? Bool else {
result(FlutterError(code: "参数isChina不能为空", message: nil, details: nil))
return
}
getVersionFromAppStore(appId: appId,isChina:isChina,result: result)
break;
default:
result(FlutterMethodNotImplemented)
Expand All @@ -55,42 +63,69 @@ public class SwiftRUpgradePlugin: NSObject, FlutterPlugin {
}

//跳转到应用的AppStore页页面
func upgradeFromAppStore(appId: String, result: @escaping FlutterResult) {
let dict = getInfoFromAppStore(appId: appId);
if((dict) != nil){
let res = dict!["results"] as! NSArray
let xx = res[0] as! NSDictionary
let urlString = xx["trackViewUrl"] as! String
result(openUrl(url: urlString))
}else{
func upgradeFromAppStore(appId: String, isChina: Bool,result: @escaping FlutterResult) {
getInfoFromAppStore(appId: appId,isChina: isChina) { dict in
if dict == nil{
result(false)
}else{
let res = dict!["results"] as! NSArray
let xx = res[0] as! NSDictionary
let urlString = xx["trackViewUrl"] as! String
result(self.openUrl(url: urlString))
}

} error: { error in
result(false)
}
}

//获取应用信息
func getInfoFromAppStore(appId:String) -> NSDictionary? {
let appUrl = "https://itunes.apple.com/lookup?id=" + appId
do{
let jsonData = NSData(contentsOf: NSURL(string: appUrl)! as URL)
if(jsonData == nil){
NSLog("获取appId:%@ 对应的appStore信息失败",appId)
return nil;
func getInfoFromAppStore(appId:String,isChina:Bool,complete:@escaping(_ result:NSDictionary?)->Void,error:@escaping(_ error:Error?)->Void) {
let checkLink = isChina == true ? "https://itunes.apple.com/cn/lookup?id=" : "https://itunes.apple.com/lookup?id="
let appUrl = checkLink + appId
let url = NSURL(string: appUrl)! as URL
let request = NSMutableURLRequest(url: url, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData, timeoutInterval: 30)
request.httpMethod = "GET"

let session = URLSession.shared

let task = session.dataTask(with: request as URLRequest){(data,response,err) in
if err == nil{
if data == nil{
NSLog("获取appId:%@ 对应的appStore信息失败",appId)
complete(nil)
}
let httpRequest = response as! HTTPURLResponse
if httpRequest.statusCode == 200 {
let json = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableLeaves) as! NSDictionary
if json["resultCount"] as! Int > 0{
complete(json)
}else{
NSLog("获取appId:%@ 对应的appStore信息失败,未上架或ID不存在", appId)
complete(nil)
}
}else{
NSLog("获取appId:%@ 对应的appStore信息失败,未上架或ID不存在", appId)
complete(nil)
}
}else{
NSLog("获取appId:%@ 对应的appStore信息失败",appId)
error(err)
}
}
let json = try JSONSerialization.jsonObject(with: jsonData! as Data, options: JSONSerialization.ReadingOptions.mutableLeaves) as! NSDictionary
return json;
}catch{
NSLog("获取appId:%@ 对应的appStore信息失败",appId)
}
return nil;
task.resume()
}

func getVersionFromAppStore(appId:String,result: @escaping FlutterResult){
let dict = getInfoFromAppStore(appId: appId)
if((dict) != nil){
let res = dict!["results"] as! NSArray
let xx = res[0] as! NSDictionary
result(xx["version"]! as! String)
}else{
func getVersionFromAppStore(appId:String,isChina:Bool,result: @escaping FlutterResult){
getInfoFromAppStore(appId: appId,isChina: isChina) {dict in
if dict != nil {
let res = dict!["results"] as! NSArray
let xx = res[0] as! NSDictionary
result(xx["version"]! as! String)
}else{
result(nil)
}
} error: { err in
result(nil)
}
}
Expand Down
10 changes: 8 additions & 2 deletions lib/r_upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,28 @@ class RUpgrade {
/// IOS
///
/// [appId] your appId in appStore
/// [isChina] if true ,will check this link https://itunes.apple.com/cn/lookup.
///
static Future<bool?> upgradeFromAppStore(String appId) async {
static Future<bool?> upgradeFromAppStore(String appId,
[bool isChina = true]) async {
assert(Platform.isIOS, 'This method only support ios application');
return await _methodChannel!.invokeMethod("upgradeFromAppStore", {
'appId': appId,
'isChina': isChina,
});
}

/// IOS
///
/// [id] your appId in appStore
/// [isChina] if true ,will check this link https://itunes.apple.com/cn/lookup.
///
static Future<String?> getVersionFromAppStore(String appId) async {
static Future<String?> getVersionFromAppStore(String appId,
[bool isChina = true]) async {
assert(Platform.isIOS, 'This method only support ios application');
return await _methodChannel!.invokeMethod("getVersionFromAppStore", {
'appId': appId,
'isChina': isChina,
});
}
}
Expand Down
40 changes: 20 additions & 20 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@ packages:
dependency: transitive
description:
name: async
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.15.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
flutter:
Expand All @@ -64,21 +64,21 @@ packages:
dependency: transitive
description:
name: matcher
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.0"
sky_engine:
Expand All @@ -90,56 +90,56 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
sdks:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: r_upgrade
description: A plugin for upgrade and install application ,Support Android and IOS.
version: 0.3.4
version: 0.3.5
homepage: https://github.com/rhymelph/r_upgrade

environment:
Expand Down

0 comments on commit 670acaf

Please sign in to comment.