React native iOS version hot update

Source: Internet
Author: User

Benefits of react Native hot update

JS script code changed, such as a few changes to the UI, if the client wants to update, if you download the APK or IPA directly, one is a waste of traffic, and more trouble

Hot update just download the packaged bundle file and replace it.

The idea is relatively simple, the client and the server to maintain a bundle version information, if the server version than the client version of the new download, and then replace the re-rendering is OK

Specifically, if there is no hot update, the code that loads the bundle is like this

Jscodelocation = [[Rctbundleurlprovider sharedsettings] Jsbundleurlforbundleroot:@ "index.ios  [ Fallbackresource:nil];

If you add a hot update, this is the case

Nsurl *jscodelocation=Nil;  Jscodelocation=[self getbundle];     if (jscodelocation==Nil)  {    = [[Rctbundleurlprovider sharedsettings] Jsbundleurlforbundleroot:@ "index.ios"  Fallbackresource:nil];  }
The Getbundle function contains the logic to load the bundle acquired by the server

-(Nsurl *) getbundle{NSString* ss=@"#####";//address of the service-side bundle version numbernsstring* str =[self httpget:ss]; NSString*pathdocuments=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) ObjectAtIndex:0]; NSString*createpath=[nsstring stringWithFormat:@"%@/%@", Pathdocuments,@"Version.txt"];//complete path with file nameBoolean bb=false; Nsfilemanager*Defaultmanager; Defaultmanager=[Nsfilemanager Defaultmanager]; Nsurl*nsurl=Nil; if([[[Nsfilemanager Defaultmanager] Fileexistsatpath:createpath])//determine if the file already exists{nsstring* FileContents =[NSString Stringwithcontentsoffile:createpath encoding:nsutf8stringencoding Error:nil]; intA=[str Intvalue];//Service-side bundle version number    intB=[filecontents Intvalue];//Client Bundle version Information    if(A>B)//if the bundle version of the server is larger, remove the bundle from the client and update it to the server    {      if([[[Nsfilemanager Defaultmanager] Fileexistsatpath:createpath])//determine if the file already exists{[Defaultmanager removeitematpath:createpath error:null]; } [self StoreFile:@"Version.txt"CONTENT:STR]; BB=true; }    Else//If you find that the bundle file you downloaded does not exist, download the update{Createpath=[nsstring stringWithFormat:@"%@/%@", Pathdocuments,@"Main.jsbundle"]; if(! [[Nsfilemanager Defaultmanager] Fileexistsatpath:createpath])//determine if the file already exists{BB=true; }    }  }  Else//if the bundle for the client is empty, also download the{[Self storefile:@"Version.txt"CONTENT:STR]; BB=true; }  if(bb==true) {Createpath=[nsstring stringWithFormat:@"%@/%@", Pathdocuments,@"Main.jsbundle"]; if([[[Nsfilemanager Defaultmanager] Fileexistsatpath:createpath])//determine if the file already exists{[Defaultmanager removeitematpath:createpath error:null]; } SS=@"####";//address of the service-side bundle filestr =[self httpget:ss]; NSString*filename=[self StoreFile:@"Main.jsbundle"CONTENT:STR]; Nsurl=[nsurl Fileurlwithpath:filename];//This is the path to the bundle file      }  Else{Createpath=[nsstring stringWithFormat:@"%@/%@", Pathdocuments,@"Main.jsbundle"]; Nsurl=[nsurl Fileurlwithpath:createpath];//This is the path to the bundle file  }  returnNsurl; }-(NSString *) HttpGet: (NSString *) ss{//The first step is to create the URLNsurl *url =[Nsurl Urlwithstring:ss]; //The second step is to create a network request via a URLNsurlrequest *request = [[Nsurlrequest alloc]initwithurl:url Cachepolicy:nsurlrequestuseprotocolcachepolicy timeOutInterval:Ten]; //Nsurlrequest Initialization Method first parameter: Request access path, second parameter: Cache protocol, third parameter: Network request time-out (seconds)  /*where the cache protocol is an enumeration type contains: Nsurlrequestuseprotocolcachepolicy (underlying policy) Nsurlrequestreloadignoringlocalcachedata (ignoring local cache) Nsurlrequestreturncachedataelseload (use cache first, if there is no local cache, download from the original address) Nsurlrequestreturncachedatadontload (use local cache, never download,   If there is no cache locally, the request fails, and this policy is mostly used for offline operation Nsurlrequestreloadignoringlocalandremotecachedata (ignoring any caching policies, whether local or remote, always re-downloaded from the original address) Nsurlrequestreloadrevalidatingcachedata (does not download if the local cache is valid, and any other cases are re-downloaded from the original address)*/  //step three, connect the serverNSData *received =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; NSString*str =[[NSString alloc]initwithdata:received encoding:nsutf8stringencoding]; returnstr;}//Storing Files-(NSString *) StoreFile: (NSString *) fileName content: (NSString *) writecontent{NSString*pathdocuments=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) ObjectAtIndex:0]; NSString*createpath=[nsstring stringWithFormat:@"%@/%@", Pathdocuments,filename];//complete path with file nameNserror *Readfileerror; NSString*readcontent; NSData*data; if([[[Nsfilemanager Defaultmanager] Fileexistsatpath:createpath])//determine if the file already exists  {    if(Nil = =writecontent) {readcontent= [NSString stringwithcontentsoffile:createpath encoding:nsutf8stringencoding error:&Readfileerror]; }Else{Data= [Writecontent datausingencoding:nsutf8stringencoding];//initial data for new files[[Nsfilemanager Defaultmanager] Createfileatpath:createpath contents:data Attributes:nil];//Create a fileReadContent = [NSString stringwithcontentsoffile:createpath encoding:nsutf8stringencoding error:&Readfileerror]; }  }  Else  {    if(Nil = =writecontent) {      returnNil; }Else{Data= [Writecontent datausingencoding:nsutf8stringencoding];//initial data for new files[[Nsfilemanager Defaultmanager] Createfileatpath:createpath contents:data Attributes:nil];//Create a fileReadContent = [NSString stringwithcontentsoffile:createpath encoding:nsutf8stringencoding error:&Readfileerror]; }  }  returnCreatepath;}

The above is the main code, there are comments explaining the meaning

The iOS bundle package script is as follows

React-native bundle--entry-filefalse

React native iOS version hot update

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.