Similar to the cheetah spam cleanup (implementation principle + Source Code), the cheetah spam cleanup
Junk cleaning of imitation cheetahs (implementation principle + Source Code)
Reprinted, please specify the Source: fake cheetah garbage removal (implementation principle + Source Code)
I did not intend to open the cheetah memory master a few days ago and found that the junk cleaning was very powerful and the effect was good. I studied it when I was idle. However, the results seem to be different from what I imagined. Let me explain it one by one.
:
We can see that it has the following functions:
Do you think it is very powerful?
Then, while sighing, I have some questions.
Confused, puzzled ..~ That's all. It took two days. I wrote a demo.
The effect is as follows:
Next, I will introduce the implementation process of the following functions, including:
However, before analysis, Let's explain,This function cannot be uploaded to the App Store! That is to say, it does not pass the review.
There are two reasons:
1. Private APIs are used
2. Apple does not allow apps to process memory-related functions
As for the cheetah memory master App, it has already beenDismounted. I suspect that it uses obfuscation code to pass the review. As for the implementation of functions, I think it should be the same as the implementation of cheetah.
So far, if you are still interested in this article, please continue to read it.
Source code for this article: CSDN download _ anti-Cheetah Spam
Obtain the list of apps installed on the device. The App information is not jailbroken. Non-Private API
No jailbreaking device, and no api is provided officially, so you can only use some tips, but the content is incomplete.
There are two methods:
Method 1: Use URL scheme to check whether the url scheme exclusive to an application has a response. If there is a response, it indicates that this specific app is installed.
To be honest, this method is silly. How can I enumerate millions of apps in App Store? In addition, you cannot scan your demo. But someone is doing this ..
This is the corresponding demo. For more information, see. IHasApp
Official Tutorial: iPhoneURLScheme_Reference
Method 2: Use some methods to obtain information about the currently running process, and obtain information about the installed app from the process information.
Reference: UIDevice_Category_For_Processes
In general, it is almost impossible to obtain a complete list of non-private APIs without jailbreak.
Private API.
Here is the method used by my demo, which is relatively simple.
#include <objc/runtime.h>Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace"); NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)]; NSLog(@"apps: %@", [workspace performSelector:@selector(allApplications)]);
Returned results
"LSApplicationProxy: com.qunar.iphoneclient8", "LSApplicationProxy: com.apple.mobilemail", "LSApplicationProxy: com.apple.mobilenotes", "LSApplicationProxy: com.apple.compass", "LSApplicationProxy: com.tencent.happymj", "LSApplicationProxy: com.apple.mobilesafari", "LSApplicationProxy: com.apple.reminders"
Returned data. Each element isLSApplicationProxy. Its description only returns its bundle id. However, this is not what we want.
Next let's take a look.
LSApplicationProxy. h
Shape:
@class LSApplicationProxy, NSArray, NSDictionary, NSProgress, NSString, NSURL, NSUUID;@interface LSApplicationProxy : LSResourceProxy <NSSecureCoding> { NSArray *_UIBackgroundModes; NSString *_applicationType; NSArray *_audioComponents; unsigned int _bundleFlags; NSURL *_bundleURL; NSString *_bundleVersion; NSArray *_directionsModes; NSDictionary *_entitlements; NSDictionary *_envi ... ...
Here we listLSApplicationProxyCorresponding attributes and methods.
We can use the following code to print the values of each attribute and find out what we want.
2./* obtain all attributes and attribute values of an object */-(NSDictionary *) properties_aps {NSMutableDictionary * props = [NSMutableDictionary dictionary]; unsigned int outCount, I; objc_property_t * properties = class_copyPropertyList ([self class], & outCount); for (I = 0; I <outCount; I ++) {objc_property_t property = properties [I]; const char * char_f = property_getName (property); NSString * propertyName = [NSString stringwithuf8string: char_f]; id propertyValue = [self valueForKey :( NSString *) propertyName]; if (propertyValue) [props setObject: propertyValue forKey: propertyName];} free (properties); return props ;}
Reference: IOS traverses attributes and methods of unknown objects
Then we extract the icons and application names we need.
[appsInfoArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSDictionary *boundIconsDictionary = [obj performSelector:@selector(boundIconsDictionary)]; NSString *iconPath = [NSString stringWithFormat:@"%@/%@.png", [[obj performSelector:@selector(resourcesDirectoryURL)] path], [[[boundIconsDictionary objectForKey:@"CFBundlePrimaryIcon"] objectForKey:@"CFBundleIconFiles"]lastObject]]; UIImage *image = [[[UIImage alloc]initWithContentsOfFile:iconPath] TransformtoSize:CGSizeMake(65, 65)]; if (image) { [self.appsIconArr addObject:image]; [self.appsNameArr addObject:[obj performSelector:@selector(localizedName)]]; } }];
So,_self.appsIconArrAnd_appsNameArrThe App data we need is stored in.
Jailbreak
.. I don't understand it here, nor did I study it. For more information, seeMobileInstallation.framework
Scan animation implementation
There are mainly two animations.
As for animation, I don't want to introduce it too much here. The source code is clearly written. (Of course, writing is rough ...)
Simple implementation of the animation with a scanning line:
/* Move to the left */CABasicAnimation * animationLeft = [CABasicAnimation animationWithKeyPath: @ "transform. translation. x "]; // set animation options. duration = 0.5f; // duration: animationLeft. beginTime = 0.0f; animationLeft. autoreverses = YES; // run the reverse animation after the animation ends. // The animation accelerates first and then slows down the animation. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; // The animation left of the end frame. toValue = [NSNumber numberWithFloat:-40];/* move to the right */CABasicAnimation * animationRight = [CABasicAnimation animationWithKeyPath: @ "transform. translation. x "]; // set animation options. duration = 0.5f; // duration: animationRight. beginTime = 1.0f; animationRight. autoreverses = YES; // run the reverse animation after the animation ends. // The animation accelerates first and then slows down the animation. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; // The animation right of the end frame. toValue = [NSNumber numberWithFloat: 40];/* animation group */CAAnimationGroup * group = [CAAnimationGroup animation]; group. delegate = self; group. duration= 2.0; group. repeatCount = 15; // The animation does not change back to the initial state group after it ends. removedOnCompletion = NO; group. fillMode = kCAFillModeForwards; // Add an animation group. animations = [NSArray arrayWithObjects: animationLeft, animationRight, nil]; [mySL. layer addAnimation: group forKey: @ "moveLeft-moveRight-layer"];
Obtain used and available storage
There is nothing to say about it. Apple provides APIs and you can use them directly.
// Obtain the occupied memory-(void) usedSpaceAndfreeSpace {NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; NSFileManager * fileManager = [[NSFileManager alloc] init]; NSDictionary * fileSysAttributes = [fileManager failed: path error: nil]; NSNumber * freeSpace = [fileSysAttributes objectForKey: NSFileSystemFreeSize]; NSNumber * totalSpace = [fileSysAttributes objectForKey: NSFileSystemSize]; NSString * str = [NSString stringWithFormat: @ "occupied % 0.1f G/remaining % 0.1f MB ", ([totalSpace longLongValue]-[freeSpace longLongValue])/1024.0/1024.0/1024.0, [freeSpace longLongValue]/1024.0/1024.0]; NSLog (@ "-------- % @", str );}
Garbage removal
I didn't want to mention it here. After all, Apple cannot accept this function.
As mentioned earlier, when the cheetah is being cleaned up"Storage full prompt". Then I started to think about it.
Finally, I have determined the implementation method of the Cheetah. It only triggers Apple's own garbage collection mechanism.
When the storage is full, the system automatically cleans up the garbage and prompts that the storage is full.
Therefore, the cheetah only calculates how much storage is left and then creates a junk file of the same size. Then, Apple's cleanup mechanism is triggered. After cleaning, delete the generated junk files. Count the available storage again. The difference value is the size of the garbage to be cleared.
Actually, it's not so powerful ~
How can we quickly create hundreds of MB or even several GB of junk files?
// Set the object length to offset-(void) truncateFileAtOffset: offset
truncateFileAtOffset:offsetYou can get it done. If you are interested, you can study it on your own.
Now, the spam analysis is complete.
Of course, this is my personal opinion. If there is a better way or there are any errors in the article. Thank you for your comments.