将链接置于UIWebView中并在不同的视图控制器上显示

时间:2013-08-07 12:53:12

标签: ios objective-c uiwebview uiwebviewdelegate

我有一个视图控制器,我使用Apple的地址簿创建了一个简单的联系人应用程序,我创建了另一个视图控制器,它有一个Web视图,可以在用户单击生成按钮时创建并显示所有联系人和电话号码。现在我需要的是,在生成联系人时应该是超链接,当我点击任何联系人时,它应该显示我在第一个视图控制器中完成的联系人的详细信息。我知道我必须使用删除webview但不知道如何实现它,到目前为止这是我写的所有内容。

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

    ViewController *latestView = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    [self.navigationController pushViewController:latestView animated:YES]; 
    return YES;
}

这是在webview中生成和显示联系人的功能,在此我想知道如何添加超链接以及联系人详细信息

- (IBAction)createFileAction:(id)sender {



    NSString *tweet, *phoneNumbers=@"", *temp, *allContacts=@"";
    addressBooks =ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBooks);
    for (CFIndex i = 0; i < CFArrayGetCount(people); i++)
    {
        person12 = CFArrayGetValueAtIndex(people, i);
         tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        //Appending Last Name
        if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonLastNameProperty))!=NULL)
        {
            tweet=[tweet stringByAppendingString:@" "];
            tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyValue(person12, kABPersonLastNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
        }


        tweet=[@"<h5>" stringByAppendingString:[NSString stringWithFormat:@"%@",tweet]];
        if(CFBridgingRelease(ABRecordCopyValue(person12,kABPersonFirstNameProperty))!=NULL)
        {


            ABMultiValueRef multi = ABRecordCopyValue(person12, kABPersonPhoneProperty);
            if(ABMultiValueGetCount(multi)!=0)
            {
            //For fetching multiple Phone Numbers
            for (int i=0; i < ABMultiValueGetCount(multi); i++) 
                {
            temp = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, i);
                phoneNumbers=[phoneNumbers stringByAppendingString:[NSString stringWithFormat:@"%@/",temp]];
                }



            //For Trimming characters in contacts (),-," "

            NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"()-\" \""];
            phoneNumbers = [[phoneNumbers componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
            phoneNumbers=[phoneNumbers substringToIndex:[phoneNumbers length]- 1];
            if([phoneNumbers rangeOfString:@"/"].location!=NSNotFound)
            {
                phoneNumbers = [phoneNumbers stringByReplacingOccurrencesOfString:@"/" withString:@" / "];
            }
            }
            else
            {
                phoneNumbers=[phoneNumbers stringByAppendingString:@"No Numbers"];

            }

            tweet=[tweet stringByAppendingString:@" ---> "];
            tweet=[tweet stringByAppendingString:[[NSString stringWithFormat:@"%@",phoneNumbers] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
            phoneNumbers=@"";

            NSLog(@"%@",tweet); 

        allContacts = [allContacts stringByAppendingFormat:@"%@</h5>",tweet];
        }

    }
    CFBridgingRelease(people);


        // To create file
        [self.fileMgr createFileAtPath:file contents:nil attributes:nil];

        [allContacts writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];


        //Test if file exists now that we have tried creating it
        if([self.fileMgr fileExistsAtPath:self.file])
        {

            NSString *content = @"Contact list generated click on view to show the list";
            [webview loadHTMLString:content baseURL:nil];
        }
        else 
        {

            NSString *content = @"Contact list is not created yet, click on create to generate the contact list";
            [webview loadHTMLString:content baseURL:nil];
        }



}

您能否建议对这两个代码进行修改,以帮助我实现结果?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望打开HTML链接和ViewController实例。基本上,您可以创建任何类型的链接<a href="myapp://{some_id_of_the_person}">click here</a>,假设您要将此人的ID传递给ViewController。

然后在[UIWebView webView:shouldStartLoadWithRequest:navigationType:]中检查示例中request.URL.scheme是否与您的计划(myapp://)匹配:

  • 如果是,请解析该人员ID的URL并打开该人员的viewController(以及return NO方法)。
  • 如果不只是return YES(假设视图中可能存在一些应该正常打开的其他标准链接)。