JSVirtualMachine
JavaScript代碼是在創(chuàng)建JSVirtualMachine類的虛擬機下執(zhí)行,不能在兩個不同JSVirtualMachine類之間進行數(shù)據(jù)傳輸,每個JSVirtualMachine類實例有自己的堆和垃圾回收器.JSContext
一個JSContext對象代表一個JavaScript代碼執(zhí)行環(huán)境,是一個全局對象,它在web開發(fā)中等同于一個window對象,同一個JSVirtualMachine類下不同的JSContext之間可以自由傳值疯淫。-
JSValue
是一個主要的數(shù)據(jù)類型,能夠代表任何可能的JavaScript的值,一個JSValue的實例綁定到它所在的JSContext對象中正塌。
-
JSExport
一種協(xié)議提供了聲明式的方法去向JavaScript代碼導出Objective-C的實例類及其實例方法,類方法和屬性
//懶加載JSContext
-(JSContext *)context{
if (_context == nil) {
_context = [[JSContext alloc] init];
NSString *commonJSPath=[[NSBundle mainBundle] pathForResource:@"common" ofType:@"js"];
NSString *additionsJSPath=[[NSBundle mainBundle] pathForResource:@"additions" ofType:@"js"];
NSString *commomStr=[NSString stringWithContentsOfFile:commonJSPath encoding:NSUTF8StringEncoding error:nil];
NSString *additionsStr=[NSString stringWithContentsOfFile:additionsJSPath encoding:NSUTF8StringEncoding error:nil];
//將OC中的Movie類對象和JavaScript中的Movie對象橋接在一起
[_context setObject:[Movie self] forKeyedSubscript:@"Movie"];
//執(zhí)行js代碼
[_context evaluateScript:commomStr];
[_context evaluateScript:additionsStr];
}
return _context;
}
-(void)loadMoviesWith:(double)limit moviesBlock:(void(^)(NSArray *movies))moviesBlock
{
AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
NSURLSessionDataTask *dataTask=[mgr GET:MovieURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *jsonString=[self switchObjectToJSON:responseObject];
//解析JSON數(shù)據(jù)并傳出Movie模型數(shù)組
moviesBlock([self parseResponseObject:jsonString limit:limit]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"MovieViewData Load Error %@",error);
}];
[dataTask resume];
}
//解析JSON數(shù)據(jù)
-(NSArray *)parseResponseObject:(NSString *)response limit:(double)limit{
//1
JSValue *parseFunction=[self.context objectForKeyedSubscript:@"parseJson"];
NSArray *parsed=[[parseFunction callWithArguments:@[response]] toArray];
//2
JSValue *filterFunction=[self.context objectForKeyedSubscript:@"filterByLimit"];
NSArray *filtered=[filterFunction callWithArguments:@[parsed,@(limit)]].toArray;
//3
return [self switchDictionaryArrayToModelArrayWith:filtered];
}
第二步自定義cell中數(shù)據(jù)設置:
-(void)setMovie:(Movie *)movie
{
_movie=movie;
_nameLabel.text=movie.title;
_priceLabel.text=movie.price;
[_imageView sd_setImageWithURL:[NSURL URLWithString:movie.imageUrl]];
}
第三部在控制器調用并加載數(shù)據(jù):
-(NSArray *)movies
{
if (_movies == nil) {
_movies=[[NSArray alloc] init];
}
return _movies;
}
#pragma mark -- UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//調用此方法 觸發(fā)textFieldDidEndEditing:方法調用
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
double number=[textField.text doubleValue];
MovieViewData *mvData= [[MovieViewData alloc] init];
[mvData loadMoviesWith:number moviesBlock:^(NSArray *movies) {
self.movies=movies;
[self.collectionView reloadData];
}];
}
#pragma mark -- UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.movies.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MovieViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:MovieViewCellID forIndexPath:indexPath];
cell.movie=self.movies[indexPath.row];
return cell;
}
原生應用與JavaScript交互之內部結構剖析: