iOS进程间通信

线程间通信 :通过performSelector系列的方法 可以实现 各种线程间的通信(通信 :调用与传参)
进程间通信:从一个App跳到另外一个App如从打地鼠练习跳转到卖票
卖票
首先添加一行info的myapp标示,这个标示表示当别个App要调用它时,要用到的表示


image.png

AppDelegate.m

#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
@synthesize window = _window;
-(BOOL)application:(UIApplication*)app openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    NSString *info = url.description;
    UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    l.text = info;
    l.backgroundColor = [UIColor yellowColor];
    [self.window.rootViewController.view addSubview:l];
    return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

打地鼠

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSString *path = @"myapp://hahahehe";
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:path] options:@{} completionHandler:^(BOOL success) {
        if(success){
            NSLog(@"成功");
        }else{
            NSLog(@"失败");
        }
    }];
}

练习图片爬虫
ViewController.m

#import "ViewController.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *myTF;
@property(nonatomic)NSInteger imageCount;
@property(nonatomic,strong)NSMutableArray *imagePaths;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSOperationQueue *myQueue;
@property(nonatomic,strong)NSMutableArray *urls;
@property(nonatomic,strong)NSMutableArray *usedUrls;
@property(nonatomic,strong)NSOperationQueue *urlQueue;

@property(nonatomic,strong)NSString *sourceCode;

@end

@implementation ViewController

-(UICollectionView*)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
        fl.itemSize = CGSizeMake(50, 50);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, 486, 896) collectionViewLayout:fl];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}
- (IBAction)clicked:(id)sender {
    [self findImageAndURLWithPath:self.myTF.text];
}
-(void)findImageAndURLWithPath:(NSString *)path{
    //如果用过的话就找其他页面(判断是否查找过这个地址里面的东西)
    if ([self.usedUrls containsObject:path]) {
        return;
    }
    //记录已经使用过的url
    [self.usedUrls addObject:path];
    NSBlockOperation *urlOp = [NSBlockOperation blockOperationWithBlock:^{
    
        if (!self.sourceCode) {
            NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
           self.sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:self.myTF.text] encoding:enc error:nil];
        }

        NSLog(@"%@",self.sourceCode);
        if(self.sourceCode.length<1000){
            return;
        }
        NSArray *arr = [self.sourceCode componentsSeparatedByString:@"\""];
        for (NSString *str in arr) {
            if ([str hasSuffix:@".jpg"]) {
                NSLog(@"%@",str);
                if ([str hasPrefix:@"//"]) {
                    [self.imagePaths addObject:[@"http:"stringByAppendingString:str]];
                }else{[self.imagePaths addObject:str];}
                    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
                        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
                        //图片大于100KB才保存
                        if (data.length>10) {
                        NSString *path = [NSString stringWithFormat:@"/Users/zhengyang/Desktop/jgps/%ld.jpg",self.imageCount++];
                        [data writeToFile:path atomically:YES];
                }
                    }];
                [self.myQueue addOperation:op];
            }
        }
        //获取所有的网址
        NSArray *urlsArr = [self.sourceCode componentsSeparatedByString:@"href=\""];
        for(NSString *str in urlsArr){
            NSString *urlPath = [str componentsSeparatedByString:@"\""].firstObject;
            //&&[urlPath containsString:@"运动"]加关键字(可以在此位置加个限制 必须包含某个关键字)
            if ([urlPath hasPrefix:@"http"]) {
                if (![self.urls containsObject:urlPath]&&![self.usedUrls containsObject:urlPath]) {
                    [self.urls addObject:urlPath];
                    //直接下载图片
                }
                [self.urls addObject:urlPath];
           } 
        }
        NSLog(@"%@",self.urls);
        for (NSString *path in self.urls) {
            [self findImageAndURLWithPath:path];
        }
    }];
    [self.urlQueue addOperation:urlOp];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.urlQueue = [NSOperationQueue new];
    self.urlQueue.maxConcurrentOperationCount = 1;
    self.imagePaths = [NSMutableArray array];
    self.urls = [NSMutableArray array];
    self.usedUrls = [NSMutableArray array];
    self.myQueue = [NSOperationQueue new];
    self.myQueue.maxConcurrentOperationCount = 5;
    _sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:self.myTF.text] encoding:NSUTF8StringEncoding error:nil];
   // [self.collectionView reloadData];
}
- (nonnull __kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    UIImageView *iv = [cell viewWithTag:1];
    if (!iv) {
        iv = [[UIImageView alloc]initWithFrame:cell.bounds];
        iv.backgroundColor = [UIColor redColor];
        iv.tag = 1;
        [cell addSubview:iv];
    }
    NSString *imagePath = self.imagePaths[indexPath.row];  
    return cell;
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imagePaths.count;
}

推荐阅读更多精彩内容