0

How to write the data from Web API to a file and how to read data from that file if no internet?

public static boolean isConnectingToInternet(Context _context) {
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }
2
  • You need at least once your app run over internet, And than save your file in NSUserDefault, So that you can access it as offline mode. Commented Feb 6, 2017 at 9:14
  • I suggest you do not write it to the NSUserDefault but just to a file in the document directory. The NSUserDefault is not ment to hold large chucks of data but just small settings. Commented Feb 6, 2017 at 9:37

1 Answer 1

0

You can not request a web service in offline mode. What is the best you can do is, you can apply client cash(setting max-age, s-maxage , max-stale in request header) or simply save the data from your web api in a local database like sqlite or Realm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.