"Festive skin Change" general technical solution __ios End implementation

Source: Internet
Author: User
Tags notification center

"Festive skin Change" general technical solution __ios End implementation

First, the question of the proposed

I do not know whether we have found that, during the new Year 's Day, many of the icons in the app interface have been replaced with festive mood style , and after the New Year festival , these icons quietly changed back to the original appearance .

How do these festive skins, which have a short life cycle and must be displayed on fixed-time nodes , actually be implemented ? Obviously , it can be achieved by releasing a new version, but for iOS apps , the Apple app Store uncertain audit time , developers often need 1-2 weeks in advance to complete and submit a review , and every to a festival to republish a new version , inevitably slightly passive .

    • Hot update is a good choice !

Imagine if all the interface elements that need to be replaced by a skin theme are abstracted out of a complete business model , and a dedicated skin manager is responsible for validating and asynchronously loading each time it is launched , then all the holiday skins will be released later. , we all only need to change a parameter in the background, we can make the user's app update to the newest skin theme instantly .

Of course , if your application is using React native, it's easier to change skin, but this article is a universal solution for iOS native applications , just a , if you have a more optimized program , welcome message proposed , we discuss learning together .

Ok , then we'll try to play a game now .

Second, the business process

To do one thing before , to clear the idea , do what, how to do, since the direction has been determined down , then need to comb the clue , I sort out such a flowchart :

Third, open the code

In fact, every square in the flowchart is the method we need, one-to- achieve .

  Request interface, judge, there are three kinds of cases, delete operation with Nsfilemanafer, download with afnetworking, decompression used to ssziparchive This is nothing to say, Just go to the code.

There's one thing worth discussing., which is the last way to load the skin., I started with the ideaKVO, later foundNotificationcenter will be handy., but it's going to be complicated.If you want to modify a lot of skin, every one must go to register a notification center and a receive method ? It's a little complicated. imagenamed: and colorwith two basic methods to replace Manager wrote two corresponding new method bundle Resources ok ! OK, directly on the code:

< Span class= "S4" > < Span class= "S4" > < Span class= "S4" >      looks simple as follows:

1 -(uiimage*) Imagenamedautomatch: (NSString *) name2{3     return  [UIImage Imagewithcontentsoffile:_currentpath]; 4 }5 -(uicolor*) Colorautomatch: (NSString *) itemname6{7      return  [Uicolor Colorwithhexstring:_colorarray[itemname]]; 8 }

The header file is like this, the common path is written in the macro:

#defineName_resource @ "Holidayskin"#defineDocumentpath [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) FirstObject] Stringbyappendingpathcomponent:name_resource]#defineBundle_path [[NSBundle Mainbundle]resourcepath]#defineKrequestdata/*the corresponding entry * /@interfaceDrskinmanager:nsobject@property (nonatomic, copy) NSString*skinstatus; @property (nonatomic, copy) NSString*DownloadURL; @property (nonatomic, copy) NSString*Currentpath, @property (nonatomic, strong) Nsarray*Colorarray;+(instancetype) Sharemanager;-(uiimage*) Imagenamedautomatch: (NSString *) name;-(uicolor*) Colorautomatch: (NSString *) ItemName;
    • Interface request, nothing to say:
-(void) checkskinstatue{    [drnetwork requestwithdictionary:krequestdata response:ID result) {        if  (success) {            *skinstaus = result[@ "skinstaus" ];             *downloadurl = result[@ "downloadurl"];            [Self judgeresourcestatus:skinstatus Url:downloadurl];}}    ];}
    • Compared to a state value, three cases, three branches:

- (void) Judgeresourcestatus: (NSString *) Skinstaus URL: (NSString *) downloadurl{if(Skinstaus.length = =0) {        return; } nsstring*localskinstatus =[[Nsuserdefaults Standarduserdefaults]valueforkey:name_resource];
if([Skinstaus isequaltostring:localskinstatus]) {self.skinstatus=Skinstaus; Self.currentpath=[Documentpath Stringbyappendingpathcomponent:skinstaus]; return; } if(![Skinstaus Isequaltostring:localskinstatus]) { if(Localskinstatus.length! =0) {Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; [FileManager Removeitematpath:[documentpath Stringbyappendingpathcomponent:localskinstatus] error:nil]; } Self.downloadurl=DownloadURL; [Self downloadandunzip]; }}

Download and decompress

    • - (void) downloadandunzip{NSString*zippath = [Documentpath stringbyappendingstring:@"/dontcare.zip"]; Afhttprequestoperation*operation =[[Afhttprequestoperation Alloc]initwithrequest:_downloadurl];    [Operation Setoutputstream:[nsoutputstream Outputstreamtofileatpath:zippath Append:no]; [Operation Setcompletionblockwithsuccess:^ (Afhttprequestoperation *operation,IDresponseobject) {                //after successful download: Unzip[ssziparchive unzipfileatpath:zippath Todestination:[documentpath Stringbyappendingpathcomponent:_skinstatus] Progresshandler:^ (NSString *entry, Unz_file_info Zipinfo,LongEntrynumber,LongTotal ) {}completionhandler:^ (NSString *path, BOOL succeeded, Nserror *error) {        //The extract is complete, so the skin resources are fully cached to the sandbox, delete the zip package, save SkinstatusNsfilemanager *filemanager = Nsfilemanager *filemanager =[Nsfilemanager Defaultmanager];        [FileManager Removeitematpath:zippath Error:nil];      [[Nsuserdefaults Standarduserdefaults]setvalue:_skinstatus Forkey:name_resource]; }} failure:^ (Afhttprequestoperation *operation, Nserror *error)                {success (operation,error); NSLog (@"Download Failed");        }]; [Operation start];}

  

"Festive skin Change" general technical solution __ios End implementation

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.