解码收据Swift

时间:2019-02-03 08:49:28

标签: swift parsing in-app-purchase decode receipt-validation

这是我希望获得应用内购买的收据作为我的应用的一部分。它回到我的日志中说:“ Optional(5107 Bytes)”。因此,那里显然有一个收据文件,但我只是想找到一些小东西来解析/解码该收据,以访问这些字节中的信息。

代码(viewDidLoad()中的第一个函数,用于检查收据)

func checkReceiptFromAppStore() {
    let receipt = self.getReceipt()
    print("receipt Data is: \(receipt)") // prints this: receipt Data is: Optional(5107 bytes)
}

func getReceipt() -> Data? {
    if Bundle.main.appStoreReceiptURL != nil {
        print("app receipt: \(Bundle.main.appStoreReceiptURL)")
        do {
            let receiptData = try Data(contentsOf: Bundle.main.appStoreReceiptURL!)

            return receiptData
        } catch {
            print("error converting receipt to Data: \(error.localizedDescription)")
        }
    }
    return nil
}

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

收据是编码的二进制文件。无法轻易读取和解析您获得的public class machinemessage extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager)); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_tab, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; public PlaceholderFragment() { } /** * Returns a new instance of this fragment for the given section * number. */ public static tab.PlaceholderFragment newInstance(int sectionNumber) { tab.PlaceholderFragment fragment = new tab.PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_tab, container, false); TextView textView = (TextView) rootView.findViewById(R.id.section_label); textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); return rootView; } } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return tab.PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { // Show 3 total pages. return 3; } } }

文档:Locate and Parse the Receipt

在GitHub上搜索收据验证以获取实施示例。