Skip to content

Commit

Permalink
fix: make version install work on ios
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Nov 22, 2021
1 parent b54bf59 commit 8099d44
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
41 changes: 19 additions & 22 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ import Foundation
import SSZipArchive
import Just

extension FileManager {
open func secureCopyItem(at srcURL: URL, to dstURL: URL) -> Bool {
do {
if FileManager.default.fileExists(atPath: dstURL.path) {
try FileManager.default.removeItem(at: dstURL)
}
try FileManager.default.copyItem(at: srcURL, to: dstURL)
} catch (let error) {
print("Cannot copy item at \(srcURL) to \(dstURL): \(error)")
return false
}
return true
extension URL {
var isDirectory: Bool {
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
}
}

Expand All @@ -24,35 +15,41 @@ extension FileManager {
return String((0..<length).map{ _ in letters.randomElement()! })
}

@objc public func updateApp(url: URL) -> Bool {
@objc public func updateApp(url: URL) -> URL? {
print(url)
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destZip = documentsUrl.appendingPathComponent(randomString(length: 10))
let dest = documentsUrl.appendingPathComponent(randomString(length: 10))
let publicFolder = documentsUrl.appendingPathComponent("public")
let index = dest.appendingPathComponent("index.html")
let r = Just.get(url)
if r.ok {
if (FileManager.default.createFile(atPath: destZip.path, contents: r.content, attributes: nil)) {
print("File created successfully.", destZip.path)
SSZipArchive.unzipFile(atPath: destZip.path, toDestination: dest.path)
do {
let files = try FileManager.default.contentsOfDirectory(atPath: dest.path)
print(files)
for file in files {
let urlFile = URL.init(string: file)!
FileManager.default.secureCopyItem(at: urlFile, to: publicFolder)
if (files.count == 1 && dest.appendingPathComponent(files[0]).isDirectory && !FileManager.default.fileExists(atPath: index.path)) {
return dest.appendingPathComponent(files[0])
} else if (FileManager.default.fileExists(atPath: index.path)) {
return dest
}
} catch {
print("Error getting zip files")
return false
print("FILE NOT AVAILABLE" + index.path)
return nil
}
do {
try FileManager.default.removeItem(atPath: dest.path)
} catch {
print("File not removed.")
return nil
}
return true
return dest
} else {
print("File not created.")
}
} else {
print("Error downloading zip file", r.error)
}
return false
return nil
}
}
8 changes: 5 additions & 3 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
let url = URL(string: call.getString("url") ?? "")

let res = implementation.updateApp(url: url!)
if (res) {
if ((res) != nil) {
print("PATH " + res!.path)
DispatchQueue.main.async {
self.bridge?.viewController?.viewDidLoad()
let vc = self.bridge?.viewController as! CAPBridgeViewController
vc.setServerBasePath(path: res!.path)
}
call.resolve([
"done": res
"done": res!.path
])
}
call.reject("error")
Expand Down
4 changes: 4 additions & 0 deletions ios/Plugin/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
</dict>
</plist>

0 comments on commit 8099d44

Please sign in to comment.