iOS【html混編】

#import <UIKit/UIKit.h>
#import <JavaScriptCore/JavaScriptCore.h>

@protocol PersonCenterJsObjectProtocol <JSExport>
//使用JSExportAs宏镣衡,可以將方法名簡(jiǎn)化
JSExportAs(toShareDialog,
- (void)toShareDialog:(NSString *)shareTitle :(NSString *)shareUrl :(NSString *)shareContent
           
);

- (void)toHomePage;

@end

@interface FFEasyLifeRecommendCtrl : UIViewController <UIWebViewDelegate ,PersonCenterJsObjectProtocol>

@property (nonatomic, copy) NSString *webUrl;
@property (nonatomic, copy) NSString *webTitle;

@end
@interface FFEasyLifeRecommendCtrl ()
@property (nonatomic, strong) JSContext *context;
@property (nonatomic, strong) JSManagedValue *managedPlugin;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) FFEasyLifeShareView *shareWindow;
@end


@implementation FFEasyLifeRecommendCtrl

#pragma mark - LifeCycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self unloadColorsPlugin];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.webView];    
    [self initWebViewDataWithPath:nil];
}


#pragma mark - 建立js環(huán)境
-(void)makeJsEnvirement {
    
    JSContext *context = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@"dmc"] = self;
    
    _context = context;
    
    self.context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
        NSLog(@"異常信息:%@",exception);
        context.exception = exception;
    };
}

- (void)unloadColorsPlugin {
    [_context.virtualMachine removeManagedReference:_managedPlugin withOwner:self];
    _managedPlugin = nil;
    _context = nil;
}

#pragma mark - PersonCenterJsObjectProtocol
- (void)toShareDialog:(NSString *)shareTitle :(NSString *)shareUrl :(NSString *)shareContent {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.shareWindow = [[FFEasyLifeShareView alloc] initShareView];
        [self.shareWindow shareWithShareTitle:shareTitle Text:shareContent shareImage:[UIImage imageNamed:@"share_recommend"]  webUrl:shareUrl delegate:self];
        [self.view addSubview:self.shareWindow];
    });
}

- (void)toHomePage {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popToRootViewControllerAnimated:NO];
    });
}



#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if([request isKindOfClass:[NSMutableURLRequest class]]) {
        
    }
    return true;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    self.webView.userInteractionEnabled = NO;
    NSLog(@"title11=%@",title);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self makeJsEnvirement];
    self.webView.userInteractionEnabled = YES;
    self.loading.hidden = YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    self.loading.hidden = YES;
    self.noDataView.hidden = NO;
    NSLog(@"%@",error.description);
}

@end

#import <JavaScriptCore/JavaScriptCore.h>

@protocol WebDetailJsObjectProtocol <JSExport>

JSExportAs(toGoodsDetail,
           - (void)toGoodsDetail:(NSString *)Goods :(NSString *)callback

);

- (void)goAppHome;
@end



#pragma mark - 建立js環(huán)境
-(void)makeJsEnvirement {
    
    JSContext *context = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@"dmc"] = self;
    
    _context = context;
    
    self.context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
        NSLog(@"異常信息:%@",exception);
        context.exception = exception;
    };
}

- (void)unloadColorsPlugin {
    [_context.virtualMachine removeManagedReference:_managedPlugin withOwner:self];
    _managedPlugin = nil;
    _context = nil;
}


#pragma mark -
#pragma mark - JSObjcDelegate

- (void)toGoodsDetail:(NSString *)Goods :(NSString *)callback{
    
    NSData *goodsData = [Goods dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:goodsData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    
    
    
    if ([dic isKindOfClass:[NSDictionary class]]) {
        
        FFEasyLifeSimilarRCModel *model = [[FFEasyLifeSimilarRCModel alloc] initWithDic:dic];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            FFEasyLifeActivityDetailCtrl *ctrl = [[FFEasyLifeActivityDetailCtrl alloc] initWithNibName:kFFEasyLifeActivityDetailCtrl bundle:nil];
            ctrl.idx = [NSString limitStringNotEmpty:model.goodsId];
            ctrl.activityId = [NSString limitStringNotEmpty:model.activityId];
            ctrl.actType = [NSString limitStringNotEmpty:model.type];
            
            if (self.navigationController) {
                [self.navigationController pushViewController:ctrl animated:YES];
                
            } else {
                ctrl.isInform = YES;
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:ctrl];
                nav.navigationBarHidden = YES;
                [self presentViewController:nav animated:YES completion:nil];
            }
        });
        
    }
}

- (void)goAppHome {
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.navigationController) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    });
    
}



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末霜定,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子廊鸥,更是在濱河造成了極大的恐慌望浩,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惰说,死亡現(xiàn)場(chǎng)離奇詭異磨德,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)吆视,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)典挑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人啦吧,你說(shuō)我怎么就攤上這事您觉。” “怎么了授滓?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵顾犹,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我褒墨,道長(zhǎng),這世上最難降的妖魔是什么擎宝? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任郁妈,我火速辦了婚禮,結(jié)果婚禮上绍申,老公的妹妹穿的比我還像新娘噩咪。我一直安慰自己,他們只是感情好极阅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布胃碾。 她就那樣靜靜地躺著,像睡著了一般筋搏。 火紅的嫁衣襯著肌膚如雪仆百。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天奔脐,我揣著相機(jī)與錄音俄周,去河邊找鬼吁讨。 笑死,一個(gè)胖子當(dāng)著我的面吹牛峦朗,可吹牛的內(nèi)容都是我干的建丧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼波势,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼翎朱!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起尺铣,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤拴曲,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后迄埃,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體疗韵,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年侄非,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蕉汪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡逞怨,死狀恐怖者疤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叠赦,我是刑警寧澤驹马,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站除秀,受9級(jí)特大地震影響糯累,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜册踩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一泳姐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧暂吉,春花似錦胖秒、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至肮街,卻和暖如春风题,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工俯邓, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留骡楼,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓稽鞭,卻偏偏與公主長(zhǎng)得像鸟整,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子朦蕴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容