小编Chi*_*ong的帖子

如何在iPhone上HTTP连接失败时弹出警报?

我想编写一些代码来处理HTTP连接失败时的异常.我使用以下代码:

-(void) connection:(NSURLConnection *)connection
  didFailWithError: (NSError *)error {
    UIAlertView *errorAlert = [[UIAlertView alloc]
                    initWithTitle: [error localizedDescription]
                    message: [error localizedFailureReason]
                    delegate:nil
                    cancelButtonTitle:@"OK"
                    otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    [activityIndicator stopAnimating];
    NSLog (@"Connection Failed with Error");
}
Run Code Online (Sandbox Code Playgroud)

但是当连接失败时程序崩溃了.如何在没有程序崩溃的情况下弹出警报?

iphone connection exception-handling exception http

5
推荐指数
1
解决办法
1594
查看次数

关于iPhone上URL的NSString字符无效的问题

我的一些http请求在URL中有"空格",但iPhone无法识别.只要网址中有"空格",我就必须将"空格"更改为"%20".现在我必须将用户输入的消息发送到服务器,它可以拥有与用户一样多的"空格".看起来我必须全部替换它们.(stringByReplacingOccurrencesOfString)

将'space'转换为'%20'是我偶尔会找到的.我的问题是NSString中的哪些其他角色不能被iPhone上的网址直接使用?

iphone url nsstring

5
推荐指数
1
解决办法
4432
查看次数

是否可以通过蓝牙将iphone连接到打印机?

我想通过蓝牙将iphone连接到打印机,但我在iPhone 3.0 SDK中找不到任何关于蓝牙的课程.我是否需要加入"Made for iPod并与iPhone许可计划合作"才能获得SDK?或者可以在不加入该计划的情况下完成此操作?

iphone bluetooth iphone-sdk-3.0

5
推荐指数
1
解决办法
1万
查看次数

如何在iPhone中删除一行表

我有一个UITableView,我想为用户提供功能,当他在行上滑动或轻弹手指时删除该行.我知道编辑样式,它提供了一个带有-ve标志的圆形红色按钮.但是如何实现轻弹风格.我看到许多应用程序使用它,因此apple为它提供了任何内置委托,或者我们需要为它编写自己的控制器.

iphone objective-c uitableview

5
推荐指数
1
解决办法
3万
查看次数

如何在 iPhone 上使用 OpenAL 播放循环声音

我正在学习有关使用 OpenAL 播放声音的教程。现在一切正常,除了我无法让声音循环播放。我相信我已经使用了AL_LOOPING源代码。现在它只能播放一次,当播放完毕时,应用程序将被阻止(不响应我点击播放按钮)。关于代码有什么问题有什么想法吗?

// start up openAL
// init device and context
-(void)initOpenAL
{
    // Initialization
    mDevice = alcOpenDevice(NULL); // select the "preferred device"
    if (mDevice) {
        // use the device to make a context
        mContext = alcCreateContext(mDevice, NULL);
        // set my context to the currently active one
        alcMakeContextCurrent(mContext);
    }
}


// open the audio file
// returns a big audio ID struct
-(AudioFileID)openAudioFile:(NSString*)filePath
{
    AudioFileID outAFID;
    // use the NSURl instead of a cfurlref cuz it is …
Run Code Online (Sandbox Code Playgroud)

iphone audio loops openal objective-c

5
推荐指数
1
解决办法
4041
查看次数

如何在Windows Mobile上解码url编码字符串?

我从服务器收到了一个url编码字符串,例如http%3a%2f%2fstatic.csbew.com%2f%2fcreative%2fpd_test_pptv%2f320x60.png

我想将它解码为普通的url字符串.我发现这个方法,但这不适用于紧凑的框架.

string url = System.Web.HttpUtility.UrlDecode(strURL, Encoding.GetEncoding("GB2312"));
Run Code Online (Sandbox Code Playgroud)

有关如何解码字符串的任何想法?

c# urlencode windows-mobile

5
推荐指数
1
解决办法
6937
查看次数

如何从iPhone上的URL接收数据?

我正在使用Apple的Document中的代码进行一些HTTP通信.我可以成功连接到URL,但是我无法从服务器接收数据.

// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..."]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    NSMutableData *receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}
Run Code Online (Sandbox Code Playgroud)

原因可能是:

  1. receivedData在行动中宣布.注释说我应该在其他地方声明它.我应该在哪里申报?我应该将其声明为控制人的财产吗?

  2. 如何[[NSMutableData data] retain]找到URL以外的URL …

iphone connection communication http nsurlconnection

3
推荐指数
1
解决办法
5488
查看次数

真的iPhone设备上有'getStreamsToHost'吗?

我想用服务器的示例代码将NSOutputStream写入服务器:


NSURL *website = [NSURL URLWithString:str_IP];
NSHost *host = [NSHost hostWithName:[website host]];
[NSStream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];
[oStream retain];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream open];
Run Code Online (Sandbox Code Playgroud)

这些代码在iPhone模拟器上运行良好,但是当我将它构建到真实设备时.弹出两个警告.问题是:

1)类NSHost不属于iphone os库

2)也找不到getStreamsToHost

对可以在真实设备上使用的替代方法或类的任何建议?

iphone objective-c stream

3
推荐指数
1
解决办法
6897
查看次数

如何在iPhone上旋转Quartz绘制的文本

我想用Quartz绘制一些文本.现在该应用程序绘制 alt文本http://www.freeimagehosting.net/uploads/71a84c9b49.png,但我希望它显示为"0123456789".有没有办法显示其正常方向?

这是我的代码:

    //set image view
 drawImage = [[UIImageView alloc] initWithImage:nil];
 drawImage.frame = self.view.frame;
 [self.view addSubview:drawImage];

 UIGraphicsBeginImageContext(self.view.frame.size);  // begin
 // current context
 CGContextRef context = UIGraphicsGetCurrentContext();

 CGContextSelectFont(context, "Courier", 40,  kCGEncodingMacRoman);
 CGFloat white[] = {1.0, 1.0, 1.0, 1.0};
 CGContextSetFillColor(context, white);
 CGContextShowTextAtPoint(context, 0, 30, "0123456789", strlen("0123456789"));

 // get image
 drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();    // end
Run Code Online (Sandbox Code Playgroud)

iphone text objective-c rotation quartz-graphics

3
推荐指数
1
解决办法
5590
查看次数

何时关闭NSOutputStream?

我想通过套接字将UIImage的数据发送到服务器,所以我:

a)打开NSOutputStream


- (IBAction)send:(id)sender {
    NSURL *website = [NSURL URLWithString:str_IP];
    NSHost *host = [NSHost hostWithName:[website host]];
    [NSStream getStreamsToHost:host port:1100 inputStream:nil outputStream:&oStream];
    [oStream retain];
    [oStream setDelegate:self];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [oStream open];
}
Run Code Online (Sandbox Code Playgroud)

b)当空间可用时将数据写入oStream


- (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
{
switch(eventCode)
    {
    case NSStreamEventHasSpaceAvailable:
    {
            if(stream == oStream)
            {
                    NSData *data = UIImageJPEGRepresentation(drawImage.image, 90);
                    //Convert from host to network endianness
                    uint32_t length = (uint32_t)htonl([data length]);

                    [oStream write:(uint8_t *)&length maxLength:4];
                    [oStream write:[data bytes] maxLength:length];
            }
    }
    break;
} …
Run Code Online (Sandbox Code Playgroud)

sockets cocoa objective-c stream

2
推荐指数
1
解决办法
2196
查看次数