亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從Javascript調用Object-C?

如何從Javascript調用Object-C?

紫衣仙女 2019-07-15 14:58:58
如何從Javascript調用Object-C?我有一個WebView,我想從JavaScript調用Object-C中的一個視圖。有人知道我該怎么做嗎?我的ViewController中有以下代碼:- (BOOL)webView:(UIWebView *)webView2   shouldStartLoadWithRequest:(NSURLRequest *)request   navigationType:(UIWebViewNavigationType)navigationType {  NSString *requestString = [[request URL] absoluteString];  NSArray *components = [requestString componentsSeparatedByString:@":"];  if ([components count] > 1 &&    [(NSString *)[components objectAtIndex:0] isEqualToString:@"myapp"]) {   if([(NSString *)[components objectAtIndex:1] isEqualToString:@"myfunction"])    {    NSLog([components objectAtIndex:2]); [[Airship shared] displayStoreFront]; //<- This is the code to open the Store    NSLog([components objectAtIndex:3]); // param2    // Call your method in Objective-C method using the above...   }   return NO;  }  return YES; // Return YES to make sure regular navigation works as expected.}在Javascript中:function store(event){     document.location = "myapp:" + "myfunction:" + param1 + ":" + param2;}但什么都沒發生。
查看完整描述

3 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

標準的解決辦法UIWebView是設置一個UIWebViewDelegate,并實現了該方法。webView:shouldStartLoadWithRequest:navigationType:..在JavaScript代碼中,導航到某個偽URL,該URL編碼要傳遞給應用程序的信息,例如:

window.location = "fake://myApp/something_happened:param1:param2:param3";

在委托方法中,查找這些假URL,提取所需的信息,采取任何適當的操作,然后返回。NO取消導航。使用一些味道.performSelector.


查看完整回答
反對 回復 2019-07-15
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

不推薦從JS調用目標c的window.Location方法。問題的一個例子:如果你連續打了兩個電話,其中一個被忽略了(因為你不能太快地改變位置)-你自己試試吧。

我建議采取以下替代辦法:

function execute(url) {
  var iframe = document.createElement("IFRAME");
  iframe.setAttribute("src", url);
  document.documentElement.appendChild(iframe);
  iframe.parentNode.removeChild(iframe);
  iframe = null;}

你打電話給execute函數,由于每個調用都在自己的iframe中執行,因此在快速調用時不應忽略它們。

貸給這家伙.


查看完整回答
反對 回復 2019-07-15
  • 3 回答
  • 0 關注
  • 709 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號