公司項(xiàng)目在線學(xué)習(xí)模塊需要用webView播放在線培訓(xùn)視頻,后臺(tái)返回視頻的iframe并沒(méi)有直接返回視頻的url給我們,這需要我們自己去截取iframe里的src傳給webView播放,后臺(tái)返回的格式總共有3種類型如下:
videoPath = <iframe height=498 width=510 src=http://player.youku.com/embed/XMzE1OTMzOTM3Mg== frameborder=0 'allowfullscreen'></iframe>;
videoPath = <iframe height=498 width=510 src='http://player.youku.com/embed/XMzEzNzc3Mjc4MA==' frameborder=0 'allowfullscreen'></iframe>;
videoPath = <iframe src="http://open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid=69d0de5269cd1f9061049f231f23745f&tvId=829999100&accessToken=2.f22860a2479ad60d8da7697274de9346&appKey=3955c3425820435e86d0f4cdfe56f5e7&appId=1368&height=100%&width=100%" frameborder="0" allowfullscreen="true" width="100%" height="100%"></iframe>;
我這里用謂詞獲取html中iframe里的src傅联,抽成一個(gè)方法直接調(diào)用即可:
-(NSString*)getIframeSrcWithHtml:(NSString *)htmlText{
if (htmlText == nil) {
return nil;
}
NSError *error;
NSString *regulaStr = @"<iframe[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *arrayOfAllMatches = [regex matchesInString:htmlText options:0 range:NSMakeRange(0, [htmlText length])];
NSMutableArray *resultArray = [NSMutableArray array];
for (NSTextCheckingResult *item in arrayOfAllMatches) {
NSString *imgHtml = [htmlText substringWithRange:[item rangeAtIndex:0]];
NSArray *tmpArray = nil;
if ([imgHtml rangeOfString:@"src="].location != NSNotFound) {
tmpArray = [imgHtml componentsSeparatedByString:@"src="];
} else if ([imgHtml rangeOfString:@"src='"].location != NSNotFound) {
tmpArray = [imgHtml componentsSeparatedByString:@"src='"];
} else if ([imgHtml rangeOfString:@"src=\""].location != NSNotFound) {
tmpArray = [imgHtml componentsSeparatedByString:@"src=\""];
}
if (tmpArray.count >= 2) {
NSString *src = tmpArray[1];
NSArray *strArr = [src componentsSeparatedByString:@" "];
NSString *srcString = strArr.firstObject;
srcString = [srcString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
srcString = [srcString stringByReplacingOccurrencesOfString:@"'" withString:@""];
srcString = [srcString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[resultArray addObject:srcString];
}
}
return resultArray.firstObject;
}