2013-04-29 54 views
0

我想檢查登錄到Gmail或雅虎後的JSON網址,但條件失敗,我試着下面的代碼。ios - 如何在ios中調用JavaScript?

如何聲明javascript,我宣佈正確的方法?

我以下鏈接,請通過它http://www.stevesaxon.me/posts/2011/window-external-notify-in-ios-uiwebview/

- (BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request 
navigationType:(UIWebViewNavigationType)navigationType 

{ 
    //_url = [[NSURL alloc] initWithString:websiteUrl]; 
    _url = [[NSURL alloc] initWithString:@"JSON URL"]; 
    if(_url) 
    { 

     if([_url isEqual:[request URL]]) 
     { 
      return YES; 
     } 

     [_url release]; 
    } 

    _url = [[request URL] retain]; 
    NSString* scheme = [_url scheme]; 

    //Condition fails here after logged in 

    if([scheme isEqualToString:@"acs"]) 
    { 
     // parse the JSON URL parameter into a dictionary 
     NSDictionary* pairs = [self parsePairs:[_url absoluteString]]; 
     if(pairs) 
     { 
      WACloudAccessToken* accessToken; 
      accessToken = [[WACloudAccessToken alloc] initWithDictionary:pairs]; 
      [WACloudAccessControlClient setToken:accessToken]; 

      [self dismissModalViewControllerAnimated:YES]; 
     } 

     return NO; 
    } 

    [NSURLConnection connectionWithRequest:request delegate:self]; 

    return NO; 

} 



- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 
    if(_data) 
    { 
     NSString* content = [[NSString alloc] initWithData:_data 
                encoding:NSUTF8StringEncoding]; 

     [_data release]; 
     _data = nil; 

     NSString *jsString = @"window.external =\ 
     {\ 
     'Notify': function(s) { document.location = 'acs://settoken?token=' + s; },\ 
     'notify': function(s) { document.location = 'acs://settoken?token=' + s; }\ 
     }"; 

     content = [jsString stringByAppendingString:content]; 

     //NSURL *url = [[NSURL alloc] initWithString:websiteUrl]; 
     NSURL *url = [[NSURL alloc] initWithString:@"JSON URL"]; 
     [webView loadHTMLString:content baseURL:url]; 
    } 

} 
+0

我真的沒有在第一個塊的開頭獲得{{_url = [[NSURL alloc] initWithString:@「JSON URL」];}}部分。你爲什麼在那裏分配? – Amandir 2013-04-29 10:20:47

+0

這是在Windows Azure中註冊的URL,對不起,我沒有權利共享該URL,這就是爲什麼我只給出了「JSON URL」 – 2013-04-29 10:24:43

回答

1
 NSString *jsString = @"window.external =\ 
     {\ 
     'Notify': function(s) { document.location = 'acs://settoken?token=' + s; },\ 
     'notify': function(s) { document.location = 'acs://settoken?token=' + s; }\ 
     }"; 

應該是:

 NSString *jsString = @"<script type='text/javascript'>\ 
     window.external =\ 
     {\ 
     'Notify': function(s) { document.location = 'acs://settoken?token=' + s; },\ 
     'notify': function(s) { document.location = 'acs://settoken?token=' + s; }\ 
     }\ 
     </script>"; 

順便說一句,粗略地看一眼顯示了一些問題,你的內存管理(_url泄漏,content泄漏)。如果您不確定如何處理手動內存管理,我強烈建議打開ARC(自動引用計數)。

+0

非常感謝MarOux爲您的寶貴回覆 – 2013-04-30 05:18:37